Skip to content

Commit e7c13d7

Browse files
authored
Merge pull request #4553 from timse/named-chunks-plugin
Named chunks plugin
2 parents 050f5f1 + 50a7335 commit e7c13d7

20 files changed

+132
-15
lines changed

lib/AsyncDependenciesBlock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = class AsyncDependenciesBlock extends DependenciesBlock {
2222
updateHash(hash) {
2323
hash.update(this.chunkName || "");
2424
hash.update(this.chunks && this.chunks.map((chunk) => {
25-
return typeof chunk.id === "number" ? chunk.id : "";
25+
return chunk.id !== null ? chunk.id : "";
2626
}).join(",") || "");
2727
super.updateHash(hash);
2828
}

lib/HotModuleReplacement.runtime.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ module.exports = function() {
175175
hotSetStatus("idle");
176176
return null;
177177
}
178-
179178
hotRequestedFilesMap = {};
180179
hotWaitingFilesMap = {};
181180
hotAvailableFilesMap = update.c;

lib/HotModuleReplacementPlugin.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
109109
c: {}
110110
};
111111
Object.keys(records.chunkHashs).forEach(function(chunkId) {
112-
chunkId = +chunkId;
113-
var currentChunk = this.chunks.filter(function(chunk) {
114-
return chunk.id === chunkId;
115-
})[0];
112+
chunkId = isNaN(+chunkId) ? chunkId : +chunkId;
113+
var currentChunk = this.chunks.find(chunk => chunk.id === chunkId);
116114
if(currentChunk) {
117115
var newModules = currentChunk.modules.filter(function(module) {
118116
return module.hotUpdate;
@@ -171,7 +169,7 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
171169
hotInitCode
172170
.replace(/\$require\$/g, this.requireFn)
173171
.replace(/\$hash\$/g, JSON.stringify(hash))
174-
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = " + chunk.id + ";")
172+
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = " + JSON.stringify(chunk.id) + ";")
175173
]);
176174
});
177175

lib/JsonpMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
1818
"var installedChunks = {",
1919
this.indent(
2020
chunk.ids.map(function(id) {
21-
return id + ": 0";
21+
return JSON.stringify(id) + ": 0";
2222
}).join(",\n")
2323
),
2424
"};"

lib/NamedChunksPlugin.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
class NamedChunksPlugin {
8+
9+
static defaultNameResolver(chunk) {
10+
return chunk.name || null;
11+
}
12+
13+
constructor(nameResolver) {
14+
this.nameResolver = nameResolver || NamedChunksPlugin.defaultNameResolver;
15+
}
16+
17+
apply(compiler) {
18+
compiler.plugin("compilation", (compilation) => {
19+
compilation.plugin("before-chunk-ids", (chunks) => {
20+
chunks.forEach((chunk) => {
21+
if(chunk.id === null) {
22+
chunk.id = this.nameResolver(chunk);
23+
}
24+
});
25+
});
26+
});
27+
}
28+
}
29+
30+
module.exports = NamedChunksPlugin;

lib/dependencies/DepBlockHelpers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ DepBlockHelpers.getLoadDepBlockWrapper = function(depBlock, outputOptions, reque
1616
DepBlockHelpers.getDepBlockPromise = function(depBlock, outputOptions, requestShortener, name) {
1717
if(depBlock.chunks) {
1818
var chunks = depBlock.chunks.filter(function(chunk) {
19-
return !chunk.hasRuntime() && typeof chunk.id === "number";
19+
return !chunk.hasRuntime() && chunk.id !== null;
2020
});
2121
if(chunks.length === 1) {
2222
var chunk = chunks[0];
23-
return "__webpack_require__.e" + asComment(name) + "(" + chunk.id + "" +
23+
return "__webpack_require__.e" + asComment(name) + "(" + JSON.stringify(chunk.id) + "" +
2424
(outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : "") +
2525
asComment(depBlock.chunkReason) + ")";
2626
} else if(chunks.length > 0) {
2727
return "Promise.all" + asComment(name) + "(" +
2828
(outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : "") +
2929
"[" +
3030
chunks.map(function(chunk) {
31-
return "__webpack_require__.e(" + chunk.id + ")";
31+
return "__webpack_require__.e(" + JSON.stringify(chunk.id) + ")";
3232
}).join(", ") +
3333
"])";
3434
}

lib/optimize/AggressiveSplittingPlugin.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,19 @@ class AggressiveSplittingPlugin {
9191
newChunk._fromAggressiveSplitting = true;
9292
if(j < savedSplits.length)
9393
newChunk._fromAggressiveSplittingIndex = j;
94-
if(typeof splitData.id === "number") newChunk.id = splitData.id;
94+
if(splitData.id !== null && splitData.id !== undefined) {
95+
newChunk.id = splitData.id;
96+
}
9597
newChunk.origins = chunk.origins.map(copyWithReason);
9698
chunk.origins = chunk.origins.map(copyWithReason);
9799
return true;
98100
} else {
99101
if(j < savedSplits.length)
100102
chunk._fromAggressiveSplittingIndex = j;
101103
chunk.name = null;
102-
if(typeof splitData.id === "number") chunk.id = splitData.id;
104+
if(splitData.id !== null && splitData.id !== undefined) {
105+
chunk.id = splitData.id;
106+
}
103107
}
104108
}
105109
}

lib/webpack.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ exportPlugins(exports, ".", [
102102
"DllReferencePlugin",
103103
"LoaderOptionsPlugin",
104104
"NamedModulesPlugin",
105+
"NamedChunksPlugin",
105106
"HashedModuleIdsPlugin",
106107
"ModuleFilenameHelpers"
107108
]);

test/TestCases.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
/* global describe, it*/
12
"use strict";
23

3-
const should = require("should");
4+
require("should");
45
const path = require("path");
56
const fs = require("fs");
67
const vm = require("vm");
@@ -92,7 +93,8 @@ describe("TestCases", () => {
9293
plugins: [
9394
new webpack.HotModuleReplacementPlugin(),
9495
new webpack.optimize.UglifyJsPlugin(),
95-
new webpack.NamedModulesPlugin()
96+
new webpack.NamedModulesPlugin(),
97+
new webpack.NamedChunksPlugin()
9698
]
9799
}].forEach((config) => {
98100
describe(config.name, () => {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import("./modules/a");
2+
import("./modules/b");
3+

0 commit comments

Comments
 (0)