Skip to content

Commit a04ffb9

Browse files
committed
added (experimental) NamedModulesPlugin
1 parent e925c52 commit a04ffb9

15 files changed

Lines changed: 108 additions & 55 deletions

‎lib/Compilation.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ Compilation.prototype.seal = function seal(callback) {
525525

526526
this.applyPlugins("revive-modules", this.modules, this.records);
527527
this.applyPlugins("optimize-module-order", this.modules);
528+
this.applyPlugins("before-module-ids", this.modules);
528529
this.applyModuleIds();
529530
this.applyPlugins("optimize-module-ids", this.modules);
530531
this.applyPlugins("after-optimize-module-ids", this.modules);
@@ -533,6 +534,7 @@ Compilation.prototype.seal = function seal(callback) {
533534

534535
this.applyPlugins("revive-chunks", this.chunks, this.records);
535536
this.applyPlugins("optimize-chunk-order", this.chunks);
537+
this.applyPlugins("before-chunk-ids", this.chunks);
536538
this.applyChunkIds();
537539
this.applyPlugins("optimize-chunk-ids", this.chunks);
538540
this.applyPlugins("after-optimize-chunk-ids", this.chunks);

‎lib/HotModuleReplacement.runtime.js‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ module.exports = function() {
7474
hot._selfAccepted = true;
7575
else if(typeof dep === "function")
7676
hot._selfAccepted = dep;
77-
else if(typeof dep === "number")
78-
hot._acceptedDependencies[dep] = callback;
79-
else for(var i = 0; i < dep.length; i++)
77+
else if(typeof dep === "object")
78+
for(var i = 0; i < dep.length; i++)
8079
hot._acceptedDependencies[dep[i]] = callback;
80+
else
81+
hot._acceptedDependencies[dep] = callback;
8182
},
8283
decline: function(dep) {
8384
if(typeof dep === "undefined")
@@ -139,6 +140,10 @@ module.exports = function() {
139140
// The update info
140141
var hotUpdate, hotUpdateNewHash;
141142

143+
function toModuleId(id) {
144+
return (+id) + "" === id ? +id : id;
145+
}
146+
142147
function hotCheck(apply, callback) {
143148
if(hotStatus !== "idle") throw new Error("check() is only allowed in idle status");
144149
if(typeof apply === "function") {
@@ -212,7 +217,7 @@ module.exports = function() {
212217
var outdatedModules = [];
213218
for(var id in hotUpdate) {
214219
if(Object.prototype.hasOwnProperty.call(hotUpdate, id)) {
215-
outdatedModules.push(+id);
220+
outdatedModules.push(toModuleId(id));
216221
}
217222
}
218223
callback(null, outdatedModules);
@@ -244,9 +249,6 @@ module.exports = function() {
244249
if(module.hot._selfDeclined) {
245250
return new Error("Aborted because of self decline: " + moduleId);
246251
}
247-
if(moduleId === 0) {
248-
return;
249-
}
250252
for(var i = 0; i < module.parents.length; i++) {
251253
var parentId = module.parents[i];
252254
var parent = installedModules[parentId];
@@ -283,7 +285,7 @@ module.exports = function() {
283285
var appliedUpdate = {};
284286
for(var id in hotUpdate) {
285287
if(Object.prototype.hasOwnProperty.call(hotUpdate, id)) {
286-
var moduleId = +id;
288+
var moduleId = toModuleId(id);
287289
var result = getAffectedStuff(moduleId);
288290
if(!result) {
289291
if(options.ignoreUnaccepted)

‎lib/MultiModule.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ MultiModule.prototype.source = function(dependencyTemplates, outputOptions) {
4444
str.push("__webpack_require__(");
4545
if(outputOptions.pathinfo)
4646
str.push("/*! " + dep.request + " */");
47-
str.push("" + dep.module.id);
47+
str.push("" + JSON.stringify(dep.module.id));
4848
str.push(")");
4949
} else {
5050
str.push("(function webpackMissingModule() { throw new Error(");

‎lib/NamedModulesPlugin.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function NamedModulesPlugin(options) {
6+
this.options = options || {};
7+
}
8+
module.exports = NamedModulesPlugin;
9+
NamedModulesPlugin.prototype.apply = function(compiler) {
10+
compiler.plugin("compilation", function(compilation) {
11+
compilation.plugin("before-module-ids", function(modules) {
12+
modules.forEach(function(module) {
13+
if(module.id === null && module.libIdent) {
14+
module.id = module.libIdent({
15+
context: this.options.context || compiler.options.context
16+
});
17+
}
18+
}, this);
19+
}.bind(this));
20+
}.bind(this));
21+
};

‎lib/Template.js‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Template.getFunctionContent = function(fn) {
1616
};
1717

1818
Template.toIdentifier = function(str) {
19+
if(typeof str !== "string") return "";
1920
return str.replace(/^[^a-zA-Z$_]/, "_").replace(/[^a-zA-Z0-9$_]/g, "_");
2021
};
2122

@@ -47,24 +48,40 @@ Template.prototype.asString = function(str) {
4748
return str;
4849
};
4950

51+
Template.prototype.getModulesArrayBounds = function(modules) {
52+
if(modules.some(function(module) {
53+
return typeof module.id !== "number";
54+
}))
55+
return false;
56+
var maxId = -Infinity;
57+
var minId = Infinity;
58+
modules.forEach(function(module) {
59+
if(maxId < module.id) maxId = module.id;
60+
if(minId > module.id) minId = module.id;
61+
});
62+
if(minId < 16 + ("" + minId).length) {
63+
// add minId x ',' instead of 'Array(minId).concat(...)'
64+
minId = 0;
65+
}
66+
var objectOverhead = modules.map(function(module) {
67+
return (module.id + "").length + 2;
68+
}).reduce(function(a, b) { return a + b; }, -1);
69+
var arrayOverhead = minId === 0 ? maxId : 16 + ("" + minId).length + maxId;
70+
return arrayOverhead < objectOverhead ? [minId, maxId] : false;
71+
};
72+
5073
Template.prototype.renderChunkModules = function(chunk, moduleTemplate, dependencyTemplates, prefix) {
5174
if(!prefix) prefix = "";
5275
var source = new ConcatSource();
5376
if(chunk.modules.length === 0) {
5477
source.add("[]");
5578
return source;
5679
}
57-
var maxId = chunk.modules[chunk.modules.length - 1].id;
58-
var minId = chunk.modules[0].id;
59-
if(minId < 16 + ("" + minId).length) {
60-
minId = 0;
61-
}
62-
var objectOverhead = chunk.modules.map(function(module) {
63-
return (module.id + "").length + 2;
64-
}).reduce(function(a, b) { return a + b; }, -1);
65-
var arrayOverhead = minId === 0 ? maxId : 16 + ("" + minId).length + maxId;
66-
if(arrayOverhead < objectOverhead) {
67-
// Render a sparse array
80+
var bounds = this.getModulesArrayBounds(chunk.modules);
81+
if(bounds) {
82+
// Render a spare array
83+
var minId = bounds[0];
84+
var maxId = bounds[1];
6885
if(minId !== 0) source.add("Array(" + minId + ").concat(");
6986
source.add("[\n");
7087
var modules = {};
@@ -87,7 +104,7 @@ Template.prototype.renderChunkModules = function(chunk, moduleTemplate, dependen
87104
source.add("{\n");
88105
chunk.modules.forEach(function(module, idx) {
89106
if(idx !== 0) source.add(",\n");
90-
source.add("\n/***/ " + module.id + ":\n");
107+
source.add("\n/***/ " + JSON.stringify(module.id) + ":\n");
91108
source.add(moduleTemplate.render(module, dependencyTemplates, chunk));
92109
});
93110
source.add("\n\n" + prefix + "}");

‎lib/dependencies/AMDRequireArrayDependency.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ AMDRequireArrayDependency.Template.prototype.apply = function(dep, source, outpu
2626
var comment = "";
2727
if(outputOptions.pathinfo) comment = "/*! " + requestShortener.shorten(dep.request) + " */ ";
2828
if(dep.module)
29-
return "__webpack_require__(" + comment + dep.module.id + ")";
29+
return "__webpack_require__(" + comment + JSON.stringify(dep.module.id) + ")";
3030
else
3131
return require("./WebpackMissingModule").module(dep.request);
3232
}

‎lib/dependencies/ContextDependencyTemplateAsId.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ ContextDependencyTemplateAsId.prototype.apply = function(dep, source, outputOpti
1111
if(dep.module && dep.module.dependencies && dep.module.dependencies.length > 0) {
1212
if(dep.valueRange) {
1313
source.replace(dep.valueRange[1], dep.range[1] - 1, ")");
14-
source.replace(dep.range[0], dep.valueRange[0] - 1, "__webpack_require__(" + comment + dep.module.id + ").resolve(" + (typeof dep.prepend === "string" ? JSON.stringify(dep.prepend) : "") + "");
14+
source.replace(dep.range[0], dep.valueRange[0] - 1, "__webpack_require__(" + comment + JSON.stringify(dep.module.id) + ").resolve(" + (typeof dep.prepend === "string" ? JSON.stringify(dep.prepend) : "") + "");
1515
} else {
16-
source.replace(dep.range[0], dep.range[1] - 1, "__webpack_require__(" + comment + dep.module.id + ").resolve");
16+
source.replace(dep.range[0], dep.range[1] - 1, "__webpack_require__(" + comment + JSON.stringify(dep.module.id) + ").resolve");
1717
}
1818
} else {
1919
var content = require("./WebpackMissingModule").module(dep.request);

‎lib/dependencies/ContextDependencyTemplateAsRequireCall.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ ContextDependencyTemplateAsRequireCall.prototype.apply = function(dep, source, o
1111
if(dep.module && dep.module.dependencies && dep.module.dependencies.length > 0) {
1212
if(dep.valueRange) {
1313
source.replace(dep.valueRange[1], dep.range[1] - 1, ")");
14-
source.replace(dep.range[0], dep.valueRange[0] - 1, "__webpack_require__(" + comment + dep.module.id + ")(" + (typeof dep.prepend === "string" ? JSON.stringify(dep.prepend) : "") + "");
14+
source.replace(dep.range[0], dep.valueRange[0] - 1, "__webpack_require__(" + comment + JSON.stringify(dep.module.id) + ")(" + (typeof dep.prepend === "string" ? JSON.stringify(dep.prepend) : "") + "");
1515
} else {
16-
source.replace(dep.range[0], dep.range[1] - 1, "__webpack_require__(" + comment + dep.module.id + ")");
16+
source.replace(dep.range[0], dep.range[1] - 1, "__webpack_require__(" + comment + JSON.stringify(dep.module.id) + ")");
1717
}
1818
} else {
1919
var content = require("./WebpackMissingModule").module(dep.request);

‎lib/dependencies/LabeledModuleDependency.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55
var ModuleDependency = require("./ModuleDependency");
6+
var Template = require("../Template");
67

78
function LabeledModuleDependency(request, range) {
89
ModuleDependency.call(this, request);
@@ -21,9 +22,9 @@ LabeledModuleDependency.Template.prototype.apply = function(dep, source, outputO
2122
var comment = "", content;
2223
if(outputOptions.pathinfo) comment = "/*! " + requestShortener.shorten(dep.request) + " */ ";
2324
if(dep.module && dep.module.meta && dep.module.meta.exports) {
24-
content = "var __WEBPACK_LABELED_MODULE__" + dep.module.id + " = __webpack_require__(" + comment + dep.module.id + ")";
25+
content = "var __WEBPACK_LABELED_MODULE__" + Template.toIdentifier(dep.module.id) + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ")";
2526
dep.module.meta.exports.forEach(function(e) {
26-
content += ", " + e + " = __WEBPACK_LABELED_MODULE__" + dep.module.id + "." + e;
27+
content += ", " + e + " = __WEBPACK_LABELED_MODULE__" + Template.toIdentifier(dep.module.id) + "." + e;
2728
});
2829
content += ";";
2930
} else if(dep.module) {
@@ -32,4 +33,4 @@ LabeledModuleDependency.Template.prototype.apply = function(dep, source, outputO
3233
content = require("./WebpackMissingModule").module(dep.request);
3334
}
3435
source.replace(dep.range[0], dep.range[1] - 1, content);
35-
};
36+
};

‎lib/dependencies/ModuleDependencyTemplateAsId.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ModuleDependencyTemplateAsId.prototype.apply = function(dep, source, outputOptio
1010
var comment = "";
1111
if(outputOptions.pathinfo) comment = "/*! " + requestShortener.shorten(dep.request) + " */ ";
1212
if(dep.module)
13-
var content = comment + dep.module.id;
13+
var content = comment + JSON.stringify(dep.module.id);
1414
else
1515
var content = require("./WebpackMissingModule").module(dep.request);
1616
source.replace(dep.range[0], dep.range[1] - 1, content);

0 commit comments

Comments
 (0)