10000 add splitChunks.minSizeReduction · webpack/webpack@3d3c65f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d3c65f

Browse files
committed
add splitChunks.minSizeReduction
1 parent 4a23389 commit 3d3c65f

File tree

16 files changed

+224
-19
lines changed

16 files changed

+224
-19
lines changed

declarations/WebpackOptions.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,10 @@ export interface OptimizationSplitChunksOptions {
17511751
* Minimal size for the created chunk.
17521752
*/
17531753
minSize?: OptimizationSplitChunksSizes;
1754+
/**
1755+
* Minimum size reduction due to the created chunk.
1756+
*/
1757+
minSizeReduction?: OptimizationSplitChunksSizes;
17541758
};
17551759
/**
17561760
* Sets the template for the filename for created chunks.
@@ -1797,6 +1801,10 @@ export interface OptimizationSplitChunksOptions {
17971801
* Minimal size for the created chunks.
17981802
*/
17991803
minSize?: OptimizationSplitChunksSizes;
1804+
/**
1805+
* Minimum size reduction due to the created chunk.
1806+
*/
1807+
minSizeReduction?: OptimizationSplitChunksSizes;
18001808
/**
18011809
* Give chunks created a name (chunks with equal name are merged).
18021810
*/
@@ -1877,6 +1885,10 @@ export interface OptimizationSplitChunksCacheGroup {
18771885
* Minimal size for the created chunk.
18781886
*/
18791887
minSize?: OptimizationSplitChunksSizes;
1888+
/**
1889+
* Minimum size reduction due to the created chunk.
1890+
*/
1891+
minSizeReduction?: OptimizationSplitChunksSizes;
18801892
/**
18811893
* Give chunks for this cache group a name (chunks with equal name are merged).
18821894
*/

lib/optimize/SplitChunksPlugin.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const MinMaxSizeWarning = require("./MinMaxSizeWarning");
5959
* @property {ChunkFilterFunction=} chunksFilter
6060
* @property {boolean=} enforce
6161
* @property {SplitChunksSizes} minSize
62+
* @property {SplitChunksSizes} minSizeReduction
6263
* @property {SplitChunksSizes} minRemainingSize
6364
* @property {SplitChunksSizes} enforceSizeThreshold
6465
* @property {SplitChunksSizes} maxAsyncSize
@@ -80,6 +81,7 @@ const MinMaxSizeWarning = require("./MinMaxSizeWarning");
8081
* @property {GetName=} getName
8182
* @property {ChunkFilterFunction=} chunksFilter
8283
* @property {SplitChunksSizes} minSize
84+
* @property {SplitChunksSizes} minSizeReduction
8385
* @property {SplitChunksSizes} minRemainingSize
8486
* @property {SplitChunksSizes} enforceSizeThreshold
8587
* @property {SplitChunksSizes} maxAsyncSize
@@ -132,6 +134,7 @@ const MinMaxSizeWarning = require("./MinMaxSizeWarning");
132134
* @property {ChunkFilterFunction} chunksFilter
133135
* @property {string[]} defaultSizeTypes
134136
* @property {SplitChunksSizes} minSize
137+
* @property {SplitChunksSizes} minSizeReduction
135138
* @property {SplitChunksSizes} minRemainingSize
136139
* @property {SplitChunksSizes} enforceSizeThreshold
137140
* @property {SplitChunksSizes} maxInitialSize
@@ -336,6 +339,21 @@ const checkMinSize = (sizes, minSize) => {
336339
return true;
337340
};
338341

342+
/**
343+
* @param {SplitChunksSizes} sizes the sizes
344+
* @param {SplitChunksSizes} minSizeReduction the min sizes
345+
* @param {number} chunkCount number of chunks
346+
* @returns {boolean} true if there are sizes and all existing sizes are at least `minSizeReduction`
347+
*/
348+
const checkMinSizeReduction = (sizes, minSizeReduction, chunkCount) => {
349+
for (const key of Object.keys(minSizeReduction)) {
350+
const size = sizes[key];
351+
if (size === undefined || size === 0) continue;
352+
if (size * chunkCount < minSizeReduction[key]) return false;
353+
}
354+
return true;
355+
};
356+
339357
/**
340358
* @param {SplitChunksSizes} sizes the sizes
341359
* @param {SplitChunksSizes} minSize the min sizes
@@ -548,6 +566,10 @@ const checkModuleLayer = (test, module) => {
548566
*/
549567
const createCacheGroupSource = (options, key, defaultSizeTypes) => {
550568
const minSize = normalizeSizes(options.minSize, defaultSizeTypes);
569+
const minSizeReduction = normalizeSizes(
570+
options.minSizeReduction,
571+
defaultSizeTypes
572+
);
551573
const maxSize = normalizeSizes(options.maxSize, defaultSizeTypes);
552574
return {
553575
key,
@@ -556,6 +578,7 @@ const createCacheGroupSource = (options, key, defaultSizeTypes) => {
556578
chunksFilter: normalizeChunksFilter(options.chunks),
557579
enforce: options.enforce,
558580
minSize,
581+
minSizeReduction,
559582
minRemainingSize: mergeSizes(
560583
normalizeSizes(options.minRemainingSize, defaultSizeTypes),
561584
minSize
@@ -594,13 +617,18 @@ module.exports = class SplitChunksPlugin {
594617
];
595618
const fallbackCacheGroup = options.fallbackCacheGroup || {};
596619
const minSize = normalizeSizes(options.minSize, defaultSizeTypes);
620+
const minSizeReduction = normalizeSizes(
621+
options.minSizeReduction,
622+
defaultSizeTypes
623+
);
597624
const maxSize = normalizeSizes(options.maxSize, defaultSizeTypes);
598625

599626
/** @type {SplitChunksOptions} */
600627
this.options = {
601628
chunksFilter: normalizeChunksFilter(options.chunks || "all"),
602629
defaultSizeTypes,
603630
minSize,
631+
minSizeReduction,
604632
minRemainingSize: mergeSizes(
605633
normalizeSizes(options.minRemainingSize, defaultSizeTypes),
606634
minSize
@@ -668,6 +696,10 @@ module.exports = class SplitChunksPlugin {
668696
cacheGroupSource.minSize,
669697
cacheGroupSource.enforce ? undefined : this.options.minSize
670698
);
699+
const minSizeReduction = mergeSizes(
700+
cacheGroupSource.minSizeReduction,
701+
cacheGroupSource.enforce ? undefined : this.options.minSizeReduction
702+
);
671703
const minRemainingSize = mergeSizes(
672704
cacheGroupSource.minRemainingSize,
673705
cacheGroupSource.enforce ? undefined : this.options.minRemainingSize
@@ -681,6 +713,7 @@ module.exports = class SplitChunksPlugin {
681713
priority: cacheGroupSource.priority || 0,
682714
chunksFilter: cacheGroupSource.chunksFilter || this.options.chunksFilter,
683715
minSize,
716+
minSizeReduction,
684717
minRemainingSize,
685718
enforceSizeThreshold,
686719
maxAsyncSize: mergeSizes(
@@ -1244,6 +1277,14 @@ module.exports = class SplitChunksPlugin {
12441277
for (const [key, info] of chunksInfoMap) {
12451278
if (removeMinSizeViolatingModules(info)) {
12461279
chunksInfoMap.delete(key);
1280+
} else if (
1281+
!checkMinSizeReduction(
1282+
info.sizes,
1283+
info.cacheGroup.minSizeReduction,
1284+
info.chunks.size
1285+
)
1286+
) {
1287+
chunksInfoMap.delete(key);
12471288
}
12481289
}
12491290

@@ -1531,7 +1572,14 @@ module.exports = class SplitChunksPlugin {
15311572
chunksInfoMap.delete(key);
15321573
continue;
15331574
}
1534-
if (removeMinSizeViolatingModules(info)) {
1575+
if (
1576+
removeMinSizeViolatingModules(info) ||
1577+
!checkMinSizeReduction(
1578+
info.sizes,
1579+
info.cacheGroup.minSizeReduction,
1580+
info.chunks.size
1581+
)
1582+
) {
15351583
chunksInfoMap.delete(key);
15361584
continue;
15371585
}

2851 ‎schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,6 +2458,14 @@
24582458
}
24592459
]
24602460
},
2461+
"minSizeReduction": {
2462+
"description": "Minimum size reduction due to the created chunk.",
2463+
"oneOf": [
2464+
{
2465+
"$ref": "#/definitions/OptimizationSplitChunksSizes"
2466+
}
2467+
]
2468+
},
24612469
"name": {
24622470
"description": "Give chunks for this cache group a name (chunks with equal name are merged).",
24632471
"anyOf": [
@@ -2654,6 +2662,14 @@
26542662
"$ref": "#/definitions/OptimizationSplitChunksSizes"
26552663
}
26562664
]
2665+
},
2666+
"minSizeReduction": {
2667+
"description": "Minimum size reduction due to the created chunk.",
2668+
"oneOf": [
2669+
{
2670+
"$ref": "#/definitions/OptimizationSplitChunksSizes"
2671+
}
2672+
]
26572673
}
26582674
}
26592675
},
@@ -2730,6 +2746,14 @@
27302746
}
27312747
]
27322748
},
2749+
"minSizeReduction": {
2750+
"description": "Minimum size reduction due to the created chunk.",
2751+
"oneOf": [
2752+
{
2753+
"$ref": "#/definitions/OptimizationSplitChunksSizes"
2754+
}
2755+
]
2756+
},
27332757
"name": {
27342758
"description": "Give chunks created a name (chunks with equal name are merged).",
27352759
"anyOf": [

test/Validation.test.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -453,18 +453,18 @@ describe("Validation", () => {
453453
},
454454
msg =>
455455
expect(msg).toMatchInlineSnapshot(`
456-
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
457-
- configuration.optimization.splitChunks.cacheGroups should not be object { test, … }.
458-
-> Using the cacheGroup shorthand syntax with a cache group named 'test' is a potential config error
459-
Did you intent to define a cache group with a test instead?
460-
cacheGroups: {
461-
<name>: {
462-
test: ...
463-
}
464-
}.
465-
object { <key>: false | RegExp | string | function | object { automaticNameDelimiter?, chunks?, enforce?, enforceSizeThreshold?, filename?, idHint?, layer?, maxAsyncRequests?, maxAsyncSize?, maxInitialRequests?, maxInitialSize?, maxSize?, minChunks?, minRemainingSize?, minSize?, name?, priority?, reuseExistingChunk?, test?, type?, usedExports? } }
466-
-> Assign modules to a cache group (modules from different cache groups are tried to keep in separate chunks, default categories: 'default', 'defaultVendors')."
467-
`)
456+
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
457+
- configuration.optimization.splitChunks.cacheGroups should not be object { test, … }.
458+
-> Using the cacheGroup shorthand syntax with a cache group named 'test' is a potential config error
459+
Did you intent to define a cache group with a test instead?
460+
cacheGroups: {
461+
<name>: {
462+
test: ...
463+
}
464+
}.
465+
object { <key>: false | RegExp | string | function | object { automaticNameDelimiter?, chunks?, enforce?, enforceSizeThreshold?, filename?, idHint?, layer?, maxAsyncRequests?, maxAsyncSize?, maxInitialRequests?, maxInitialSize?, maxSize?, minChunks?, minRemainingSize?, minSize?, minSizeReduction?, name?, priority?, reuseExistingChunk?, test?, type?, usedExports? } }
466+
-> Assign modules to a cache group (modules from different cache groups are tried to keep in separate chunks, default categories: 'default', 'defaultVendors')."
467+
`)
468468
);
469469

470470
createTestCase(
@@ -661,11 +661,11 @@ describe("Validation", () => {
661661
},
662662
msg =>
663663
expect(msg).toMatchInlineSnapshot(`
664-
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
665-
- configuration.optimization.splitChunks has an unknown property 'automaticNamePrefix'. These properties are valid:
666-
object { automaticNameDelimiter?, cacheGroups?, chunks?, defaultSizeTypes?, enforceSizeThreshold?, fallbackCacheGroup?, filename?, hidePathInfo?, maxAsyncRequests?, maxAsyncSize?, maxInitialRequests?, maxInitialSize?, maxSize?, minChunks?, minRemainingSize?, minSize?, name?, usedExports? }
667-
-> Options object for splitting chunks into smaller chunks."
668-
`)
664+
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
665+
- configuration.optimization.splitChunks has an unknown property 'automaticNamePrefix'. These properties are valid:
666+
object { automaticNameDelimiter?, cacheGroups?, chunks?, defaultSizeTypes?, enforceSizeThreshold?, fallbackCacheGroup?, filename?, hidePathInfo?, maxAsyncRequests?, maxAsyncSize?, maxInitialRequests?, maxInitialSize?, maxSize?, minChunks?, minRemainingSize?, minSize?, minSizeReduction?, name?, usedExports? }
667+
-> Options object for splitting chunks into smaller chunks."
668+
`)
669669
);
670670
});
671671
});

test/__snapshots__/Cli.test.js.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,6 +4446,19 @@ Object {
44464446
"multiple": false,
44474447
"simpleType": "number",
44484448
},
4449+
"optimization-split-chunks-fallback-cache-group-min-size-reduction": Object {
4450+
"configs": Array [
4451+
Object {
4452+
"description": "Size of the javascript part of the chunk.",
4453+
"multiple": false,
4454+
"path": "optimization.splitChunks.fallbackCacheGroup.minSizeReduction",
4455+
"type": "number",
4456+
},
4457+
],
4458+
"description": "Size of the javascript part of the chunk.",
4459+
"multiple": false,
4460+
"simpleType": "number",
4461+
},
44494462
"optimization-split-chunks-filename": Object {
44504463
"configs": Array [
44514464
Object {
@@ -4576,6 +4589,19 @@ Object {
45764589
"multiple": false,
45774590
"simpleType": "number",
45784591
},
4592+
"optimization-split-chunks-min-size-reduction": Object {
4593+
"configs": Array [
4594+
Object {
4595+
"description": "Size of the javascript part of the chunk.",
4596+
"multiple": false,
4597+
"path": "optimization.splitChunks.minSizeReduction",
4598+
"type": "number",
4599+
},
4600+
],
4601+
"description": "Size of the javascript part of the chunk.",
4602+
"multiple": false,
4603+
"simpleType": "number",
4604+
},
45794605
"optimization-split-chunks-name": Object {
45804606
"configs": Array [
45814607
Object {

test/__snapshots__/StatsTestCases.basictest.js.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4292,6 +4292,37 @@ enforce-min-size:
42924292
enforce-min-size (webpack x.x.x) compiled successfully"
42934293
`;
42944294
4295+
exports[`StatsTestCases should print correct stats for split-chunks-min-size-reduction 1`] = `
4296+
"Entrypoint main 11.5 KiB = default/main.js
4297+
chunk (runtime: main) default/async-d.js (async-d) 50 bytes <{179}> ={821}= [rendered]
4298+
> ./d ./index.js 4:0-47
4299+
./d.js 50 bytes [built] [code generated]
4300+
chunk (runtime: main) default/main.js (main) 245 bytes (javascript) 6.68 KiB (runtime) >{31}< >{334}< >{383}< >{449}< >{794}< >{821}< [entry] [rendered]
4301+
> ./ main
4302+
runtime modules 6.68 KiB 9 modules
4303+
./index.js 245 bytes [built] [code generated]
4304+
chunk (runtime: main) default/async-b.js (async-b) 176 bytes <{179}> [rendered]
4305+
> ./b ./index.js 2:0-47
4306+
./b.js 50 bytes [built] [code generated]
4307+
./node_modules/shared.js?1 126 bytes [dependent] [built] [code generated]
4308+
chunk (runtime: main) default/async-c.js (async-c) 50 bytes <{179}> ={821}= [rendered]
4309+
> ./c ./index.js 3:0-47
4310+
./c.js 50 bytes [built] [code generated]
4311+
chunk (runtime: main) default/async-e.js (async-e) 50 bytes <{179}> ={821}= [rendered]
4312+
> ./e ./index.js 5:0-47
4313+
./e.js 50 bytes [built] [code generated]
4314+
chunk (runtime: main) default/async-a.js (async-a) 176 bytes <{179}> [rendered]
4315+
> ./a ./index.js 1:0-47
4316+
./a.js 50 bytes [built] [code generated]
4317+
./node_modules/shared.js?1 126 bytes [dependent] [built] [code generated]
4318+
chunk (runtime: main) default/821.js (id hint: vendors) 126 bytes <{179}> ={31}= ={383}= ={449}= [rendered] split chunk (cache group: defaultVendors)
4319+
> ./c ./index.js 3:0-47
4320+
> ./d ./index.js 4:0-47
4321+
> ./e ./index.js 5:0-47
4322+
./node_modules/shared.js?2 126 bytes [built] [code generated]
4323+
webpack x.x.x compiled successfully"
4324+
`;
4325+
42954326
exports[`StatsTestCases should print correct stats for split-chunks-prefer-bigger-splits 1`] = `
42964327
"Entrypoint main 11.2 KiB = default/main.js
42974328
chunk (runtime: main) default/118.js 150 bytes <{179}> ={334}= ={383}= [rendered] split chunk (cache group: default)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import s from "shared?1";
2+
export default "a" + s;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import s from "shared?1";
2+
export default "b" + s;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import s from "shared?2";
2+
export default "c" + s;

0 commit comments

Comments
 (0)
0