Skip to content

Commit b50a5b6

Browse files
authored
Merge pull request webpack#6384 from benthemonkey/next
Add "single" option to RuntimeChunkPlugin
2 parents 1de3d48 + 18c1e28 commit b50a5b6

12 files changed

Lines changed: 102 additions & 4 deletions

File tree

‎lib/WebpackOptionsApply.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ class WebpackOptionsApply extends OptionsApply {
291291
if(options.optimization.splitChunks)
292292
new SplitChunksPlugin(options.optimization.splitChunks).apply(compiler);
293293
if(options.optimization.runtimeChunk)
294-
new RuntimeChunkPlugin().apply(compiler);
294+
new RuntimeChunkPlugin(options.optimization.runtimeChunk).apply(compiler);
295295
if(options.optimization.noEmitOnErrors)
296296
new NoEmitOnErrorsPlugin().apply(compiler);
297297
if(options.optimization.namedModules)

‎lib/WebpackOptionsDefaulter.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
181181
test: /[\\/]node_modules[\\/]/,
182182
priority: -10
183183
});
184+
this.set("optimization.runtimeChunk", "call", value => {
185+
if(value === "single") {
186+
return {
187+
name: "runtime"
188+
};
189+
}
190+
if(value === true || value === "multiple") {
191+
return {
192+
name: entrypoint => `runtime~${entrypoint.name}`
193+
};
194+
}
195+
return value;
196+
});
184197
this.set("optimization.noEmitOnErrors", "make", options => isProductionLikeMode(options));
185198
this.set("optimization.namedModules", "make", options => options.mode === "development");
186199
this.set("optimization.namedChunks", "make", options => options.mode === "development");

‎lib/optimize/RuntimeChunkPlugin.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55
"use strict";
66

77
module.exports = class RuntimeChunkPlugin {
8-
constructor(options) {}
8+
constructor(options) {
9+
this.options = Object.assign({
10+
name: entrypoint => `runtime~${entrypoint.name}`
11+
}, options);
12+
}
913

1014
apply(compiler) {
1115
compiler.hooks.thisCompilation.tap("RuntimeChunkPlugin", compilation => {
1216
compilation.hooks.optimizeChunksAdvanced.tap("RuntimeChunkPlugin", () => {
1317
for(const entrypoint of compilation.entrypoints.values()) {
1418
const chunk = entrypoint.getRuntimeChunk();
1519
if(chunk.getNumberOfModules() > 0) {
16-
const newChunk = compilation.addChunk(entrypoint.name + "-runtime");
20+
let name = this.options.name;
21+
if(typeof name === "function") {
22+
name = name(entrypoint);
23+
}
24+
const newChunk = compilation.addChunk(name);
1725
entrypoint.unshiftChunk(newChunk);
1826
newChunk.addGroup(entrypoint);
1927
entrypoint.setRuntimeChunk(newChunk);

‎schemas/WebpackOptions.json‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,34 @@
14841484
},
14851485
"runtimeChunk": {
14861486
"description": "Create an additional chunk which contains only the webpack runtime and chunk hash maps",
1487-
"type": "boolean"
1487+
"oneOf": [
1488+
{
1489+
"type": "boolean"
1490+
},
1491+
{
1492+
"enum": [
1493+
"single",
1494+
"multiple"
1495+
]
1496+
},
1497+
{
1498+
"type": "object",
1499+
"additionalProperties": false,
1500+
"properties": {
1501+
"name": {
1502+
"description": "The name or name factory for the runtime chunks",
1503+
"oneOf": [
1504+
{
1505+
"type": "string"
1506+
},
1507+
{
1508+
"instanceof": "Function"
1509+
}
1510+
]
1511+
}
1512+
}
1513+
}
1514+
]
14881515
},
14891516
"noEmitOnErrors": {
14901517
"description": "Avoid emitting assets when errors occur",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "entry1";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "entry2";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Entrypoint e1 = runtime.js e1.js
2+
Entrypoint e2 = runtime.js e2.js
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
mode: "development",
3+
entry: {
4+
e1: "./e1",
5+
e2: "./e2"
6+
},
7+
output: {
8+
filename: "[name].js"
9+
},
10+
stats: {
11+
hash: false,
12+
timings: false,
13+
builtAt: false,
14+
assets: false,
15+
modules: false,
16+
reasons: true
17+
},
18+
optimization: {
19+
runtimeChunk: "single"
20+
}
21+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "entry1";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "entry2";

0 commit comments

Comments
 (0)