Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate forEach* and map* methods #6367

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 17 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,27 @@ class Chunk {
}
}

// TODO remove in webpack 5
Object.defineProperty(Chunk.prototype, "forEachModule", {
configurable: false,
value: util.deprecate(function(fn) {
this._modules.forEach(fn);
}, "Chunk.forEachModule: Use for(const module of chunk.modulesIterable) instead")
});

// TODO remove in webpack 5
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
26 changes: 17 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,22 @@ class Module extends DependenciesBlock {
}
}

// TODO remove in webpack 5
Object.defineProperty(Module.prototype, "forEachChunk", {
configurable: false,
value: util.deprecate(function(fn) {
this._chunks.forEach(fn);
}, "Module.forEachChunk: Use for(const chunk of module.chunksIterable) instead")
});

// TODO remove in webpack 5
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 +324,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