Skip to content

Commit 3bc6d7b

Browse files
committed
fixed edge case when using [chunkhash:xxx]
webpack#427
1 parent dd23201 commit 3bc6d7b

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎lib/JsonpMainTemplatePlugin.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
6565
chunk: {
6666
id: "\" + chunkId + \"",
6767
hash: "\" + " + JSON.stringify(chunkHashMap) + "[chunkId] + \"",
68+
hashWithLength: function(length) {
69+
var shortChunkHashMap = {};
70+
Object.keys(chunkHashMap).forEach(function(chunkId) {
71+
if(typeof chunkHashMap[chunkId] === "string")
72+
shortChunkHashMap[chunkId] = chunkHashMap[chunkId].substr(0, length);
73+
});
74+
return "\" + " + JSON.stringify(shortChunkHashMap) + "[chunkId] + \"";
75+
},
6876
name: "\" + (" + JSON.stringify(chunkNameMap) + "[chunkId]||chunkId) + \""
6977
}
7078
}) + ";",

‎lib/TemplatedPathPlugin.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ function TemplatedPathPlugin() {}
2525

2626
module.exports = TemplatedPathPlugin;
2727

28-
function withHashLength(replacer) {
28+
function withHashLength(replacer, handlerFn) {
2929
return function (_, hashLength) {
30+
var length = hashLength && parseInt(hashLength, 10);
31+
if(length && handlerFn) {
32+
return handlerFn(length);
33+
}
3034
var hash = replacer.apply(this, arguments);
31-
return hashLength ? hash.slice(0, parseInt(hashLength, 10)) : hash;
35+
return length ? hash.slice(0, length) : hash;
3236
};
3337
}
3438

@@ -50,10 +54,11 @@ function replacePathVariables(path, data) {
5054
var chunkId = chunk && chunk.id;
5155
var chunkName = chunk && (chunk.name || chunk.id);
5256
var chunkHash = chunk && (chunk.renderedHash || chunk.hash);
57+
var chunkHashWithLength = chunk && chunk.hashWithLength;
5358

5459
return path
5560
.replace(REGEXP_HASH, withHashLength(getReplacer(data.hash)))
56-
.replace(REGEXP_CHUNKHASH, withHashLength(getReplacer(chunkHash)))
61+
.replace(REGEXP_CHUNKHASH, withHashLength(getReplacer(chunkHash), chunkHashWithLength))
5762
.replace(REGEXP_ID, getReplacer(chunkId))
5863
.replace(REGEXP_NAME, getReplacer(chunkName))
5964
.replace(REGEXP_FILE, getReplacer(data.filename))

0 commit comments

Comments
 (0)