Skip to content

Commit

Permalink
Merge pull request #6651 from webpack/feature/split-chunks-filename
Browse files Browse the repository at this point in the history
allow to configure filename for splitted chunks
  • Loading branch information
sokra committed Mar 3, 2018
2 parents 2e687d0 + ecb65aa commit 01c18cc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/JavascriptModulesPlugin.js
Expand Up @@ -100,7 +100,10 @@ class JavascriptModulesPlugin {
const moduleTemplates = options.moduleTemplates;
const dependencyTemplates = options.dependencyTemplates;

const filenameTemplate = outputOptions.chunkFilename;
let filenameTemplate;
if (chunk.filenameTemplate)
filenameTemplate = chunk.filenameTemplate;
else filenameTemplate = outputOptions.chunkFilename;

result.push({
render: () =>
Expand Down
17 changes: 17 additions & 0 deletions lib/optimize/SplitChunksPlugin.js
Expand Up @@ -91,6 +91,7 @@ module.exports = class SplitChunksPlugin {
maxAsyncRequests: options.maxAsyncRequests || 1,
maxInitialRequests: options.maxInitialRequests || 1,
getName: SplitChunksPlugin.normalizeName(options.name) || (() => {}),
filename: options.filename || undefined,
getCacheGroups: SplitChunksPlugin.normalizeCacheGroups(
options.cacheGroups
)
Expand Down Expand Up @@ -169,6 +170,7 @@ module.exports = class SplitChunksPlugin {
minChunks: option.minChunks,
maxAsyncRequests: option.maxAsyncRequests,
maxInitialRequests: option.maxInitialRequests,
filename: option.filename,
reuseExistingChunk: option.reuseExistingChunk
});
}
Expand Down Expand Up @@ -277,6 +279,10 @@ module.exports = class SplitChunksPlugin {
cacheGroupSource.getName !== undefined
? cacheGroupSource.getName
: this.options.getName,
filename:
cacheGroupSource.filename !== undefined
? cacheGroupSource.filename
: this.options.filename,
reuseExistingChunk: cacheGroupSource.reuseExistingChunk
};
// For all combination of chunk selection
Expand Down Expand Up @@ -439,6 +445,17 @@ module.exports = class SplitChunksPlugin {
newChunk.entryModule = undefined;
}
}
if (item.cacheGroup.filename) {
if (!newChunk.isOnlyInitial()) {
throw new Error(
"SplitChunksPlugin: You are trying to set a filename for a chunk which is (also) loaded on demand. " +
"The runtime can only handle loading of chunks which match the chunkFilename schema. " +
"Using a custom filename would fail at runtime. " +
`(cache group: ${item.cacheGroup.key})`
);
}
newChunk.filenameTemplate = item.cacheGroup.filename;
}
if (!isReused) {
// Add all modules to the new chunk
for (const module of item.modules) {
Expand Down
10 changes: 10 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -1377,6 +1377,11 @@
}
]
},
"filename": {
"description": "Sets the template for the filename for created chunks (Only works for initial chunks)",
"type": "string",
"minLength": 1
},
"cacheGroups": {
"description": "Assign modules to a cache group (modules from different cache groups are tried to keep in separate chunks)",
"type": "object",
Expand Down Expand Up @@ -1468,6 +1473,11 @@
"type": "string"
}
]
},
"filename": {
"description": "Sets the template for the filename for created chunks (Only works for initial chunks)",
"type": "string",
"minLength": 1
}
}
}
Expand Down
@@ -0,0 +1,9 @@
it("should create a vendor file", function() {
var fs = require("fs");
var path = require("path");
if(!fs.existsSync(path.join(__dirname, "vendor.js")))
throw new Error("vendor.js file was not created");
});

require.include("test");

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,19 @@
module.exports = {
mode: "development",
node: {
__dirname: false,
__filename: false
},
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: "initial",
filename: "vendor.js",
enforce: true
}
}
}
}
};

0 comments on commit 01c18cc

Please sign in to comment.