diff --git a/lib/optimize/CommonsChunkPlugin.js b/lib/optimize/CommonsChunkPlugin.js index 9585ca7b5..51ff53807 100644 --- a/lib/optimize/CommonsChunkPlugin.js +++ b/lib/optimize/CommonsChunkPlugin.js @@ -115,7 +115,18 @@ You can however specify the name of the async chunk by passing the desired strin // override the "commonChunk" with the newly created async one and use it as commonChunk from now on let asyncChunk; if(this.async) { - asyncChunk = this.createAsyncChunk(compilation, this.async, targetChunk); + // If async chunk is one of the affected chunks, just use it + asyncChunk = affectedChunks.filter(c => c.name === this.async)[0]; + // Elsewise create a new one + if(!asyncChunk) { + asyncChunk = this.createAsyncChunk( + compilation, + targetChunks.length <= 1 || typeof this.async !== "string" ? this.async : + targetChunk.name ? `${this.async}-${targetChunk.name}` : + true, + targetChunk + ); + } targetChunk = asyncChunk; } @@ -306,7 +317,7 @@ Take a look at the "name"/"names" or async/children option.`); extractModulesAndReturnAffectedChunks(reallyUsedModules, usedChunks) { return reallyUsedModules.reduce((affectedChunksSet, module) => { - for(let chunk of usedChunks) { + for(const chunk of usedChunks) { // removeChunk returns true if the chunk was contained and succesfully removed // false if the module did not have a connection to the chunk in question if(module.removeChunk(chunk)) { @@ -318,28 +329,29 @@ Take a look at the "name"/"names" or async/children option.`); } addExtractedModulesToTargetChunk(chunk, modules) { - for(let module of modules) { + for(const module of modules) { chunk.addModule(module); module.addChunk(chunk); } } makeTargetChunkParentOfAffectedChunks(usedChunks, commonChunk) { - for(let chunk of usedChunks) { + for(const chunk of usedChunks) { // set commonChunk as new sole parent chunk.parents = [commonChunk]; // add chunk to commonChunk commonChunk.addChunk(chunk); - for(let entrypoint of chunk.entrypoints) { + for(const entrypoint of chunk.entrypoints) { entrypoint.insertChunk(commonChunk, chunk); } } } moveExtractedChunkBlocksToTargetChunk(chunks, targetChunk) { - for(let chunk of chunks) { - for(let block of chunk.blocks) { + for(const chunk of chunks) { + if(chunk === targetChunk) continue; + for(const block of chunk.blocks) { if(block.chunks.indexOf(targetChunk) === -1) { block.chunks.unshift(targetChunk); } @@ -350,8 +362,8 @@ Take a look at the "name"/"names" or async/children option.`); extractOriginsOfChunksWithExtractedModules(chunks) { const origins = []; - for(let chunk of chunks) { - for(let origin of chunk.origins) { + for(const chunk of chunks) { + for(const origin of chunk.origins) { const newOrigin = Object.create(origin); newOrigin.reasons = (origin.reasons || []).concat("async commons"); origins.push(newOrigin);