From e5dfc8b3d7a0f8720b5410f623009fd3c2de78e9 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 19 Oct 2018 12:06:15 +0200 Subject: [PATCH] make LimitChunkCountPlugin stable --- lib/Chunk.js | 13 ++++++-- lib/Entrypoint.js | 10 ++++++ lib/optimize/LimitChunkCountPlugin.js | 16 +++++++--- .../__snapshots__/StatsTestCases.test.js.snap | 32 +++++++++---------- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/lib/Chunk.js b/lib/Chunk.js index 13a4356b6af..80dc170b729 100644 --- a/lib/Chunk.js +++ b/lib/Chunk.js @@ -182,12 +182,13 @@ class Chunk { */ hasRuntime() { for (const chunkGroup of this._groups) { - // We only need to check the first one - return ( + if ( chunkGroup.isInitial() && chunkGroup instanceof Entrypoint && chunkGroup.getRuntimeChunk() === this - ); + ) { + return true; + } } return false; } @@ -394,6 +395,10 @@ class Chunk { } otherChunk._modules.clear(); + if (otherChunk.entryModule) { + this.entryModule = otherChunk.entryModule; + } + for (const chunkGroup of otherChunk._groups) { chunkGroup.replaceChunk(otherChunk, this); this.addGroup(chunkGroup); @@ -409,6 +414,8 @@ class Chunk { } else { this.name = this.name < otherChunk.name ? this.name : otherChunk.name; } + } else if (otherChunk.name) { + this.name = otherChunk.name; } return true; diff --git a/lib/Entrypoint.js b/lib/Entrypoint.js index c67ff44ed7c..c1389a4f7d1 100644 --- a/lib/Entrypoint.js +++ b/lib/Entrypoint.js @@ -49,6 +49,16 @@ class Entrypoint extends ChunkGroup { getRuntimeChunk() { return this.runtimeChunk || this.chunks[0]; } + + /** + * @param {Chunk} oldChunk chunk to be replaced + * @param {Chunk} newChunk New chunkt that will be replaced + * @returns {boolean} rerturns true for + */ + replaceChunk(oldChunk, newChunk) { + if (this.runtimeChunk === oldChunk) this.runtimeChunk = newChunk; + return super.replaceChunk(oldChunk, newChunk); + } } module.exports = Entrypoint; diff --git a/lib/optimize/LimitChunkCountPlugin.js b/lib/optimize/LimitChunkCountPlugin.js index a271191bf7f..0f8bd6dcbe5 100644 --- a/lib/optimize/LimitChunkCountPlugin.js +++ b/lib/optimize/LimitChunkCountPlugin.js @@ -30,11 +30,13 @@ class LimitChunkCountPlugin { if (maxChunks < 1) return; if (chunks.length <= maxChunks) return; - const sortedExtendedPairCombinations = chunks + const orderedChunks = chunks.slice().sort((a, b) => a.compareTo(b)); + + const sortedExtendedPairCombinations = orderedChunks .reduce((combinations, a, idx) => { // create combination pairs for (let i = 0; i < idx; i++) { - const b = chunks[i]; + const b = orderedChunks[i]; combinations.push([b, a]); } return combinations; @@ -54,9 +56,13 @@ class LimitChunkCountPlugin { .sort((a, b) => { // sadly javascript does an inplace sort here // sort them by size - const diff = b[0] - a[0]; - if (diff !== 0) return diff; - return a[1] - b[1]; + const diff1 = b[0] - a[0]; + if (diff1 !== 0) return diff1; + const diff2 = a[1] - b[1]; + if (diff2 !== 0) return diff2; + const diff3 = a[2].compareTo(b[2]); + if (diff3 !== 0) return diff3; + return a[3].compareTo(b[3]); }); const pair = sortedExtendedPairCombinations[0]; diff --git a/test/__snapshots__/StatsTestCases.test.js.snap b/test/__snapshots__/StatsTestCases.test.js.snap index 17bab9f0cab..7628a8d8bf6 100644 --- a/test/__snapshots__/StatsTestCases.test.js.snap +++ b/test/__snapshots__/StatsTestCases.test.js.snap @@ -1164,7 +1164,7 @@ Child `; exports[`StatsTestCases should print correct stats for limit-chunk-count-plugin 1`] = ` -"Hash: eb610b429c869eba72876747d3a05d57aba4ed011c34e7c25ef408f41e6c4d70e3d24b67ba636024 +"Hash: eb610b429c869eba72876747d3a05d57aba4ed0100a352bc82c330d5a88b516679988e145181f3e2 Child 1 chunks: Hash: eb610b429c869eba7287 Time: Xms @@ -1196,42 +1196,42 @@ Child 2 chunks: chunk {1} bundle.js (main) 73 bytes >{0}< [entry] [rendered] [0] ./index.js 73 bytes {1} [built] Child 3 chunks: - Hash: 1c34e7c25ef408f41e6c + Hash: 00a352bc82c330d5a88b Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT Asset Size Chunks Chunk Names - 0.bundle.js 494 bytes 0 [emitted] + 0.bundle.js 485 bytes 0 [emitted] 1.bundle.js 232 bytes 1 [emitted] bundle.js 8.28 KiB 2 [emitted] main Entrypoint main = bundle.js - chunk {0} 0.bundle.js 74 bytes <{0}> <{2}> >{0}< >{1}< [rendered] + chunk {0} 0.bundle.js 74 bytes <{2}> >{1}< [rendered] [1] ./a.js 22 bytes {0} [built] + [2] ./b.js 22 bytes {0} [built] [3] ./c.js 30 bytes {0} [built] - [4] ./d.js 22 bytes {0} [built] - chunk {1} 1.bundle.js 44 bytes <{0}> <{2}> [rendered] - [2] ./b.js 22 bytes {1} [built] + chunk {1} 1.bundle.js 44 bytes <{0}> [rendered] + [4] ./d.js 22 bytes {1} [built] [5] ./e.js 22 bytes {1} [built] - chunk {2} bundle.js (main) 73 bytes >{0}< >{1}< [entry] [rendered] + chunk {2} bundle.js (main) 73 bytes >{0}< [entry] [rendered] [0] ./index.js 73 bytes {2} [built] Child 4 chunks: - Hash: 4d70e3d24b67ba636024 + Hash: 516679988e145181f3e2 Time: Xms Built at: Thu Jan 01 1970 00:00:00 GMT Asset Size Chunks Chunk Names - 0.bundle.js 254 bytes 0 [emitted] + 0.bundle.js 236 bytes 0 [emitted] 1.bundle.js 232 bytes 1 [emitted] 3.bundle.js 323 bytes 3 [emitted] bundle.js 8.28 KiB 2 [emitted] main Entrypoint main = bundle.js - chunk {0} 0.bundle.js 44 bytes <{2}> <{3}> [rendered] + chunk {0} 0.bundle.js 44 bytes <{2}> [rendered] [1] ./a.js 22 bytes {0} [built] - [4] ./d.js 22 bytes {0} [built] - chunk {1} 1.bundle.js 44 bytes <{2}> <{3}> [rendered] - [2] ./b.js 22 bytes {1} [built] + [2] ./b.js 22 bytes {0} [built] + chunk {1} 1.bundle.js 44 bytes <{3}> [rendered] + [4] ./d.js 22 bytes {1} [built] [5] ./e.js 22 bytes {1} [built] - chunk {2} bundle.js (main) 73 bytes >{0}< >{1}< >{3}< [entry] [rendered] + chunk {2} bundle.js (main) 73 bytes >{0}< >{3}< [entry] [rendered] [0] ./index.js 73 bytes {2} [built] - chunk {3} 3.bundle.js 30 bytes <{2}> >{0}< >{1}< [rendered] + chunk {3} 3.bundle.js 30 bytes <{2}> >{1}< [rendered] [3] ./c.js 30 bytes {3} [built]" `;