Skip to content

Commit 6ddba9b

Browse files
authored
Merge pull request webpack#6712 from ManuelBauer/master
Allow configuration of split chunk filename delimiter
2 parents 66ff412 + 771bf85 commit 6ddba9b

File tree

17 files changed

+133
-14
lines changed

17 files changed

+133
-14
lines changed

lib/WebpackOptionsDefaulter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
210210
this.set("optimization.splitChunks.minSize", 30000);
211211
this.set("optimization.splitChunks.minChunks", 1);
212212
this.set("optimization.splitChunks.maxAsyncRequests", 5);
213+
this.set("optimization.splitChunks.automaticNameDelimiter", "~");
213214
this.set("optimization.splitChunks.maxInitialRequests", 3);
214215
this.set("optimization.splitChunks.name", true);
215216
this.set("optimization.splitChunks.cacheGroups", {});

lib/optimize/SplitChunksPlugin.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,45 +90,52 @@ module.exports = class SplitChunksPlugin {
9090
minChunks: options.minChunks || 1,
9191
maxAsyncRequests: options.maxAsyncRequests || 1,
9292
maxInitialRequests: options.maxInitialRequests || 1,
93-
getName: SplitChunksPlugin.normalizeName(options.name) || (() => {}),
93+
getName:
94+
SplitChunksPlugin.normalizeName({
95+
name: options.name,
96+
automaticNameDelimiter: options.automaticNameDelimiter
97+
}) || (() => {}),
9498
filename: options.filename || undefined,
95-
getCacheGroups: SplitChunksPlugin.normalizeCacheGroups(
96-
options.cacheGroups
97-
)
99+
getCacheGroups: SplitChunksPlugin.normalizeCacheGroups({
100+
cacheGroups: options.cacheGroups,
101+
automaticNameDelimiter: options.automaticNameDelimiter
102+
})
98103
};
99104
}
100105

101-
static normalizeName(option) {
102-
if (option === true) {
106+
static normalizeName({ name, automaticNameDelimiter }) {
107+
if (name === true) {
103108
const fn = (module, chunks, cacheGroup) => {
104109
const names = chunks.map(c => c.name);
105110
if (!names.every(Boolean)) return;
106111
names.sort();
107112
let name =
108-
(cacheGroup && cacheGroup !== "default" ? cacheGroup + "~" : "") +
109-
names.join("~");
113+
(cacheGroup && cacheGroup !== "default"
114+
? cacheGroup + automaticNameDelimiter
115+
: "") + names.join(automaticNameDelimiter);
110116
// Filenames and paths can't be too long otherwise an
111117
// ENAMETOOLONG error is raised. If the generated name if too
112118
// long, it is truncated and a hash is appended. The limit has
113119
// been set to 100 to prevent `[name].[chunkhash].[ext]` from
114120
// generating a 256+ character string.
115121
if (name.length > 100) {
116-
name = name.slice(0, 100) + "~" + hashFilename(name);
122+
name =
123+
name.slice(0, 100) + automaticNameDelimiter + hashFilename(name);
117124
}
118125
return name;
119126
};
120127
return fn;
121128
}
122-
if (typeof option === "string") {
129+
if (typeof name === "string") {
123130
const fn = () => {
124-
return option;
131+
return name;
125132
};
126133
return fn;
127134
}
128-
if (typeof option === "function") return option;
135+
if (typeof name === "function") return name;
129136
}
130137

131-
static normalizeCacheGroups(cacheGroups) {
138+
static normalizeCacheGroups({ cacheGroups, automaticNameDelimiter }) {
132139
if (typeof cacheGroups === "function") {
133140
return cacheGroups;
134141
}
@@ -163,7 +170,10 @@ module.exports = class SplitChunksPlugin {
163170
results.push({
164171
key: key,
165172
priority: option.priority,
166-
getName: SplitChunksPlugin.normalizeName(option.name),
173+
getName: SplitChunksPlugin.normalizeName({
174+
name: option.name,
175+
automaticNameDelimiter
176+
}),
167177
chunks: option.chunks,
168178
enforce: option.enforce,
169179
minSize: option.minSize,

schemas/WebpackOptions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,11 @@
13821382
"type": "string",
13831383
"minLength": 1
13841384
},
1385+
"automaticNameDelimiter": {
1386+
"description": "Sets the name delimiter for created chunks",
1387+
"type": "string",
1388+
"minLength": 1
1389+
},
13851390
"cacheGroups": {
13861391
"description": "Assign modules to a cache group (modules from different cache groups are tried to keep in separate chunks)",
13871392
"type": "object",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const c = require("./commons");
2+
3+
module.exports = "a" + c;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const c = require("./commons");
2+
3+
module.exports = "b" + c;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const c = require("./commons");
2+
3+
module.exports = "c" + c;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* Large module to trigger chunk generation */
2+
module.exports = "commons";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require("should");
2+
3+
it("should run", function() {
4+
Promise.all(
5+
[
6+
import(/* webpackChunkName: "a" */ "./a"),
7+
import(/* webpackChunkName: "b" */ "./b"),
8+
import(/* webpackChunkName: "c" */ "./c")
9+
]
10+
);
11+
12+
const files = require("fs").readdirSync(__dirname);
13+
const hasFile = files.indexOf('a~b~c.bundle.js') !== -1;
14+
15+
hasFile.should.be.eql(true);
16+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
findBundle: function(i, options) {
3+
return ["main.js"];
4+
}
5+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
entry: {
3+
main: "./index"
4+
},
5+
node: {
6+
__dirname: false,
7+
__filename: false
8+
},
9+
output: {
10+
filename: "[name].js",
11+
chunkFilename: "[name].bundle.js",
12+
jsonpFunction: "_load_chunk"
13+
},
14+
optimization: {
15+
splitChunks: {
16+
minSize: 1
17+
}
18+
}
19+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const c = require("./commons");
2+
3+
module.exports = "a" + c;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const c = require("./commons");
2+
3+
module.exports = "b" + c;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const c = require("./commons");
2+
3+
module.exports = "c" + c;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
module.exports = "commons";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require("should");
2+
3+
it("should run", function() {
4+
Promise.all(
5+
[
6+
import(/* webpackChunkName: "a" */ "./a"),
7+
import(/* webpackChunkName: "b" */ "./b"),
8+
import(/* webpackChunkName: "c" */ "./c")
9+
]
10+
);
11+
12+
const files = require("fs").readdirSync(__dirname);
13+
const hasFile = files.indexOf('a-b-c.bundle.js') !== -1;
14+
15+
hasFile.should.be.eql(true);
16+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
findBundle: function(i, options) {
3+
return ["main.js"];
4+
}
5+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
entry: {
3+
main: "./index"
4+
},
5+
node: {
6+
__dirname: false,
7+
__filename: false
8+
},
9+
output: {
10+
filename: "[name].js",
11+
chunkFilename: "[name].bundle.js",
12+
jsonpFunction: "_load_chunk"
13+
},
14+
optimization: {
15+
splitChunks: {
16+
automaticNameDelimiter: "-",
17+
minSize: 1
18+
}
19+
}
20+
};

0 commit comments

Comments
 (0)