Skip to content

Commit 085d288

Browse files
committed
Added chunkFilenameDelimiter option for SplitChunksPlugin: Added tests and fixed code style
1 parent ddb78d7 commit 085d288

File tree

15 files changed

+1452
-11
lines changed

15 files changed

+1452
-11
lines changed

lib/optimize/SplitChunksPlugin.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,39 +84,46 @@ module.exports = class SplitChunksPlugin {
8484
}
8585

8686
static normalizeOptions(options = {}) {
87-
const chunkFilenameDelimiter = options.chunkFilenameDelimiter || '~';
87+
const chunkFilenameDelimiter = options.chunkFilenameDelimiter || "~";
8888

8989
return {
9090
chunks: options.chunks || "all",
9191
minSize: options.minSize || 0,
9292
minChunks: options.minChunks || 1,
9393
maxAsyncRequests: options.maxAsyncRequests || 1,
9494
maxInitialRequests: options.maxInitialRequests || 1,
95-
getName: SplitChunksPlugin.normalizeName(options.name, chunkFilenameDelimiter) || (() => {}),
95+
getName:
96+
SplitChunksPlugin.normalizeName({
97+
option: options.name,
98+
chunkFilenameDelimiter
99+
}) || (() => {}),
96100
filename: options.filename || undefined,
97-
getCacheGroups: SplitChunksPlugin.normalizeCacheGroups(
98-
options.cacheGroups, chunkFilenameDelimiter
99-
),
101+
getCacheGroups: SplitChunksPlugin.normalizeCacheGroups({
102+
cacheGroups: options.cacheGroups,
103+
chunkFilenameDelimiter
104+
}),
100105
chunkFilenameDelimiter: chunkFilenameDelimiter
101106
};
102107
}
103108

104-
static normalizeName(option, chunkFilenameDelimiter) {
109+
static normalizeName({ option, chunkFilenameDelimiter }) {
105110
if (option === true) {
106111
const fn = (module, chunks, cacheGroup) => {
107112
const names = chunks.map(c => c.name);
108113
if (!names.every(Boolean)) return;
109114
names.sort();
110115
let name =
111-
(cacheGroup && cacheGroup !== "default" ? cacheGroup + chunkFilenameDelimiter : "") +
112-
names.join(chunkFilenameDelimiter);
116+
(cacheGroup && cacheGroup !== "default"
117+
? cacheGroup + chunkFilenameDelimiter
118+
: "") + names.join(chunkFilenameDelimiter);
113119
// Filenames and paths can't be too long otherwise an
114120
// ENAMETOOLONG error is raised. If the generated name if too
115121
// long, it is truncated and a hash is appended. The limit has
116122
// been set to 100 to prevent `[name].[chunkhash].[ext]` from
117123
// generating a 256+ character string.
118124
if (name.length > 100) {
119-
name = name.slice(0, 100) + chunkFilenameDelimiter + hashFilename(name);
125+
name =
126+
name.slice(0, 100) + chunkFilenameDelimiter + hashFilename(name);
120127
}
121128
return name;
122129
};
@@ -131,7 +138,7 @@ module.exports = class SplitChunksPlugin {
131138
if (typeof option === "function") return option;
132139
}
133140

134-
static normalizeCacheGroups(cacheGroups, chunkFilenameDelimiter) {
141+
static normalizeCacheGroups({ cacheGroups, chunkFilenameDelimiter }) {
135142
if (typeof cacheGroups === "function") {
136143
return cacheGroups;
137144
}
@@ -166,7 +173,10 @@ module.exports = class SplitChunksPlugin {
166173
results.push({
167174
key: key,
168175
priority: option.priority,
169-
getName: SplitChunksPlugin.normalizeName(option.name, chunkFilenameDelimiter),
176+
getName: SplitChunksPlugin.normalizeName({
177+
option: option.name,
178+
chunkFilenameDelimiter
179+
}),
170180
chunks: option.chunks,
171181
enforce: option.enforce,
172182
minSize: option.minSize,
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;

test/configCases/split-chunks/chunk-filename-delimiter-default/commons.js

Lines changed: 666 additions & 0 deletions
Large diffs are not rendered by default.
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const SplitChunksPlugin = require("../../../../lib/optimize/SplitChunksPlugin");
2+
3+
module.exports = {
4+
entry: {
5+
main: "./index"
6+
},
7+
node: {
8+
__dirname: false,
9+
__filename: false
10+
},
11+
output: {
12+
filename: "[name].js",
13+
chunkFilename: "[name].bundle.js",
14+
jsonpFunction: "_load_chunk"
15+
},
16+
plugins: [new SplitChunksPlugin()]
17+
};
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;

0 commit comments

Comments
 (0)