Skip to content

Commit 951d9ec

Browse files
committed
Merge pull request webpack#394 from kurtharriger/FixHotUpdateFunction
Function name cannot contain -
2 parents f7be3c7 + bdae82a commit 951d9ec

8 files changed

+26
-8
lines changed

lib/JsonpChunkTemplatePlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
Author Tobias Koppers @sokra
44
*/
55
var ConcatSource = require("webpack-core/lib/ConcatSource");
6+
var Template = require("./Template");
67

78
function JsonpChunkTemplatePlugin() {
89
}
910
module.exports = JsonpChunkTemplatePlugin;
1011

1112
JsonpChunkTemplatePlugin.prototype.apply = function(chunkTemplate) {
1213
chunkTemplate.plugin("render", function(modules, chunk) {
13-
var jsonpFunction = this.outputOptions.jsonpFunction || ("webpackJsonp" + (this.outputOptions.library || ""));
14+
var jsonpFunction = this.outputOptions.jsonpFunction || Template.toIdentifier("webpackJsonp" + (this.outputOptions.library || ""));
1415
var source = new ConcatSource();
1516
source.add(jsonpFunction + "(" + JSON.stringify(chunk.ids) + ",");
1617
source.add(modules);

lib/JsonpHotUpdateChunkTemplatePlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
Author Tobias Koppers @sokra
44
*/
55
var ConcatSource = require("webpack-core/lib/ConcatSource");
6+
var Template = require("./Template");
7+
68

79
function JsonpHotUpdateChunkTemplatePlugin() {
810
}
911
module.exports = JsonpHotUpdateChunkTemplatePlugin;
1012

1113
JsonpHotUpdateChunkTemplatePlugin.prototype.apply = function(hotUpdateChunkTemplate) {
1214
hotUpdateChunkTemplate.plugin("render", function(modulesSource, modules, hash, id) {
13-
var jsonpFunction = this.outputOptions.hotUpdateFunction || ("webpackHotUpdate" + (this.outputOptions.library || ""));
15+
var jsonpFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
1416
var source = new ConcatSource();
1517
source.add(jsonpFunction + "(" + JSON.stringify(id) + ",");
1618
source.add(modulesSource);

lib/JsonpMainTemplatePlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
7272
});
7373
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
7474
if(chunk.chunks.length > 0) {
75-
var jsonpFunction = this.outputOptions.jsonpFunction || ("webpackJsonp" + (this.outputOptions.library || ""));
75+
var jsonpFunction = this.outputOptions.jsonpFunction || Template.toIdentifier("webpackJsonp" + (this.outputOptions.library || ""));
7676
return this.asString([
7777
source,
7878
"",
@@ -114,7 +114,7 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
114114
mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
115115
var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
116116
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
117-
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || ("webpackHotUpdate" + (this.outputOptions.library || ""));
117+
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
118118
var currentHotUpdateChunkFilename = JSON.stringify(hotUpdateChunkFilename)
119119
.replace(Template.REGEXP_HASH, "\" + " + this.renderCurrentHashCode(hash) + " + \"")
120120
.replace(Template.REGEXP_ID, "\" + chunkId + \"");

lib/Template.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,8 @@ Template.prototype.renderChunkModules = function(chunk, moduleTemplate, dependen
9797

9898
Template.getFunctionContent = function(fn) {
9999
return fn.toString().replace(/^function\s?\(\)\s?\{\n?|\n?\}$/g, "").replace(/^\t/mg, "");
100-
};
100+
};
101+
102+
Template.toIdentifier = function(str) {
103+
return str.replace(/^[^a-zA-Z$_]/,"_").replace(/[^a-zA-Z0-9$_]/g, '_');
104+
}

lib/node/NodeMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) {
9595
mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
9696
var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
9797
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
98-
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || ("webpackHotUpdate" + (this.outputOptions.library || ""));
98+
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
9999
var currentHotUpdateChunkFilename = JSON.stringify(hotUpdateChunkFilename)
100100
.replace(Template.REGEXP_HASH, "\" + " + this.renderCurrentHashCode(hash) + " + \"")
101101
.replace(Template.REGEXP_ID, "\" + chunkId + \"");

lib/webworker/WebWorkerChunkTemplatePlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
Author Tobias Koppers @sokra
44
*/
55
var ConcatSource = require("webpack-core/lib/ConcatSource");
6+
var Template = require("../Template");
67

78
function WebWorkerChunkTemplatePlugin() {
89
}
910
module.exports = WebWorkerChunkTemplatePlugin;
1011

1112
WebWorkerChunkTemplatePlugin.prototype.apply = function(chunkTemplate) {
1213
chunkTemplate.plugin("render", function(modules, chunk) {
13-
var chunkCallbackName = this.outputOptions.chunkCallbackName || ("webpackChunk" + (this.outputOptions.library || ""));
14+
var chunkCallbackName = this.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (this.outputOptions.library || ""));
1415
var source = new ConcatSource();
1516
source.add(chunkCallbackName + "(" + JSON.stringify(chunk.ids) + ",");
1617
source.add(modules);

lib/webworker/WebWorkerMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ WebWorkerMainTemplatePlugin.prototype.apply = function(mainTemplate) {
4747
});
4848
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
4949
if(chunk.chunks.length > 0) {
50-
var chunkCallbackName = this.outputOptions.chunkCallbackName || ("webpackChunk" + (this.outputOptions.library || ""));
50+
var chunkCallbackName = this.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (this.outputOptions.library || ""));
5151
return this.asString([
5252
source,
5353
"this[" + JSON.stringify(chunkCallbackName) + "] = function webpackChunkCallback(chunkIds, moreModules) {",

test/Template.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ould = require("should");
2+
var path = require("path");
3+
4+
var template = require("../lib/Template");
5+
6+
describe("Template", function() {
7+
it("should generate valid identifiers", function() {
8+
template.toIdentifier("0abc-def9").should.equal("_abc_def9");
9+
});
10+
});

0 commit comments

Comments
 (0)