Skip to content

Commit d4811b0

Browse files
committed
Use for-of loops instead of forEach
1 parent dc90e70 commit d4811b0

Some content is hidden

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

55 files changed

+443
-377
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/CachePlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class CachePlugin {
5858
});
5959
}, err => {
6060
if(err) return callback(err);
61-
Object.keys(fileTs).forEach(key => {
61+
for(const key of Object.keys(fileTs)) {
6262
fileTs[key] += this.FS_ACCURENCY;
63-
});
63+
}
6464
callback();
6565
});
6666
});

lib/Chunk.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const getFrozenArray = set => Object.freeze(Array.from(set));
2626
const getModulesIdent = set => {
2727
set.sort();
2828
let str = "";
29-
set.forEach(m => {
29+
for(const m of set) {
3030
str += m.identifier() + "#";
31-
});
31+
}
3232
return str;
3333
};
3434

@@ -258,7 +258,9 @@ class Chunk {
258258
hash.update(`${this.id} `);
259259
hash.update(this.ids ? this.ids.join(",") : "");
260260
hash.update(`${this.name || ""} `);
261-
this._modules.forEach(m => hash.update(m.hash));
261+
for(const m of this._modules) {
262+
hash.update(m.hash);
263+
}
262264
}
263265

264266
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: 20 additions & 16 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();
@@ -1280,7 +1282,9 @@ class Compilation extends Tapable {
12801282
return;
12811283
}
12821284
if(d.module.removeReason(module, d)) {
1283-
d.module.forEachChunk(chunk => this.patchChunksAfterReasonRemoval(d.module, chunk));
1285+
for(const chunk of d.module.chunksIterable) {
1286+
this.patchChunksAfterReasonRemoval(d.module, chunk);
1287+
}
12841288
}
12851289
};
12861290

@@ -1511,11 +1515,11 @@ class Compilation extends Tapable {
15111515
addAllToSet(this.contextDependencies, module.buildInfo.contextDependencies);
15121516
}
15131517
}
1514-
this.errors.forEach(error => {
1518+
for(const error of this.errors) {
15151519
if(typeof error.missing === "object" && error.missing && error.missing[Symbol.iterator]) {
15161520
addAllToSet(this.missingDependencies, error.missing);
15171521
}
1518-
});
1522+
}
15191523
this.fileDependencies.sort();
15201524
this.contextDependencies.sort();
15211525
this.missingDependencies.sort();
@@ -1531,10 +1535,10 @@ class Compilation extends Tapable {
15311535
hash.update(outputOptions.hashSalt);
15321536
this.mainTemplate.updateHash(hash);
15331537
this.chunkTemplate.updateHash(hash);
1534-
Object.keys(this.moduleTemplates).sort().forEach(key => this.moduleTemplates[key].updateHash(hash));
1535-
this.children.forEach(child => hash.update(child.hash));
1536-
this.warnings.forEach(warning => hash.update(`${warning.message}`));
1537-
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}`);
15381542
const modules = this.modules;
15391543
for(let i = 0; i < modules.length; i++) {
15401544
const module = modules[i];
@@ -1593,11 +1597,11 @@ class Compilation extends Tapable {
15931597
for(let i = 0; i < this.modules.length; i++) {
15941598
const module = this.modules[i];
15951599
if(module.buildInfo.assets) {
1596-
Object.keys(module.buildInfo.assets).forEach((assetName) => {
1600+
for(const assetName of Object.keys(module.buildInfo.assets)) {
15971601
const fileName = this.getPath(assetName);
15981602
this.assets[fileName] = module.buildInfo.assets[assetName];
15991603
this.hooks.moduleAsset.call(module, fileName);
1600-
});
1604+
}
16011605
}
16021606
}
16031607
}

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;
@@ -246,9 +247,9 @@ class Compiler extends Tapable {
246247
if(err) return callback(err);
247248

248249
this.parentCompilation.children.push(compilation);
249-
Object.keys(compilation.assets).forEach(name => {
250+
for(const name of Object.keys(compilation.assets)) {
250251
this.parentCompilation.assets[name] = compilation.assets[name];
251-
});
252+
}
252253

253254
const entries = Array.from(compilation.entrypoints.values(), ep => ep.chunks).reduce((array, chunks) => {
254255
return array.concat(chunks);
@@ -269,7 +270,7 @@ class Compiler extends Tapable {
269270
const emitFiles = (err) => {
270271
if(err) return callback(err);
271272

272-
require("async").forEach(Object.keys(compilation.assets), (file, callback) => {
273+
asyncLib.forEach(Object.keys(compilation.assets), (file, callback) => {
273274

274275
let targetFile = file;
275276
const queryStringIdx = targetFile.indexOf("?");
@@ -367,7 +368,7 @@ class Compiler extends Tapable {
367368
createChildCompiler(compilation, compilerName, compilerIndex, outputOptions, plugins) {
368369
const childCompiler = new Compiler(this.context);
369370
if(Array.isArray(plugins)) {
370-
plugins.forEach(plugin => plugin.apply(childCompiler));
371+
for(const plugin of plugins) plugin.apply(childCompiler);
371372
}
372373
for(const name in this.hooks) {
373374
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;

0 commit comments

Comments
 (0)