Skip to content

Commit 443b62f

Browse files
committed
add special case for async without name
1 parent e610b65 commit 443b62f

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

lib/optimize/CommonsChunkPlugin.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,18 @@ You can however specify the name of the async chunk by passing the desired strin
115115
// override the "commonChunk" with the newly created async one and use it as commonChunk from now on
116116
let asyncChunk;
117117
if(this.async) {
118-
asyncChunk = this.createAsyncChunk(compilation, this.async, targetChunk);
118+
// If async chunk is one of the affected chunks, just use it
119+
asyncChunk = affectedChunks.filter(c => c.name === this.async)[0];
120+
// Elsewise create a new one
121+
if(!asyncChunk) {
122+
asyncChunk = this.createAsyncChunk(
123+
compilation,
124+
targetChunks.length <= 1 || typeof this.async !== "string" ? this.async :
125+
targetChunk.name ? `${this.async}-${targetChunk.name}` :
126+
true,
127+
targetChunk
128+
);
129+
}
119130
targetChunk = asyncChunk;
120131
}
121132

@@ -306,7 +317,7 @@ Take a look at the "name"/"names" or async/children option.`);
306317

307318
extractModulesAndReturnAffectedChunks(reallyUsedModules, usedChunks) {
308319
return reallyUsedModules.reduce((affectedChunksSet, module) => {
309-
for(let chunk of usedChunks) {
320+
for(const chunk of usedChunks) {
310321
// removeChunk returns true if the chunk was contained and succesfully removed
311322
// false if the module did not have a connection to the chunk in question
312323
if(module.removeChunk(chunk)) {
@@ -318,28 +329,29 @@ Take a look at the "name"/"names" or async/children option.`);
318329
}
319330

320331
addExtractedModulesToTargetChunk(chunk, modules) {
321-
for(let module of modules) {
332+
for(const module of modules) {
322333
chunk.addModule(module);
323334
module.addChunk(chunk);
324335
}
325336
}
326337

327338
makeTargetChunkParentOfAffectedChunks(usedChunks, commonChunk) {
328-
for(let chunk of usedChunks) {
339+
for(const chunk of usedChunks) {
329340
// set commonChunk as new sole parent
330341
chunk.parents = [commonChunk];
331342
// add chunk to commonChunk
332343
commonChunk.addChunk(chunk);
333344

334-
for(let entrypoint of chunk.entrypoints) {
345+
for(const entrypoint of chunk.entrypoints) {
335346
entrypoint.insertChunk(commonChunk, chunk);
336347
}
337348
}
338349
}
339350

340351
moveExtractedChunkBlocksToTargetChunk(chunks, targetChunk) {
341-
for(let chunk of chunks) {
342-
for(let block of chunk.blocks) {
352+
for(const chunk of chunks) {
353+
if(chunk === targetChunk) continue;
354+
for(const block of chunk.blocks) {
343355
if(block.chunks.indexOf(targetChunk) === -1) {
344356
block.chunks.unshift(targetChunk);
345357
}
@@ -350,8 +362,8 @@ Take a look at the "name"/"names" or async/children option.`);
350362

351363
extractOriginsOfChunksWithExtractedModules(chunks) {
352364
const origins = [];
353-
for(let chunk of chunks) {
354-
for(let origin of chunk.origins) {
365+
for(const chunk of chunks) {
366+
for(const origin of chunk.origins) {
355367
const newOrigin = Object.create(origin);
356368
newOrigin.reasons = (origin.reasons || []).concat("async commons");
357369
origins.push(newOrigin);

0 commit comments

Comments
 (0)