Skip to content

Commit

Permalink
make LimitChunkCountPlugin stable
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Oct 19, 2018
1 parent 872e3b8 commit e5dfc8b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
13 changes: 10 additions & 3 deletions lib/Chunk.js
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions lib/Entrypoint.js
Expand Up @@ -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;
16 changes: 11 additions & 5 deletions lib/optimize/LimitChunkCountPlugin.js
Expand Up @@ -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;
Expand All @@ -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];
Expand Down
32 changes: 16 additions & 16 deletions test/__snapshots__/StatsTestCases.test.js.snap
Expand Up @@ -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
Expand Down Expand Up @@ -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]"
`;

Expand Down

0 comments on commit e5dfc8b

Please sign in to comment.