Skip to content

Commit 7de03f2

Browse files
authored
Merge pull request webpack#6367 from webpack/deprecate_forEach_map
Deprecate forEach* and map* methods
2 parents e82677c + 77bb4c0 commit 7de03f2

File tree

9 files changed

+44
-28
lines changed

9 files changed

+44
-28
lines changed

lib/Chunk.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,6 @@ class Chunk {
133133
return this._modules;
134134
}
135135

136-
// TODO remove and replace calls with for of loop
137-
forEachModule(fn) {
138-
this._modules.forEach(fn);
139-
}
140-
141-
// TODO remove and replace calls with Array.from
142-
mapModules(fn) {
143-
return Array.from(this._modules, fn);
144-
}
145-
146136
addGroup(chunkGroup) {
147137
if(this._groups.has(chunkGroup))
148138
return false;
@@ -417,11 +407,27 @@ class Chunk {
417407
}
418408
}
419409

410+
// TODO remove in webpack 5
411+
Object.defineProperty(Chunk.prototype, "forEachModule", {
412+
configurable: false,
413+
value: util.deprecate(function(fn) {
414+
this._modules.forEach(fn);
415+
}, "Chunk.forEachModule: Use for(const module of chunk.modulesIterable) instead")
416+
});
417+
418+
// TODO remove in webpack 5
419+
Object.defineProperty(Chunk.prototype, "mapModules", {
420+
configurable: false,
421+
value: util.deprecate(function(fn) {
422+
return Array.from(this._modules, fn);
423+
}, "Chunk.mapModules: Use Array.from(chunk.modulesIterable, fn) instead")
424+
});
425+
420426
Object.defineProperty(Chunk.prototype, "modules", {
421427
configurable: false,
422428
get: util.deprecate(function() {
423429
return this._modules.getFromCache(getFrozenArray);
424-
}, "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead."),
430+
}, "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/containsModule/modulesIterable instead."),
425431
set: util.deprecate(function(value) {
426432
this.setModules(value);
427433
}, "Chunk.modules is deprecated. Use Chunk.addModule/removeModule instead.")

lib/Compilation.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,9 @@ class Compilation extends Tapable {
12801280
return;
12811281
}
12821282
if(d.module.removeReason(module, d)) {
1283-
d.module.forEachChunk(chunk => this.patchChunksAfterReasonRemoval(d.module, chunk));
1283+
for(const chunk of d.module.chunksIterable) {
1284+
this.patchChunksAfterReasonRemoval(d.module, chunk);
1285+
}
12841286
}
12851287
};
12861288

lib/HotModuleReplacementPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module.exports = class HotModuleReplacementPlugin {
6363
});
6464
records.chunkModuleIds = {};
6565
compilation.chunks.forEach(chunk => {
66-
records.chunkModuleIds[chunk.id] = chunk.mapModules(m => m.id);
66+
records.chunkModuleIds[chunk.id] = Array.from(chunk.modulesIterable, m => m.id);
6767
});
6868
});
6969
let initialPass = false;

lib/LibManifestPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LibManifestPlugin {
3030
const manifest = {
3131
name,
3232
type: this.options.type,
33-
content: chunk.mapModules(module => {
33+
content: Array.from(chunk.modulesIterable, module => {
3434
if(module.libIdent) {
3535
const ident = module.libIdent({
3636
context: this.options.context || compiler.options.context

lib/Module.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,6 @@ class Module extends DependenciesBlock {
148148
return this.reasons.length > 0 && this.reasons.every(r => r.dependency && r.dependency.optional);
149149
}
150150

151-
forEachChunk(fn) {
152-
this._chunks.forEach(fn);
153-
}
154-
155-
mapChunks(fn) {
156-
return Array.from(this._chunks, fn);
157-
}
158-
159151
getChunks() {
160152
return Array.from(this._chunks);
161153
}
@@ -302,6 +294,22 @@ class Module extends DependenciesBlock {
302294
}
303295
}
304296

297+
// TODO remove in webpack 5
298+
Object.defineProperty(Module.prototype, "forEachChunk", {
299+
configurable: false,
300+
value: util.deprecate(function(fn) {
301+
this._chunks.forEach(fn);
302+
}, "Module.forEachChunk: Use for(const chunk of module.chunksIterable) instead")
303+
});
304+
305+
// TODO remove in webpack 5
306+
Object.defineProperty(Module.prototype, "mapChunks", {
307+
configurable: false,
308+
value: util.deprecate(function(fn) {
309+
return Array.from(this._chunks, fn);
310+
}, "Module.mapChunks: Use Array.from(module.chunksIterable, fn) instead")
311+
});
312+
305313
Object.defineProperty(Module.prototype, "entry", {
306314
configurable: false,
307315
get() {
@@ -316,7 +324,7 @@ Object.defineProperty(Module.prototype, "chunks", {
316324
configurable: false,
317325
get: util.deprecate(function() {
318326
return this._chunks.getFromCache(getFrozenArray);
319-
}, "Module.chunks: Use Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk instead"),
327+
}, "Module.chunks: Use Module.chunksIterable/getNumberOfChunks/isInChunk/addChunk/removeChunk instead"),
320328
set() {
321329
throw new Error("Readonly. Use Module.addChunk/removeChunk to modify chunks.");
322330
}

lib/ModuleFilenameHelpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ ModuleFilenameHelpers.createFooter = (module, requestShortener) => {
125125
"// WEBPACK FOOTER",
126126
`// ${module.readableIdentifier(requestShortener)}`,
127127
`// module id = ${module.id}`,
128-
`// module chunks = ${module.mapChunks(c => c.id).join(" ")}`
128+
`// module chunks = ${Array.from(module.chunksIterable, c => c.id).join(" ")}`
129129
].join("\n");
130130
}
131131
};

lib/Stats.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class Stats {
340340
built: !!module.built,
341341
optional: module.optional,
342342
prefetched: module.prefetched,
343-
chunks: module.mapChunks(chunk => chunk.id),
343+
chunks: Array.from(module.chunksIterable, chunk => chunk.id),
344344
assets: Object.keys(module.assets || {}),
345345
issuer: module.issuer && module.issuer.identifier(),
346346
issuerId: module.issuer && module.issuer.id,

lib/optimize/OccurrenceOrderPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class OccurrenceOrderPlugin {
2323
modules.forEach(m => {
2424
let initial = 0;
2525
let entry = 0;
26-
m.forEachChunk(c => {
26+
for(const c of m.chunksIterable) {
2727
if(c.canBeInitial()) initial++;
2828
if(c.entryModule === m) entry++;
29-
});
29+
}
3030
initialChunkChunkMap.set(m, initial);
3131
entryCountMap.set(m, entry);
3232
});

test/statsCases/named-chunks-plugin-async/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
if(chunk.name) {
1414
return chunk.name;
1515
}
16-
const chunkModulesToName = (chunk) => chunk.mapModules((mod) => {
16+
const chunkModulesToName = (chunk) => Array.from(chunk.modulesIterable, (mod) => {
1717
const rs = new RequestShortener(mod.context);
1818
return rs.shorten(mod.request).replace(/[./\\]/g, "_");
1919
}).join("-");

0 commit comments

Comments
 (0)