Skip to content

Commit dc90e70

Browse files
authored
Merge pull request webpack#6353 from webpack/bugfix/chunk-is-initial
split Chunk.isInitial into isOnlyInitial and canBeInitial
2 parents 6bd1240 + ea60937 commit dc90e70

18 files changed

+131
-130
lines changed

lib/BannerPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class BannerPlugin {
3939
compiler.hooks.compilation.tap("BannerPlugin", (compilation) => {
4040
compilation.hooks.optimizeChunkAssets.tap("BannerPlugin", (chunks) => {
4141
chunks.forEach((chunk) => {
42-
if(options.entryOnly && !chunk.isInitial()) return;
42+
if(options.entryOnly && !chunk.canBeInitial()) return;
4343
chunk.files
4444
.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
4545
.forEach((file) => {

lib/Chunk.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ class Chunk {
6969
}
7070

7171
get initial() {
72-
throw new Error("Chunk.initial was removed. Use isInitial()");
72+
throw new Error("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
7373
}
7474

7575
set initial(data) {
76-
throw new Error("Chunk.initial was removed. Use isInitial()");
76+
throw new Error("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
7777
}
7878

7979
hasRuntime() {
@@ -84,14 +84,23 @@ class Chunk {
8484
return false;
8585
}
8686

87-
isInitial() {
87+
canBeInitial() {
8888
for(const chunkGroup of this._groups) {
89-
// We only need to check the first one
90-
return chunkGroup.isInitial();
89+
if(chunkGroup.isInitial())
90+
return true;
9191
}
9292
return false;
9393
}
9494

95+
isOnlyInitial() {
96+
if(this._groups.size <= 0) return false;
97+
for(const chunkGroup of this._groups) {
98+
if(!chunkGroup.isInitial())
99+
return false;
100+
}
101+
return true;
102+
}
103+
95104
hasEntryModule() {
96105
return !!this.entryModule;
97106
}
@@ -263,10 +272,10 @@ class Chunk {
263272
}
264273
return true;
265274
};
266-
if(this.isInitial() !== otherChunk.isInitial()) {
267-
if(this.isInitial()) {
275+
if(this.hasRuntime() !== otherChunk.hasRuntime()) {
276+
if(this.hasRuntime()) {
268277
return isAvailable(this, otherChunk);
269-
} else if(otherChunk.isInitial()) {
278+
} else if(otherChunk.hasRuntime()) {
270279
return isAvailable(otherChunk, this);
271280
} else {
272281
return false;
@@ -279,7 +288,7 @@ class Chunk {
279288

280289
addMultiplierAndOverhead(size, options) {
281290
const overhead = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
282-
const multiplicator = this.isInitial() ? (options.entryChunkMultiplicator || 10) : 1;
291+
const multiplicator = this.canBeInitial() ? (options.entryChunkMultiplicator || 10) : 1;
283292

284293
return size * multiplicator + overhead;
285294
}
@@ -317,22 +326,33 @@ class Chunk {
317326
this.sortModules();
318327
}
319328

320-
getChunkMaps(includeInitial, realHash) {
321-
const chunkHashMap = Object.create(null);
322-
const chunkNameMap = Object.create(null);
323-
329+
getAllAsyncChunks() {
330+
const initialChunks = new Set();
324331
const queue = new Set(this.groupsIterable);
325332
const chunks = new Set();
326333

327334
for(const chunkGroup of queue) {
328-
if(includeInitial || !chunkGroup.isInitial())
329-
for(const chunk of chunkGroup.chunks)
335+
for(const chunk of chunkGroup.chunks)
336+
initialChunks.add(chunk);
337+
}
338+
339+
for(const chunkGroup of queue) {
340+
for(const chunk of chunkGroup.chunks) {
341+
if(!initialChunks.has(chunk))
330342
chunks.add(chunk);
343+
}
331344
for(const child of chunkGroup.childrenIterable)
332345
queue.add(child);
333346
}
334347

335-
for(const chunk of chunks) {
348+
return chunks;
349+
}
350+
351+
getChunkMaps(realHash) {
352+
const chunkHashMap = Object.create(null);
353+
const chunkNameMap = Object.create(null);
354+
355+
for(const chunk of this.getAllAsyncChunks()) {
336356
chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
337357
if(chunk.name)
338358
chunkNameMap[chunk.id] = chunk.name;
@@ -344,22 +364,11 @@ class Chunk {
344364
};
345365
}
346366

347-
getChunkModuleMaps(includeInitial, filterFn) {
367+
getChunkModuleMaps(filterFn) {
348368
const chunkModuleIdMap = Object.create(null);
349369
const chunkModuleHashMap = Object.create(null);
350370

351-
const queue = new Set(this.groupsIterable);
352-
const chunks = new Set();
353-
354-
for(const chunkGroup of queue) {
355-
if(includeInitial || !chunkGroup.isInitial())
356-
for(const chunk of chunkGroup.chunks)
357-
chunks.add(chunk);
358-
for(const child of chunkGroup.childrenIterable)
359-
queue.add(child);
360-
}
361-
362-
for(const chunk of chunks) {
371+
for(const chunk of this.getAllAsyncChunks()) {
363372
let array;
364373
for(const module of chunk.modulesIterable) {
365374
if(filterFn(module)) {

lib/FlagInitialModulesAsUsedPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FlagInitialModulesAsUsedPlugin {
1313
compiler.hooks.compilation.tap("FlagInitialModulesAsUsedPlugin", (compilation) => {
1414
compilation.hooks.afterOptimizeChunks.tap("FlagInitialModulesAsUsedPlugin", (chunks) => {
1515
chunks.forEach((chunk) => {
16-
if(!chunk.isInitial()) {
16+
if(!chunk.isOnlyInitial()) {
1717
return;
1818
}
1919
for(const module of chunk.modulesIterable) {

lib/LibManifestPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class LibManifestPlugin {
1515
apply(compiler) {
1616
compiler.hooks.emit.tapAsync("LibManifestPlugin", (compilation, callback) => {
1717
asyncLib.forEach(compilation.chunks, (chunk, callback) => {
18-
if(!chunk.isInitial()) {
18+
if(!chunk.isOnlyInitial()) {
1919
callback();
2020
return;
2121
}

lib/Stats.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class Stats {
193193
message: e
194194
};
195195
if(e.chunk) {
196-
text += `chunk ${e.chunk.name || e.chunk.id}${e.chunk.hasRuntime() ? " [entry]" : e.chunk.isInitial() ? " [initial]" : ""}\n`;
196+
text += `chunk ${e.chunk.name || e.chunk.id}${e.chunk.hasRuntime() ? " [entry]" : e.chunk.canBeInitial() ? " [initial]" : ""}\n`;
197197
}
198198
if(e.file) {
199199
text += `${e.file}\n`;
@@ -431,7 +431,7 @@ class Stats {
431431
const obj = {
432432
id: chunk.id,
433433
rendered: chunk.rendered,
434-
initial: chunk.isInitial(),
434+
initial: chunk.canBeInitial(),
435435
entry: chunk.hasRuntime(),
436436
recorded: chunk.recorded,
437437
reason: chunk.chunkReason,

lib/TemplatedPathPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ class TemplatedPathPlugin {
104104
const outputOptions = mainTemplate.outputOptions;
105105
const chunkFilename = outputOptions.chunkFilename || outputOptions.filename;
106106
if(REGEXP_CHUNKHASH_FOR_TEST.test(chunkFilename))
107-
hash.update(JSON.stringify(chunk.getChunkMaps(false, true).hash));
107+
hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
108108
if(REGEXP_NAME_FOR_TEST.test(chunkFilename))
109-
hash.update(JSON.stringify(chunk.getChunkMaps(false, true).name));
109+
hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
110110
});
111111
});
112112
}

lib/node/ReadFileCompileWasmMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ReadFileCompileWasmMainTemplatePlugin {
1919
});
2020
mainTemplate.hooks.requireEnsure.tap("ReadFileCompileWasmMainTemplatePlugin", (source, chunk, hash) => {
2121
const webassemblyModuleFilename = mainTemplate.outputOptions.webassemblyModuleFilename;
22-
const chunkModuleMaps = chunk.getChunkModuleMaps(false, m => m.type.startsWith("webassembly"));
22+
const chunkModuleMaps = chunk.getChunkModuleMaps(m => m.type.startsWith("webassembly"));
2323
if(Object.keys(chunkModuleMaps.id).length === 0) return source;
2424
const wasmModuleSrcPath = mainTemplate.getAssetPath(JSON.stringify(webassemblyModuleFilename), {
2525
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,

lib/optimize/AggressiveMergingPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class AggressiveMergingPlugin {
2020
compilation.hooks.optimizeChunksAdvanced.tap("AggressiveMergingPlugin", (chunks) => {
2121
let combinations = [];
2222
chunks.forEach((a, idx) => {
23-
if(a.isInitial()) return;
23+
if(a.canBeInitial()) return;
2424
for(let i = 0; i < idx; i++) {
2525
const b = chunks[i];
26-
if(b.isInitial()) continue;
26+
if(b.canBeInitial()) continue;
2727
combinations.push({
2828
a,
2929
b,

lib/optimize/MergeDuplicateChunksPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MergeDuplicateChunksPlugin {
4747
// when we found duplicates
4848
if(possibleDuplicates !== undefined && possibleDuplicates.size > 0) {
4949
for(const otherChunk of possibleDuplicates) {
50-
if(otherChunk.isInitial() !== chunk.isInitial()) continue;
50+
if(otherChunk.hasRuntime() !== chunk.hasRuntime()) continue;
5151
// merge them
5252
if(chunk.integrate(otherChunk, "duplicate"))
5353
chunks.splice(chunks.indexOf(otherChunk), 1);

lib/optimize/OccurrenceOrderPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class OccurrenceOrderPlugin {
2424
let initial = 0;
2525
let entry = 0;
2626
m.forEachChunk(c => {
27-
if(c.isInitial()) initial++;
27+
if(c.canBeInitial()) initial++;
2828
if(c.entryModule === m) entry++;
2929
});
3030
initialChunkChunkMap.set(m, initial);

0 commit comments

Comments
 (0)