Skip to content

Commit 8e7022b

Browse files
committed
refactor MinChunkSizePlugin to be more functional
1 parent 79a36a6 commit 8e7022b

1 file changed

Lines changed: 23 additions & 22 deletions

File tree

lib/optimize/MinChunkSizePlugin.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,44 @@ class MinChunkSizePlugin {
1717
const minChunkSize = options.minChunkSize;
1818
compiler.plugin("compilation", (compilation) => {
1919
compilation.plugin("optimize-chunks-advanced", (chunks) => {
20-
21-
let combinations = [];
22-
chunks.forEach((a, idx) => {
23-
for(let i = 0; i < idx; i++) {
24-
const b = chunks[i];
25-
combinations.push([b, a]);
26-
}
27-
});
28-
2920
const equalOptions = {
3021
chunkOverhead: 1,
3122
entryChunkMultiplicator: 1
3223
};
33-
combinations = combinations.filter((pair) => {
34-
return pair[0].size(equalOptions) < minChunkSize || pair[1].size(equalOptions) < minChunkSize;
35-
});
3624

37-
combinations.forEach((pair) => {
25+
const sortedSizeFilteredExtendedPairCombinations = chunks.reduce((combinations, a, idx) => {
26+
// create combination pairs
27+
for(let i = 0; i < idx; i++) {
28+
const b = chunks[i];
29+
combinations.push([b, a]);
30+
}
31+
return combinations;
32+
}, []).filter((pair) => {
33+
// check if one of the chunks sizes is smaller than the minChunkSize
34+
const p0SmallerThanMinChunkSize = pair[0].size(equalOptions) < minChunkSize;
35+
const p1mallerThanMinChunkSize = pair[1].size(equalOptions) < minChunkSize;
36+
return p0SmallerThanMinChunkSize || p1mallerThanMinChunkSize;
37+
}).map((pair) => {
38+
// extend combination pairs with size and integrated size
3839
const a = pair[0].size(options);
3940
const b = pair[1].size(options);
4041
const ab = pair[0].integratedSize(pair[1], options);
4142
pair.unshift(a + b - ab, ab);
42-
});
43-
44-
combinations = combinations.filter((pair) => {
43+
return [a + b - ab, ab, pair[0], pair[1]];
44+
}).filter((pair) => {
45+
// filter pairs that do not have an integratedSize
46+
// meaning they can NOT be integrated!
4547
return pair[1] !== false;
46-
});
47-
48-
if(combinations.length === 0) return;
49-
50-
combinations.sort((a, b) => {
48+
}).sort((a, b) => { // sadly javascript does an inplace sort here
49+
// sort by size
5150
const diff = b[0] - a[0];
5251
if(diff !== 0) return diff;
5352
return a[1] - b[1];
5453
});
5554

56-
const pair = combinations[0];
55+
if(sortedSizeFilteredExtendedPairCombinations.length === 0) return;
56+
57+
const pair = sortedSizeFilteredExtendedPairCombinations[0];
5758

5859
pair[2].integrate(pair[3], "min-size");
5960
chunks.splice(chunks.indexOf(pair[3]), 1);

0 commit comments

Comments
 (0)