Skip to content

Commit d5dc904

Browse files
committed
fixed incomplete hash bug
1 parent 8c606b4 commit d5dc904

File tree

7 files changed

+39
-40
lines changed

7 files changed

+39
-40
lines changed

lib/HotModuleReplacementPlugin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
144144
return true;
145145
});
146146

147-
compilation.mainTemplate.plugin("current-hash", function() {
148-
return "hotCurrentHash";
147+
compilation.mainTemplate.plugin("current-hash", function(_, length) {
148+
if(isFinite(length))
149+
return "hotCurrentHash.substr(0, " + length + ")";
150+
else
151+
return "hotCurrentHash";
149152
});
150153

151154
compilation.mainTemplate.plugin("module-obj", function(source, chunk, hash, varModuleId) {

lib/JsonpMainTemplatePlugin.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
3333
var filename = this.outputOptions.filename || "bundle.js";
3434
var chunkFilename = this.outputOptions.chunkFilename || "[id]." + filename;
3535
var chunkMaps = chunk.getChunkMaps();
36-
var renderedHash = this.renderCurrentHashCode(hash);
3736
return this.asString([
3837
"// \"0\" is the signal for \"already loaded\"",
3938
"if(installedChunks[chunkId] === 0)",
@@ -53,10 +52,10 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
5352
"script.async = true;",
5453
"script.src = " + this.requireFn + ".p + " +
5554
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
56-
hash: "\" + " + renderedHash + " + \"",
55+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
5756
hashWithLength: function(length) {
58-
return "\" + " + renderedHash.slice(0, length) + " + \"";
59-
},
57+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
58+
}.bind(this),
6059
chunk: {
6160
id: "\" + chunkId + \"",
6261
hash: "\" + " + JSON.stringify(chunkMaps.hash) + "[chunkId] + \"",
@@ -121,21 +120,20 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
121120
var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
122121
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
123122
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
124-
var renderedHash = this.renderCurrentHashCode(hash);
125123
var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
126-
hash: "\" + " + renderedHash + " + \"",
124+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
127125
hashWithLength: function(length) {
128-
return "\" + " + renderedHash.slice(0, length) + " + \"";
129-
},
126+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
127+
}.bind(this),
130128
chunk: {
131129
id: "\" + chunkId + \""
132130
}
133131
});
134132
var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
135-
hash: "\" + " + renderedHash + " + \"",
133+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
136134
hashWithLength: function(length) {
137-
return "\" + " + renderedHash.slice(0, length) + " + \"";
138-
}
135+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
136+
}.bind(this)
139137
});
140138
return source + "\n"+
141139
"var parentHotUpdateCallback = this[" + JSON.stringify(hotUpdateFunction) + "];\n" +

lib/MainTemplate.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ MainTemplate.prototype.renderAddModule = function(hash, chunk, varModuleId, varM
123123
return this.applyPluginsWaterfall("add-module", "modules[" + varModuleId + "] = " + varModule + ";", chunk, hash, varModuleId, varModule);
124124
};
125125

126-
MainTemplate.prototype.renderCurrentHashCode = function(hash) {
127-
return this.applyPluginsWaterfall("current-hash", JSON.stringify(hash));
126+
MainTemplate.prototype.renderCurrentHashCode = function(hash, length) {
127+
length = length || Infinity;
128+
return this.applyPluginsWaterfall("current-hash", JSON.stringify(hash.substr(0, length)), length);
128129
};
129130

130131
MainTemplate.prototype.entryPointInChildren = function(chunk) {

lib/node/NodeMainTemplatePlugin.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) {
3232
var filename = this.outputOptions.filename || "bundle.js";
3333
var chunkFilename = this.outputOptions.chunkFilename || "[id]." + filename;
3434
var chunkMaps = chunk.getChunkMaps();
35-
var renderedHash = this.renderCurrentHashCode(hash);
3635
var insertMoreModules = [
3736
"var moreModules = chunk.modules, chunkIds = chunk.ids;",
3837
"for(var moduleId in moreModules) {",
@@ -46,10 +45,10 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) {
4645
this.indent([
4746
"installedChunks[chunkId] = [callback];",
4847
"var filename = __dirname + " + this.applyPluginsWaterfall("asset-path", JSON.stringify("/" + chunkFilename), {
49-
hash: "\" + " + renderedHash + " + \"",
48+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
5049
hashWithLength: function(length) {
51-
return "\" + " + renderedHash.slice(0, length) + " + \"";
52-
},
50+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
51+
}.bind(this),
5352
chunk: {
5453
id: "\" + chunkId + \"",
5554
hash: "\" + " + JSON.stringify(chunkMaps.hash) + "[chunkId] + \"",
@@ -90,10 +89,10 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) {
9089
]);
9190
} else {
9291
var request = this.applyPluginsWaterfall("asset-path", JSON.stringify("./" + chunkFilename), {
93-
hash: "\" + " + renderedHash + " + \"",
92+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
9493
hashWithLength: function(length) {
95-
return "\" + " + renderedHash.slice(0, length) + " + \"";
96-
},
94+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
95+
}.bind(this),
9796
chunk: {
9897
id: "\" + chunkId + \"",
9998
hash: "\" + " + JSON.stringify(chunkMaps.hash) + "[chunkId] + \"",
@@ -127,12 +126,11 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) {
127126
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
128127
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
129128
var chunkMaps = chunk.getChunkMaps();
130-
var renderedHash = this.renderCurrentHashCode(hash);
131129
var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
132-
hash: "\" + " + renderedHash + " + \"",
130+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
133131
hashWithLength: function(length) {
134-
return "\" + " + renderedHash.slice(0, length) + " + \"";
135-
},
132+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
133+
}.bind(this),
136134
chunk: {
137135
id: "\" + chunkId + \"",
138136
hash: "\" + " + JSON.stringify(chunkMaps.hash) + "[chunkId] + \"",
@@ -148,10 +146,10 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) {
148146
}
149147
});
150148
var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
151-
hash: "\" + " + renderedHash + " + \"",
149+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
152150
hashWithLength: function(length) {
153-
return "\" + " + renderedHash.slice(0, length) + " + \"";
154-
}
151+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
152+
}.bind(this)
155153
});
156154
return Template.getFunctionContent(self.asyncChunkLoading ? function() {
157155
function hotDownloadUpdateChunk(chunkId) {

lib/webworker/WebWorkerMainTemplatePlugin.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ WebWorkerMainTemplatePlugin.prototype.apply = function(mainTemplate) {
3030
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
3131
var filename = this.outputOptions.filename || "bundle.js";
3232
var chunkFilename = this.outputOptions.chunkFilename || "[id]." + filename;
33-
var renderedHash = this.renderCurrentHashCode(hash);
3433
return this.asString([
3534
"// \"1\" is the signal for \"already loaded\"",
3635
"if(!installedChunks[chunkId]) {",
3736
this.indent([
3837
"importScripts(" +
3938
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
40-
hash: "\" + " + renderedHash + " + \"",
39+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
4140
hashWithLength: function(length) {
42-
return "\" + " + renderedHash.slice(0, length) + " + \"";
43-
},
41+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
42+
}.bind(this),
4443
chunk: {
4544
id: "\" + chunkId + \""
4645
}

test/configCases/hash-length/output-filename/test.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
var files = fs.readdirSync(options.output.path);
66
var hashParamMatches = options.output.filename.match(/:(\d+)/);
77
var hashLength = hashParamMatches && hashParamMatches[1];
8-
var bundleDetect = new RegExp("^bundle\.[a-z0-9]" + (hashLength ? "{" + hashLength + "}" : "{16,}") + "\.js$", "i");
8+
var bundleDetect = new RegExp("^bundle" + i, "i");
99
for (var i = 0, file; i < files.length; i++) {
1010
file = files[i];
1111
if (bundleDetect.test(file)) {
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
module.exports = [{
22
output: {
3-
filename: 'bundle.[hash:6].js'
3+
filename: 'bundle0.[hash:6].js'
44
}
55
}, {
66
output: {
7-
filename: 'bundle.[hash].js'
7+
filename: 'bundle1.[hash].js'
88
}
99
}, {
1010
output: {
11-
filename: 'bundle.[chunkhash:8].js',
11+
filename: 'bundle2.[chunkhash:8].js',
1212
chunkFilename: '[id].bundle.[chunkhash:8].js'
1313
}
1414
}, {
1515
output: {
16-
filename: 'bundle.[chunkhash].js',
16+
filename: 'bundle3.[chunkhash].js',
1717
chunkFilename: '[id].bundle.[chunkhash].js'
1818
}
1919
}, {
2020
output: {
21-
filename: 'bundle.[hash].js',
21+
filename: 'bundle4.[hash].js',
2222
chunkFilename: '[id].bundle.[hash:8].js'
2323
}
2424
}, {
2525
output: {
26-
filename: 'bundle.[hash:6].js',
26+
filename: 'bundle5.[hash:6].js',
2727
chunkFilename: '[id].bundle.[hash:8].js'
2828
}
2929
}];

0 commit comments

Comments
 (0)