Skip to content

Commit 01c18cc

Browse files
authored
Merge pull request webpack#6651 from webpack/feature/split-chunks-filename
allow to configure filename for splitted chunks
2 parents 2e687d0 + ecb65aa commit 01c18cc

File tree

6 files changed

+60
-1
lines changed

6 files changed

+60
-1
lines changed

lib/JavascriptModulesPlugin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ class JavascriptModulesPlugin {
100100
const moduleTemplates = options.moduleTemplates;
101101
const dependencyTemplates = options.dependencyTemplates;
102102

103-
const filenameTemplate = outputOptions.chunkFilename;
103+
let filenameTemplate;
104+
if (chunk.filenameTemplate)
105+
filenameTemplate = chunk.filenameTemplate;
106+
else filenameTemplate = outputOptions.chunkFilename;
104107

105108
result.push({
106109
render: () =>

lib/optimize/SplitChunksPlugin.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ module.exports = class SplitChunksPlugin {
9191
maxAsyncRequests: options.maxAsyncRequests || 1,
9292
maxInitialRequests: options.maxInitialRequests || 1,
9393
getName: SplitChunksPlugin.normalizeName(options.name) || (() => {}),
94+
filename: options.filename || undefined,
9495
getCacheGroups: SplitChunksPlugin.normalizeCacheGroups(
9596
options.cacheGroups
9697
)
@@ -169,6 +170,7 @@ module.exports = class SplitChunksPlugin {
169170
minChunks: option.minChunks,
170171
maxAsyncRequests: option.maxAsyncRequests,
171172
maxInitialRequests: option.maxInitialRequests,
173+
filename: option.filename,
172174
reuseExistingChunk: option.reuseExistingChunk
173175
});
174176
}
@@ -277,6 +279,10 @@ module.exports = class SplitChunksPlugin {
277279
cacheGroupSource.getName !== undefined
278280
? cacheGroupSource.getName
279281
: this.options.getName,
282+
filename:
283+
cacheGroupSource.filename !== undefined
284+
? cacheGroupSource.filename
285+
: this.options.filename,
280286
reuseExistingChunk: cacheGroupSource.reuseExistingChunk
281287
};
282288
// For all combination of chunk selection
@@ -439,6 +445,17 @@ module.exports = class SplitChunksPlugin {
439445
newChunk.entryModule = undefined;
440446
}
441447
}
448+
if (item.cacheGroup.filename) {
449+
if (!newChunk.isOnlyInitial()) {
450+
throw new Error(
451+
"SplitChunksPlugin: You are trying to set a filename for a chunk which is (also) loaded on demand. " +
452+
"The runtime can only handle loading of chunks which match the chunkFilename schema. " +
453+
"Using a custom filename would fail at runtime. " +
454+
`(cache group: ${item.cacheGroup.key})`
455+
);
456+
}
457+
newChunk.filenameTemplate = item.cacheGroup.filename;
458+
}
442459
if (!isReused) {
443460
// Add all modules to the new chunk
444461
for (const module of item.modules) {

schemas/WebpackOptions.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,11 @@
13771377
}
13781378
]
13791379
},
1380+
"filename": {
1381+
"description": "Sets the template for the filename for created chunks (Only works for initial chunks)",
1382+
"type": "string",
1383+
"minLength": 1
1384+
},
13801385
"cacheGroups": {
13811386
"description": "Assign modules to a cache group (modules from different cache groups are tried to keep in separate chunks)",
13821387
"type": "object",
@@ -1468,6 +1473,11 @@
14681473
"type": "string"
14691474
}
14701475
]
1476+
},
1477+
"filename": {
1478+
"description": "Sets the template for the filename for created chunks (Only works for initial chunks)",
1479+
"type": "string",
1480+
"minLength": 1
14711481
}
14721482
}
14731483
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
it("should create a vendor file", function() {
2+
var fs = require("fs");
3+
var path = require("path");
4+
if(!fs.existsSync(path.join(__dirname, "vendor.js")))
5+
throw new Error("vendor.js file was not created");
6+
});
7+
8+
require.include("test");
9+

test/configCases/filename-template/split-chunks-filename/node_modules/test.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
mode: "development",
3+
node: {
4+
__dirname: false,
5+
__filename: false
6+
},
7+
optimization: {
8+
splitChunks: {
9+
cacheGroups: {
10+
vendor: {
11+
test: /node_modules/,
12+
chunks: "initial",
13+
filename: "vendor.js",
14+
enforce: true
15+
}
16+
}
17+
}
18+
}
19+
};

0 commit comments

Comments
 (0)