Skip to content

Commit 3366421

Browse files
authored
Merge pull request webpack#7663 from webpack/feature/automatic-name-prefix
automaticNamePrefix and name merging bugfix
2 parents 8891bd5 + 0c2ea9d commit 3366421

File tree

15 files changed

+131
-15
lines changed

15 files changed

+131
-15
lines changed

lib/WebpackOptionsDefaulter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,13 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
241241
this.set("optimization.splitChunks.name", true);
242242
this.set("optimization.splitChunks.cacheGroups", {});
243243
this.set("optimization.splitChunks.cacheGroups.default", {
244+
automaticNamePrefix: "",
244245
reuseExistingChunk: true,
245246
minChunks: 2,
246247
priority: -20
247248
});
248249
this.set("optimization.splitChunks.cacheGroups.vendors", {
250+
automaticNamePrefix: "vendors",
249251
test: /[\\/]node_modules[\\/]/,
250252
priority: -10
251253
});

lib/optimize/SplitChunksPlugin.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,11 @@ module.exports = class SplitChunksPlugin {
115115
minChunks: options.minChunks || 1,
116116
maxAsyncRequests: options.maxAsyncRequests || 1,
117117
maxInitialRequests: options.maxInitialRequests || 1,
118-
getName:
119-
SplitChunksPlugin.normalizeName({
120-
name: options.name,
121-
automaticNameDelimiter: options.automaticNameDelimiter
122-
}) || (() => {}),
123118
hidePathInfo: options.hidePathInfo || false,
124119
filename: options.filename || undefined,
125120
getCacheGroups: SplitChunksPlugin.normalizeCacheGroups({
126121
cacheGroups: options.cacheGroups,
122+
name: options.name,
127123
automaticNameDelimiter: options.automaticNameDelimiter
128124
}),
129125
automaticNameDelimiter: options.automaticNameDelimiter,
@@ -134,7 +130,7 @@ module.exports = class SplitChunksPlugin {
134130
};
135131
}
136132

137-
static normalizeName({ name, automaticNameDelimiter }) {
133+
static normalizeName({ name, automaticNameDelimiter, automaticNamePrefix }) {
138134
if (name === true) {
139135
/** @type {WeakMap<Chunk[], Record<string, string>>} */
140136
const cache = new WeakMap();
@@ -152,10 +148,12 @@ module.exports = class SplitChunksPlugin {
152148
return;
153149
}
154150
names.sort();
155-
let name =
156-
(cacheGroup && cacheGroup !== "default"
157-
? cacheGroup + automaticNameDelimiter
158-
: "") + names.join(automaticNameDelimiter);
151+
const prefix =
152+
typeof automaticNamePrefix === "string"
153+
? automaticNamePrefix
154+
: cacheGroup;
155+
const namePrefix = prefix ? prefix + automaticNameDelimiter : "";
156+
let name = namePrefix + names.join(automaticNameDelimiter);
159157
// Filenames and paths can't be too long otherwise an
160158
// ENAMETOOLONG error is raised. If the generated name if too
161159
// long, it is truncated and a hash is appended. The limit has
@@ -212,7 +210,7 @@ module.exports = class SplitChunksPlugin {
212210
};
213211
}
214212

215-
static normalizeCacheGroups({ cacheGroups, automaticNameDelimiter }) {
213+
static normalizeCacheGroups({ cacheGroups, name, automaticNameDelimiter }) {
216214
if (typeof cacheGroups === "function") {
217215
// TODO webpack 5 remove this
218216
if (cacheGroups.length !== 1) {
@@ -251,10 +249,15 @@ module.exports = class SplitChunksPlugin {
251249
results.push({
252250
key: key,
253251
priority: option.priority,
254-
getName: SplitChunksPlugin.normalizeName({
255-
name: option.name,
256-
automaticNameDelimiter
257-
}),
252+
getName:
253+
SplitChunksPlugin.normalizeName({
254+
name: option.name || name,
255+
automaticNameDelimiter:
256+
typeof option.automaticNameDelimiter === "string"
257+
? option.automaticNameDelimiter
258+
: automaticNameDelimiter,
259+
automaticNamePrefix: option.automaticNamePrefix
260+
}) || (() => {}),
258261
chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
259262
option.chunks
260263
),
@@ -487,6 +490,12 @@ module.exports = class SplitChunksPlugin {
487490
chunksKeys: new Set()
488491
})
489492
);
493+
} else {
494+
if (info.cacheGroup !== cacheGroup) {
495+
if (info.cacheGroup.priority < cacheGroup.priority) {
496+
info.cacheGroup = cacheGroup;
497+
}
498+
}
490499
}
491500
info.modules.add(module);
492501
info.size += module.size();

schemas/WebpackOptions.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,15 @@
15291529
}
15301530
]
15311531
},
1532+
"automaticNameDelimiter": {
1533+
"description": "Sets the name delimiter for created chunks",
1534+
"type": "string",
1535+
"minLength": 1
1536+
},
1537+
"automaticNamePrefix": {
1538+
"description": "Sets the name prefix for created chunks",
1539+
"type": "string"
1540+
},
15321541
"filename": {
15331542
"description": "Sets the template for the filename for created chunks (Only works for initial chunks)",
15341543
"type": "string",

test/__snapshots__/StatsTestCases.test.js.snap

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,41 @@ Child custom-chunks-filter-in-cache-groups:
26712671
[5] ./c.js 72 bytes {3} {8} [built]"
26722672
`;
26732673

2674+
exports[`StatsTestCases should print correct stats for split-chunks-automatic-name 1`] = `
2675+
"Entrypoint main = main.js
2676+
chunk {0} common~async-a~async-b~async-c.js (common~async-a~async-b~async-c) 40 bytes <{7}> ={1}= ={2}= ={3}= ={4}= ={5}= ={6}= [rendered] split chunk (cache group: vendors) (name: common~async-a~async-b~async-c)
2677+
> ./a [8] ./index.js 1:0-47
2678+
> ./b [8] ./index.js 2:0-47
2679+
> ./c [8] ./index.js 3:0-47
2680+
[0] ./d.js 20 bytes {0} [built]
2681+
[1] ./node_modules/x.js 20 bytes {0} [built]
2682+
chunk {1} common~async-b~async-c.js (common~async-b~async-c) 20 bytes <{7}> ={0}= ={2}= ={4}= ={5}= ={6}= [rendered] split chunk (cache group: vendors) (name: common~async-b~async-c)
2683+
> ./b [8] ./index.js 2:0-47
2684+
> ./c [8] ./index.js 3:0-47
2685+
[3] ./f.js 20 bytes {1} [built]
2686+
chunk {2} common~async-a~async-b.js (common~async-a~async-b) 20 bytes <{7}> ={0}= ={1}= ={3}= ={4}= [rendered] split chunk (cache group: vendors) (name: common~async-a~async-b)
2687+
> ./a [8] ./index.js 1:0-47
2688+
> ./b [8] ./index.js 2:0-47
2689+
[2] ./node_modules/y.js 20 bytes {2} [built]
2690+
chunk {3} async-a.js (async-a) 107 bytes <{7}> ={0}= ={2}= [rendered]
2691+
> ./a [8] ./index.js 1:0-47
2692+
[7] ./a.js + 1 modules 107 bytes {3} [built]
2693+
| ./a.js 72 bytes [built]
2694+
| ./e.js 20 bytes [built]
2695+
chunk {4} async-b.js (async-b) 72 bytes <{7}> ={0}= ={1}= ={2}= [rendered]
2696+
> ./b [8] ./index.js 2:0-47
2697+
[5] ./b.js 72 bytes {4} [built]
2698+
chunk {5} async-c.js (async-c) 72 bytes <{7}> ={0}= ={1}= ={6}= [rendered]
2699+
> ./c [8] ./index.js 3:0-47
2700+
[6] ./c.js 72 bytes {5} [built]
2701+
chunk {6} common~async-c.js (common~async-c) 20 bytes <{7}> ={0}= ={1}= ={5}= [rendered] split chunk (cache group: vendors) (name: common~async-c)
2702+
> ./c [8] ./index.js 3:0-47
2703+
[4] ./node_modules/z.js 20 bytes {6} [built]
2704+
chunk {7} main.js (main) 147 bytes >{0}< >{1}< >{2}< >{3}< >{4}< >{5}< >{6}< [entry] [rendered]
2705+
> ./ main
2706+
[8] ./index.js 147 bytes {7} [built]"
2707+
`;
2708+
26742709
exports[`StatsTestCases should print correct stats for split-chunks-combinations 1`] = `
26752710
"Entrypoint main = main.js
26762711
chunk {0} async-a~async-b.js (async-a~async-b) 134 bytes <{8}> ={1}= ={2}= [rendered] split chunk (cache group: default) (name: async-a~async-b)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./d";
2+
import "./e";
3+
import "x";
4+
import "y";
5+
export default "a";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./d";
2+
import "./f";
3+
import "x";
4+
import "y";
5+
export default "b";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./d";
2+
import "./f";
3+
import "x";
4+
import "z";
5+
export default "c";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "d";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "e";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "f";

0 commit comments

Comments
 (0)