Skip to content

Commit

Permalink
Use for-of loops instead of forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
ooflorent committed Jan 23, 2018
1 parent dc90e70 commit d4811b0
Show file tree
Hide file tree
Showing 55 changed files with 443 additions and 377 deletions.
74 changes: 40 additions & 34 deletions lib/BannerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,50 @@ class BannerPlugin {
apply(compiler) {
const options = this.options;
const banner = this.banner;
const matchObject = ModuleFilenameHelpers.matchObject.bind(undefined, options);

compiler.hooks.compilation.tap("BannerPlugin", (compilation) => {
compilation.hooks.optimizeChunkAssets.tap("BannerPlugin", (chunks) => {
chunks.forEach((chunk) => {
if(options.entryOnly && !chunk.canBeInitial()) return;
chunk.files
.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
.forEach((file) => {
let basename;
let query = "";
let filename = file;
const hash = compilation.hash;
const querySplit = filename.indexOf("?");

if(querySplit >= 0) {
query = filename.substr(querySplit);
filename = filename.substr(0, querySplit);
}

const lastSlashIndex = filename.lastIndexOf("/");

if(lastSlashIndex === -1) {
basename = filename;
} else {
basename = filename.substr(lastSlashIndex + 1);
}

const comment = compilation.getPath(banner, {
hash,
chunk,
filename,
basename,
query,
});

return compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file]);
for(const chunk of chunks) {
if(options.entryOnly && !chunk.canBeInitial()) {
continue;
}

for(const file of chunk.files) {
if(!matchObject(file)) {
continue;
}

let basename;
let query = "";
let filename = file;
const hash = compilation.hash;
const querySplit = filename.indexOf("?");

if(querySplit >= 0) {
query = filename.substr(querySplit);
filename = filename.substr(0, querySplit);
}

const lastSlashIndex = filename.lastIndexOf("/");

if(lastSlashIndex === -1) {
basename = filename;
} else {
basename = filename.substr(lastSlashIndex + 1);
}

const comment = compilation.getPath(banner, {
hash,
chunk,
filename,
basename,
query,
});
});

compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file]);
}
}
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions lib/CachePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class CachePlugin {
});
}, err => {
if(err) return callback(err);
Object.keys(fileTs).forEach(key => {
for(const key of Object.keys(fileTs)) {
fileTs[key] += this.FS_ACCURENCY;
});
}
callback();
});
});
Expand Down
8 changes: 5 additions & 3 deletions lib/Chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const getFrozenArray = set => Object.freeze(Array.from(set));
const getModulesIdent = set => {
set.sort();
let str = "";
set.forEach(m => {
for(const m of set) {
str += m.identifier() + "#";
});
}
return str;
};

Expand Down Expand Up @@ -258,7 +258,9 @@ class Chunk {
hash.update(`${this.id} `);
hash.update(this.ids ? this.ids.join(",") : "");
hash.update(`${this.name || ""} `);
this._modules.forEach(m => hash.update(m.hash));
for(const m of this._modules) {
hash.update(m.hash);
}
}

canBeIntegrated(otherChunk) {
Expand Down
4 changes: 2 additions & 2 deletions lib/ChunkGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class ChunkGroup {
parentChunkGroup._children.delete(this);

// cleanup "sub chunks"
this._children.forEach(chunkGroup => {
for(const chunkGroup of this._children) {
/**
* remove this chunk as "intermediary" and connect
* it "sub chunks" and parents directly
Expand All @@ -240,7 +240,7 @@ class ChunkGroup {
chunkGroup.addParent(parentChunkGroup);
// add "sub chunk" to parent
parentChunkGroup.addChild(chunkGroup);
});
}
}

/**
Expand Down
36 changes: 20 additions & 16 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ class Compilation extends Tapable {
cacheModule.disconnect();
this._modules.set(identifier, cacheModule);
this.modules.push(cacheModule);
cacheModule.errors.forEach(err => this.errors.push(err));
cacheModule.warnings.forEach(err => this.warnings.push(err));
for(const err of cacheModule.errors) this.errors.push(err);
for(const err of cacheModule.warnings) this.warnings.push(err);
return {
module: cacheModule,
issuer: true,
Expand Down Expand Up @@ -323,7 +323,7 @@ class Compilation extends Tapable {

const callback = err => {
this._buildingModules.delete(module);
callbackList.forEach(cb => cb(err));
for(const cb of callbackList) cb(err);
};

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

const callback = err => {
this._rebuildingModules.delete(module);
callbackList.forEach(cb => cb(err));
for(const cb of callbackList) cb(err);
};

this.hooks.rebuildModule.call(module);
Expand Down Expand Up @@ -737,7 +737,9 @@ class Compilation extends Tapable {
this.namedChunkGroups.clear();
this.additionalChunkAssets.length = 0;
this.assets = {};
this.modules.forEach(module => module.unseal());
for(const module of this.modules) {
module.unseal();
}
}

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

this.nextFreeModuleIndex = 0;
this.nextFreeModuleIndex2 = 0;
this._preparedEntrypoints.forEach(preparedEntrypoint => {
for(const preparedEntrypoint of this._preparedEntrypoints) {
const module = preparedEntrypoint.module;
const name = preparedEntrypoint.name;
const chunk = this.addChunk(name);
Expand All @@ -769,7 +771,7 @@ class Compilation extends Tapable {

this.assignIndex(module);
this.assignDepth(module);
});
}
this.processDependenciesBlocksForChunkGroups(this.chunkGroups);
this.sortModules(this.modules);
this.hooks.optimize.call();
Expand Down Expand Up @@ -1280,7 +1282,9 @@ class Compilation extends Tapable {
return;
}
if(d.module.removeReason(module, d)) {
d.module.forEachChunk(chunk => this.patchChunksAfterReasonRemoval(d.module, chunk));
for(const chunk of d.module.chunksIterable) {
this.patchChunksAfterReasonRemoval(d.module, chunk);
}
}
};

Expand Down Expand Up @@ -1511,11 +1515,11 @@ class Compilation extends Tapable {
addAllToSet(this.contextDependencies, module.buildInfo.contextDependencies);
}
}
this.errors.forEach(error => {
for(const error of this.errors) {
if(typeof error.missing === "object" && error.missing && error.missing[Symbol.iterator]) {
addAllToSet(this.missingDependencies, error.missing);
}
});
}
this.fileDependencies.sort();
this.contextDependencies.sort();
this.missingDependencies.sort();
Expand All @@ -1531,10 +1535,10 @@ class Compilation extends Tapable {
hash.update(outputOptions.hashSalt);
this.mainTemplate.updateHash(hash);
this.chunkTemplate.updateHash(hash);
Object.keys(this.moduleTemplates).sort().forEach(key => this.moduleTemplates[key].updateHash(hash));
this.children.forEach(child => hash.update(child.hash));
this.warnings.forEach(warning => hash.update(`${warning.message}`));
this.errors.forEach(error => hash.update(`${error.message}`));
for(const key of Object.keys(this.moduleTemplates).sort()) this.moduleTemplates[key].updateHash(hash);
for(const child of this.children) hash.update(child.hash);
for(const warning of this.warnings) hash.update(`${warning.message}`);
for(const error of this.errors) hash.update(`${error.message}`);
const modules = this.modules;
for(let i = 0; i < modules.length; i++) {
const module = modules[i];
Expand Down Expand Up @@ -1593,11 +1597,11 @@ class Compilation extends Tapable {
for(let i = 0; i < this.modules.length; i++) {
const module = this.modules[i];
if(module.buildInfo.assets) {
Object.keys(module.buildInfo.assets).forEach((assetName) => {
for(const assetName of Object.keys(module.buildInfo.assets)) {
const fileName = this.getPath(assetName);
this.assets[fileName] = module.buildInfo.assets[assetName];
this.hooks.moduleAsset.call(module, fileName);
});
}
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions lib/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
"use strict";

const asyncLib = require("async");
const path = require("path");
const util = require("util");
const Tapable = require("tapable").Tapable;
Expand Down Expand Up @@ -246,9 +247,9 @@ class Compiler extends Tapable {
if(err) return callback(err);

this.parentCompilation.children.push(compilation);
Object.keys(compilation.assets).forEach(name => {
for(const name of Object.keys(compilation.assets)) {
this.parentCompilation.assets[name] = compilation.assets[name];
});
}

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

require("async").forEach(Object.keys(compilation.assets), (file, callback) => {
asyncLib.forEach(Object.keys(compilation.assets), (file, callback) => {

let targetFile = file;
const queryStringIdx = targetFile.indexOf("?");
Expand Down Expand Up @@ -367,7 +368,7 @@ class Compiler extends Tapable {
createChildCompiler(compilation, compilerName, compilerIndex, outputOptions, plugins) {
const childCompiler = new Compiler(this.context);
if(Array.isArray(plugins)) {
plugins.forEach(plugin => plugin.apply(childCompiler));
for(const plugin of plugins) plugin.apply(childCompiler);
}
for(const name in this.hooks) {
if(!["make", "compile", "emit", "afterEmit", "invalid", "done", "thisCompilation"].includes(name)) {
Expand Down
19 changes: 11 additions & 8 deletions lib/ContextModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ class ContextModule extends Module {
}

// enhance dependencies with meta info
dependencies.forEach(dep => {
for(const dep of dependencies) {
dep.loc = dep.userRequest;
dep.request = this.options.addon + dep.request;
});
}

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

Expand All @@ -172,33 +172,36 @@ class ContextModule extends Module {
// and add that block to this context
if(dependencies.length > 0) {
const block = new AsyncDependenciesBlock(this.options.chunkName, this);
dependencies.forEach(dep => {
for(const dep of dependencies) {
block.addDependency(dep);
});
}
this.addBlock(block);
}

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

// we mark all dependencies as weak
dependencies.forEach(dep => dep.weak = true);
for(const dep of dependencies) {
dep.weak = true;
}
this.dependencies = dependencies;

} else if(this.options.mode === "lazy") {
// if we are lazy create a new async dependency block per dependency
// and add all blocks to this context
dependencies.forEach((dep, idx) => {
let index = 0;
for(const dep of dependencies) {
let chunkName = this.options.chunkName;
if(chunkName) {
if(!/\[(index|request)\]/.test(chunkName))
chunkName += "[index]";
chunkName = chunkName.replace(/\[index\]/g, idx);
chunkName = chunkName.replace(/\[index\]/g, index++);
chunkName = chunkName.replace(/\[request\]/g, Template.toPath(dep.userRequest));
}
const block = new AsyncDependenciesBlock(chunkName, dep.module, dep.loc, dep.userRequest);
block.addDependency(dep);
this.addBlock(block);
});
}
} else {
callback(new Error(`Unsupported mode "${this.options.mode}" in context`));
return;
Expand Down
8 changes: 4 additions & 4 deletions lib/ContextReplacementPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ class ContextReplacementPlugin {
if(typeof newContentCallback === "function") {
newContentCallback(result);
} else {
result.dependencies.forEach((d) => {
for(const d of result.dependencies) {
if(d.critical)
d.critical = false;
});
}
}
}
return result;
Expand All @@ -84,10 +84,10 @@ class ContextReplacementPlugin {
result.resource = path.resolve(origResource, result.resource);
}
} else {
result.dependencies.forEach((d) => {
for(const d of result.dependencies) {
if(d.critical)
d.critical = false;
});
}
}
}
return result;
Expand Down
Loading

0 comments on commit d4811b0

Please sign in to comment.