Skip to content

Commit

Permalink
Deprecate forEach* and map* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ooflorent committed Jan 23, 2018
1 parent e8c2596 commit 5d8086d
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 28 deletions.
26 changes: 15 additions & 11 deletions lib/Chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,6 @@ class Chunk {
return this._modules;
}

// TODO remove and replace calls with for of loop
forEachModule(fn) {
this._modules.forEach(fn);
}

// TODO remove and replace calls with Array.from
mapModules(fn) {
return Array.from(this._modules, fn);
}

addGroup(chunkGroup) {
if(this._groups.has(chunkGroup))
return false;
Expand Down Expand Up @@ -417,11 +407,25 @@ class Chunk {
}
}

Object.defineProperty(Chunk.prototype, "forEachModule", {
configurable: false,
value: util.deprecate(function(fn) {
this._modules.forEach(fn);
}, "Chunk.forEachModule: Use chunk.modulesIterable.forEach(fn) instead")
});

Object.defineProperty(Chunk.prototype, "mapModules", {
configurable: false,
value: util.deprecate(function(fn) {
return Array.from(this._modules, fn);
}, "Chunk.mapModules: Use Array.from(chunk.modulesIterable, fn) instead")
});

Object.defineProperty(Chunk.prototype, "modules", {
configurable: false,
get: util.deprecate(function() {
return this._modules.getFromCache(getFrozenArray);
}, "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead."),
}, "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/containsModule/modulesIterable instead."),
set: util.deprecate(function(value) {
this.setModules(value);
}, "Chunk.modules is deprecated. Use Chunk.addModule/removeModule instead.")
Expand Down
4 changes: 3 additions & 1 deletion lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,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
2 changes: 1 addition & 1 deletion lib/HotModuleReplacementPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = class HotModuleReplacementPlugin {
});
records.chunkModuleIds = {};
compilation.chunks.forEach(chunk => {
records.chunkModuleIds[chunk.id] = chunk.mapModules(m => m.id);
records.chunkModuleIds[chunk.id] = Array.from(chunk.modulesIterable, m => m.id);
});
});
let initialPass = false;
Expand Down
2 changes: 1 addition & 1 deletion lib/LibManifestPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LibManifestPlugin {
const manifest = {
name,
type: this.options.type,
content: chunk.mapModules(module => {
content: Array.from(chunk.modulesIterable, module => {
if(module.libIdent) {
const ident = module.libIdent({
context: this.options.context || compiler.options.context
Expand Down
24 changes: 15 additions & 9 deletions lib/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,6 @@ class Module extends DependenciesBlock {
return this.reasons.length > 0 && this.reasons.every(r => r.dependency && r.dependency.optional);
}

forEachChunk(fn) {
this._chunks.forEach(fn);
}

mapChunks(fn) {
return Array.from(this._chunks, fn);
}

getChunks() {
return Array.from(this._chunks);
}
Expand Down Expand Up @@ -302,6 +294,20 @@ class Module extends DependenciesBlock {
}
}

Object.defineProperty(Module.prototype, "forEachChunk", {
configurable: false,
value: util.deprecate(function(fn) {
this._chunks.forEach(fn);
}, "Module.forEachChunk: Use module.chunksIterable.forEach(fn) instead")
});

Object.defineProperty(Module.prototype, "mapChunks", {
configurable: false,
value: util.deprecate(function(fn) {
return Array.from(this._chunks, fn);
}, "Module.mapChunks: Use Array.from(module.chunksIterable, fn) instead")
});

Object.defineProperty(Module.prototype, "entry", {
configurable: false,
get() {
Expand All @@ -316,7 +322,7 @@ Object.defineProperty(Module.prototype, "chunks", {
configurable: false,
get: util.deprecate(function() {
return this._chunks.getFromCache(getFrozenArray);
}, "Module.chunks: Use Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk instead"),
}, "Module.chunks: Use Module.chunksIterable/getNumberOfChunks/isInChunk/addChunk/removeChunk instead"),
set() {
throw new Error("Readonly. Use Module.addChunk/removeChunk to modify chunks.");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ModuleFilenameHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ ModuleFilenameHelpers.createFooter = (module, requestShortener) => {
"// WEBPACK FOOTER",
`// ${module.readableIdentifier(requestShortener)}`,
`// module id = ${module.id}`,
`// module chunks = ${module.mapChunks(c => c.id).join(" ")}`
`// module chunks = ${Array.from(module.chunksIterable, c => c.id).join(" ")}`
].join("\n");
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class Stats {
built: !!module.built,
optional: module.optional,
prefetched: module.prefetched,
chunks: module.mapChunks(chunk => chunk.id),
chunks: Array.from(module.chunksIterable, chunk => chunk.id),
assets: Object.keys(module.assets || {}),
issuer: module.issuer && module.issuer.identifier(),
issuerId: module.issuer && module.issuer.id,
Expand Down
4 changes: 2 additions & 2 deletions lib/optimize/OccurrenceOrderPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class OccurrenceOrderPlugin {
modules.forEach(m => {
let initial = 0;
let entry = 0;
m.forEachChunk(c => {
for(const c of m.chunksIterable) {
if(c.canBeInitial()) initial++;
if(c.entryModule === m) entry++;
});
}
initialChunkChunkMap.set(m, initial);
entryCountMap.set(m, entry);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
if(chunk.name) {
return chunk.name;
}
const chunkModulesToName = (chunk) => chunk.mapModules((mod) => {
const chunkModulesToName = (chunk) => Array.from(chunk.modulesIterable, (mod) => {
const rs = new RequestShortener(mod.context);
return rs.shorten(mod.request).replace(/[./\\]/g, "_");
}).join("-");
Expand Down

0 comments on commit 5d8086d

Please sign in to comment.