Skip to content

Commit c84bd70

Browse files
committed
Fixes issue with hash length and loading external chunks
If the length option on a hash was being used for a file path template (e.g. '[hash:8].js'), the file would be outputted with the correct name, but the reference inside require.ensure would not work, because the length truncation would operate on more than just the hash, and would truncate some of the characters added to pad the hash value. Fix is to implement the `hashWithLength` function where this is a problem. This doesn't feel like a proper solution, as it adds some redundancies in a few places, but it's only in the chunk loading blocks that this matters, so it's small in scope.
1 parent b02ef8b commit c84bd70

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

lib/JsonpMainTemplatePlugin.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
4343
chunkNameMap[c.id] = c.name || undefined;
4444
c.chunks.forEach(addChunk);
4545
}(chunk));
46+
var renderedHash = this.renderCurrentHashCode(hash);
4647
return this.asString([
4748
"// \"0\" is the signal for \"already loaded\"",
4849
"if(installedChunks[chunkId] === 0)",
@@ -62,7 +63,10 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
6263
"script.async = true;",
6364
"script.src = " + this.requireFn + ".p + " +
6465
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
65-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
66+
hash: "\" + " + renderedHash + " + \"",
67+
hashWithLength: function(length) {
68+
return "\" + " + renderedHash.slice(0, length) + " + \"";
69+
},
6670
chunk: {
6771
id: "\" + chunkId + \"",
6872
hash: "\" + " + JSON.stringify(chunkHashMap) + "[chunkId] + \"",
@@ -127,14 +131,21 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
127131
var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
128132
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
129133
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
134+
var renderedHash = this.renderCurrentHashCode(hash);
130135
var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
131-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
136+
hash: "\" + " + renderedHash + " + \"",
137+
hashWithLength: function(length) {
138+
return "\" + " + renderedHash.slice(0, length) + " + \"";
139+
},
132140
chunk: {
133141
id: "\" + chunkId + \""
134142
}
135143
});
136144
var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
137-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \""
145+
hash: "\" + " + renderedHash + " + \"",
146+
hashWithLength: function(length) {
147+
return "\" + " + renderedHash.slice(0, length) + " + \"";
148+
}
138149
});
139150
return source + "\n"+
140151
"var parentHotUpdateCallback = this[" + JSON.stringify(hotUpdateFunction) + "];\n" +

lib/TemplatedPathPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function replacePathVariables(path, data) {
6363
var chunkHashWithLength = chunk && chunk.hashWithLength;
6464

6565
return path
66-
.replace(REGEXP_HASH, withHashLength(getReplacer(data.hash)))
66+
.replace(REGEXP_HASH, withHashLength(getReplacer(data.hash), data.hashWithLength))
6767
.replace(REGEXP_CHUNKHASH, withHashLength(getReplacer(chunkHash), chunkHashWithLength))
6868
.replace(REGEXP_ID, getReplacer(chunkId))
6969
.replace(REGEXP_NAME, getReplacer(chunkName))

lib/node/NodeMainTemplatePlugin.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,21 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) {
9898
var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
9999
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
100100
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
101+
var renderedHash = this.renderCurrentHashCode(hash);
101102
var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
102-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
103+
hash: "\" + " + renderedHash + " + \"",
104+
hashWithLength: function(length) {
105+
return "\" + " + renderedHash.slice(0, length) + " + \"";
106+
},
103107
chunk: {
104108
id: "\" + chunkId + \""
105109
}
106110
});
107111
var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
108-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \""
112+
hash: "\" + " + renderedHash + " + \"",
113+
hashWithLength: function(length) {
114+
return "\" + " + renderedHash.slice(0, length) + " + \"";
115+
}
109116
});
110117
return Template.getFunctionContent(self.asyncChunkLoading ? function() {
111118
function hotDownloadUpdateChunk(chunkId) {

lib/webworker/WebWorkerMainTemplatePlugin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ 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);
3334
return this.asString([
3435
"// \"1\" is the signal for \"already loaded\"",
3536
"if(!installedChunks[chunkId]) {",
3637
this.indent([
3738
"importScripts(" +
3839
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
39-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
40+
hash: "\" + " + renderedHash + " + \"",
41+
hashWithLength: function(length) {
42+
return "\" + " + renderedHash.slice(0, length) + " + \"";
43+
},
4044
chunk: {
4145
id: "\" + chunkId + \""
4246
}

0 commit comments

Comments
 (0)