Skip to content

Commit d6816af

Browse files
authored
Merge pull request webpack#6355 from webpack/for_of_loops
Use for-of loops instead of forEach
2 parents 9c8098c + bcbd878 commit d6816af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+435
-372
lines changed

lib/BannerPlugin.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,50 @@ class BannerPlugin {
3535
apply(compiler) {
3636
const options = this.options;
3737
const banner = this.banner;
38+
const matchObject = ModuleFilenameHelpers.matchObject.bind(undefined, options);
3839

3940
compiler.hooks.compilation.tap("BannerPlugin", (compilation) => {
4041
compilation.hooks.optimizeChunkAssets.tap("BannerPlugin", (chunks) => {
41-
chunks.forEach((chunk) => {
42-
if(options.entryOnly && !chunk.canBeInitial()) return;
43-
chunk.files
44-
.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
45-
.forEach((file) => {
46-
let basename;
47-
let query = "";
48-
let filename = file;
49-
const hash = compilation.hash;
50-
const querySplit = filename.indexOf("?");
51-
52-
if(querySplit >= 0) {
53-
query = filename.substr(querySplit);
54-
filename = filename.substr(0, querySplit);
55-
}
56-
57-
const lastSlashIndex = filename.lastIndexOf("/");
58-
59-
if(lastSlashIndex === -1) {
60-
basename = filename;
61-
} else {
62-
basename = filename.substr(lastSlashIndex + 1);
63-
}
64-
65-
const comment = compilation.getPath(banner, {
66-
hash,
67-
chunk,
68-
filename,
69-
basename,
70-
query,
71-
});
72-
73-
return compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file]);
42+
for(const chunk of chunks) {
43+
if(options.entryOnly && !chunk.canBeInitial()) {
44+
continue;
45+
}
46+
47+
for(const file of chunk.files) {
48+
if(!matchObject(file)) {
49+
continue;
50+
}
51+
52+
let basename;
53+
let query = "";
54+
let filename = file;
55+
const hash = compilation.hash;
56+
const querySplit = filename.indexOf("?");
57+
58+
if(querySplit >= 0) {
59+
query = filename.substr(querySplit);
60+
filename = filename.substr(0, querySplit);
61+
}
62+
63+
const lastSlashIndex = filename.lastIndexOf("/");
64+
65+
if(lastSlashIndex === -1) {
66+
basename = filename;
67+
} else {
68+
basename = filename.substr(lastSlashIndex + 1);
69+
}
70+
71+
const comment = compilation.getPath(banner, {
72+
hash,
73+
chunk,
74+
filename,
75+
basename,
76+
query,
7477
});
75-
});
78+
79+
compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file]);
80+
}
81+
}
7682
});
7783
});
7884
}

lib/Chunk.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ const sortByIdentifier = (a, b) => {
2424
const getModulesIdent = set => {
2525
set.sort();
2626
let str = "";
27-
set.forEach(m => {
27+
for(const m of set) {
2828
str += m.identifier() + "#";
29-
});
29+
}
3030
return str;
3131
};
3232

@@ -246,7 +246,9 @@ class Chunk {
246246
hash.update(`${this.id} `);
247247
hash.update(this.ids ? this.ids.join(",") : "");
248248
hash.update(`${this.name || ""} `);
249-
this._modules.forEach(m => hash.update(m.hash));
249+
for(const m of this._modules) {
250+
hash.update(m.hash);
251+
}
250252
}
251253

252254
canBeIntegrated(otherChunk) {

lib/ChunkGroup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class ChunkGroup {
231231
parentChunkGroup._children.delete(this);
232232

233233
// cleanup "sub chunks"
234-
this._children.forEach(chunkGroup => {
234+
for(const chunkGroup of this._children) {
235235
/**
236236
* remove this chunk as "intermediary" and connect
237237
* it "sub chunks" and parents directly
@@ -240,7 +240,7 @@ class ChunkGroup {
240240
chunkGroup.addParent(parentChunkGroup);
241241
// add "sub chunk" to parent
242242
parentChunkGroup.addChild(chunkGroup);
243-
});
243+
}
244244
}
245245

246246
/**

lib/Compilation.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ class Compilation extends Tapable {
270270
cacheModule.disconnect();
271271
this._modules.set(identifier, cacheModule);
272272
this.modules.push(cacheModule);
273-
cacheModule.errors.forEach(err => this.errors.push(err));
274-
cacheModule.warnings.forEach(err => this.warnings.push(err));
273+
for(const err of cacheModule.errors) this.errors.push(err);
274+
for(const err of cacheModule.warnings) this.warnings.push(err);
275275
return {
276276
module: cacheModule,
277277
issuer: true,
@@ -323,7 +323,7 @@ class Compilation extends Tapable {
323323

324324
const callback = err => {
325325
this._buildingModules.delete(module);
326-
callbackList.forEach(cb => cb(err));
326+
for(const cb of callbackList) cb(err);
327327
};
328328

329329
this.hooks.buildModule.call(module);
@@ -691,7 +691,7 @@ class Compilation extends Tapable {
691691

692692
const callback = err => {
693693
this._rebuildingModules.delete(module);
694-
callbackList.forEach(cb => cb(err));
694+
for(const cb of callbackList) cb(err);
695695
};
696696

697697
this.hooks.rebuildModule.call(module);
@@ -737,7 +737,9 @@ class Compilation extends Tapable {
737737
this.namedChunkGroups.clear();
738738
this.additionalChunkAssets.length = 0;
739739
this.assets = {};
740-
this.modules.forEach(module => module.unseal());
740+
for(const module of this.modules) {
741+
module.unseal();
742+
}
741743
}
742744

743745
seal(callback) {
@@ -750,7 +752,7 @@ class Compilation extends Tapable {
750752

751753
this.nextFreeModuleIndex = 0;
752754
this.nextFreeModuleIndex2 = 0;
753-
this._preparedEntrypoints.forEach(preparedEntrypoint => {
755+
for(const preparedEntrypoint of this._preparedEntrypoints) {
754756
const module = preparedEntrypoint.module;
755757
const name = preparedEntrypoint.name;
756758
const chunk = this.addChunk(name);
@@ -769,7 +771,7 @@ class Compilation extends Tapable {
769771

770772
this.assignIndex(module);
771773
this.assignDepth(module);
772-
});
774+
}
773775
this.processDependenciesBlocksForChunkGroups(this.chunkGroups);
774776
this.sortModules(this.modules);
775777
this.hooks.optimize.call();
@@ -1513,11 +1515,11 @@ class Compilation extends Tapable {
15131515
addAllToSet(this.contextDependencies, module.buildInfo.contextDependencies);
15141516
}
15151517
}
1516-
this.errors.forEach(error => {
1518+
for(const error of this.errors) {
15171519
if(typeof error.missing === "object" && error.missing && error.missing[Symbol.iterator]) {
15181520
addAllToSet(this.missingDependencies, error.missing);
15191521
}
1520-
});
1522+
}
15211523
this.fileDependencies.sort();
15221524
this.contextDependencies.sort();
15231525
this.missingDependencies.sort();
@@ -1533,10 +1535,10 @@ class Compilation extends Tapable {
15331535
hash.update(outputOptions.hashSalt);
15341536
this.mainTemplate.updateHash(hash);
15351537
this.chunkTemplate.updateHash(hash);
1536-
Object.keys(this.moduleTemplates).sort().forEach(key => this.moduleTemplates[key].updateHash(hash));
1537-
this.children.forEach(child => hash.update(child.hash));
1538-
this.warnings.forEach(warning => hash.update(`${warning.message}`));
1539-
this.errors.forEach(error => hash.update(`${error.message}`));
1538+
for(const key of Object.keys(this.moduleTemplates).sort()) this.moduleTemplates[key].updateHash(hash);
1539+
for(const child of this.children) hash.update(child.hash);
1540+
for(const warning of this.warnings) hash.update(`${warning.message}`);
1541+
for(const error of this.errors) hash.update(`${error.message}`);
15401542
const modules = this.modules;
15411543
for(let i = 0; i < modules.length; i++) {
15421544
const module = modules[i];
@@ -1595,11 +1597,11 @@ class Compilation extends Tapable {
15951597
for(let i = 0; i < this.modules.length; i++) {
15961598
const module = this.modules[i];
15971599
if(module.buildInfo.assets) {
1598-
Object.keys(module.buildInfo.assets).forEach((assetName) => {
1600+
for(const assetName of Object.keys(module.buildInfo.assets)) {
15991601
const fileName = this.getPath(assetName);
16001602
this.assets[fileName] = module.buildInfo.assets[assetName];
16011603
this.hooks.moduleAsset.call(module, fileName);
1602-
});
1604+
}
16031605
}
16041606
}
16051607
}

lib/Compiler.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
"use strict";
66

7+
const asyncLib = require("async");
78
const path = require("path");
89
const util = require("util");
910
const Tapable = require("tapable").Tapable;
@@ -224,9 +225,9 @@ class Compiler extends Tapable {
224225
if(err) return callback(err);
225226

226227
this.parentCompilation.children.push(compilation);
227-
Object.keys(compilation.assets).forEach(name => {
228+
for(const name of Object.keys(compilation.assets)) {
228229
this.parentCompilation.assets[name] = compilation.assets[name];
229-
});
230+
}
230231

231232
const entries = Array.from(compilation.entrypoints.values(), ep => ep.chunks).reduce((array, chunks) => {
232233
return array.concat(chunks);
@@ -247,7 +248,7 @@ class Compiler extends Tapable {
247248
const emitFiles = (err) => {
248249
if(err) return callback(err);
249250

250-
require("async").forEach(Object.keys(compilation.assets), (file, callback) => {
251+
asyncLib.forEach(Object.keys(compilation.assets), (file, callback) => {
251252

252253
let targetFile = file;
253254
const queryStringIdx = targetFile.indexOf("?");
@@ -345,7 +346,7 @@ class Compiler extends Tapable {
345346
createChildCompiler(compilation, compilerName, compilerIndex, outputOptions, plugins) {
346347
const childCompiler = new Compiler(this.context);
347348
if(Array.isArray(plugins)) {
348-
plugins.forEach(plugin => plugin.apply(childCompiler));
349+
for(const plugin of plugins) plugin.apply(childCompiler);
349350
}
350351
for(const name in this.hooks) {
351352
if(!["make", "compile", "emit", "afterEmit", "invalid", "done", "thisCompilation"].includes(name)) {

lib/ContextModule.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ class ContextModule extends Module {
155155
}
156156

157157
// enhance dependencies with meta info
158-
dependencies.forEach(dep => {
158+
for(const dep of dependencies) {
159159
dep.loc = dep.userRequest;
160160
dep.request = this.options.addon + dep.request;
161-
});
161+
}
162162

163163
if(this.options.mode === "sync" || this.options.mode === "eager") {
164164

@@ -172,33 +172,36 @@ class ContextModule extends Module {
172172
// and add that block to this context
173173
if(dependencies.length > 0) {
174174
const block = new AsyncDependenciesBlock(this.options.chunkName, this);
175-
dependencies.forEach(dep => {
175+
for(const dep of dependencies) {
176176
block.addDependency(dep);
177-
});
177+
}
178178
this.addBlock(block);
179179
}
180180

181181
} else if(this.options.mode === "weak" || this.options.mode === "async-weak") {
182182

183183
// we mark all dependencies as weak
184-
dependencies.forEach(dep => dep.weak = true);
184+
for(const dep of dependencies) {
185+
dep.weak = true;
186+
}
185187
this.dependencies = dependencies;
186188

187189
} else if(this.options.mode === "lazy") {
188190
// if we are lazy create a new async dependency block per dependency
189191
// and add all blocks to this context
190-
dependencies.forEach((dep, idx) => {
192+
let index = 0;
193+
for(const dep of dependencies) {
191194
let chunkName = this.options.chunkName;
192195
if(chunkName) {
193196
if(!/\[(index|request)\]/.test(chunkName))
194197
chunkName += "[index]";
195-
chunkName = chunkName.replace(/\[index\]/g, idx);
198+
chunkName = chunkName.replace(/\[index\]/g, index++);
196199
chunkName = chunkName.replace(/\[request\]/g, Template.toPath(dep.userRequest));
197200
}
198201
const block = new AsyncDependenciesBlock(chunkName, dep.module, dep.loc, dep.userRequest);
199202
block.addDependency(dep);
200203
this.addBlock(block);
201-
});
204+
}
202205
} else {
203206
callback(new Error(`Unsupported mode "${this.options.mode}" in context`));
204207
return;

lib/ContextReplacementPlugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ class ContextReplacementPlugin {
5858
if(typeof newContentCallback === "function") {
5959
newContentCallback(result);
6060
} else {
61-
result.dependencies.forEach((d) => {
61+
for(const d of result.dependencies) {
6262
if(d.critical)
6363
d.critical = false;
64-
});
64+
}
6565
}
6666
}
6767
return result;
@@ -84,10 +84,10 @@ class ContextReplacementPlugin {
8484
result.resource = path.resolve(origResource, result.resource);
8585
}
8686
} else {
87-
result.dependencies.forEach((d) => {
87+
for(const d of result.dependencies) {
8888
if(d.critical)
8989
d.critical = false;
90-
});
90+
}
9191
}
9292
}
9393
return result;

lib/DependenciesBlockVariable.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ class DependenciesBlockVariable {
1717
updateHash(hash) {
1818
hash.update(this.name);
1919
hash.update(this.expression);
20-
this.dependencies.forEach(d => {
20+
for(const d of this.dependencies) {
2121
d.updateHash(hash);
22-
});
22+
}
2323
}
2424

2525
expressionSource(dependencyTemplates, runtimeTemplate) {
2626
const source = new ReplaceSource(new RawSource(this.expression));
27-
this.dependencies.forEach(dep => {
27+
for(const dep of this.dependencies) {
2828
const template = dependencyTemplates.get(dep.constructor);
2929
if(!template) throw new Error(`No template for dependency: ${dep.constructor.name}`);
3030
template.apply(dep, source, runtimeTemplate, dependencyTemplates);
31-
});
31+
}
3232
return source;
3333
}
3434

3535
disconnect() {
36-
this.dependencies.forEach(d => {
36+
for(const d of this.dependencies) {
3737
d.disconnect();
38-
});
38+
}
3939
}
4040

4141
hasDependencies(filter) {

lib/EntryOptionPlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ module.exports = class EntryOptionPlugin {
2121
if(typeof entry === "string" || Array.isArray(entry)) {
2222
itemToPlugin(context, entry, "main").apply(compiler);
2323
} else if(typeof entry === "object") {
24-
Object.keys(entry).forEach(name => itemToPlugin(context, entry[name], name).apply(compiler));
24+
for(const name of Object.keys(entry)) {
25+
itemToPlugin(context, entry[name], name).apply(compiler);
26+
}
2527
} else if(typeof entry === "function") {
2628
new DynamicEntryPlugin(context, entry).apply(compiler);
2729
}

0 commit comments

Comments
 (0)