From 3bc6d7b60b361f3ffebf8ef505c9160cf126d27a Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 11 Sep 2014 19:25:18 +0200 Subject: [PATCH] fixed edge case when using [chunkhash:xxx] #427 --- lib/JsonpMainTemplatePlugin.js | 8 ++++++++ lib/TemplatedPathPlugin.js | 11 ++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/JsonpMainTemplatePlugin.js b/lib/JsonpMainTemplatePlugin.js index d72a4280f..95bffb38b 100644 --- a/lib/JsonpMainTemplatePlugin.js +++ b/lib/JsonpMainTemplatePlugin.js @@ -65,6 +65,14 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) { chunk: { id: "\" + chunkId + \"", hash: "\" + " + JSON.stringify(chunkHashMap) + "[chunkId] + \"", + hashWithLength: function(length) { + var shortChunkHashMap = {}; + Object.keys(chunkHashMap).forEach(function(chunkId) { + if(typeof chunkHashMap[chunkId] === "string") + shortChunkHashMap[chunkId] = chunkHashMap[chunkId].substr(0, length); + }); + return "\" + " + JSON.stringify(shortChunkHashMap) + "[chunkId] + \""; + }, name: "\" + (" + JSON.stringify(chunkNameMap) + "[chunkId]||chunkId) + \"" } }) + ";", diff --git a/lib/TemplatedPathPlugin.js b/lib/TemplatedPathPlugin.js index 11da69d30..578381f6f 100644 --- a/lib/TemplatedPathPlugin.js +++ b/lib/TemplatedPathPlugin.js @@ -25,10 +25,14 @@ function TemplatedPathPlugin() {} module.exports = TemplatedPathPlugin; -function withHashLength(replacer) { +function withHashLength(replacer, handlerFn) { return function (_, hashLength) { + var length = hashLength && parseInt(hashLength, 10); + if(length && handlerFn) { + return handlerFn(length); + } var hash = replacer.apply(this, arguments); - return hashLength ? hash.slice(0, parseInt(hashLength, 10)) : hash; + return length ? hash.slice(0, length) : hash; }; } @@ -50,10 +54,11 @@ function replacePathVariables(path, data) { var chunkId = chunk && chunk.id; var chunkName = chunk && (chunk.name || chunk.id); var chunkHash = chunk && (chunk.renderedHash || chunk.hash); + var chunkHashWithLength = chunk && chunk.hashWithLength; return path .replace(REGEXP_HASH, withHashLength(getReplacer(data.hash))) - .replace(REGEXP_CHUNKHASH, withHashLength(getReplacer(chunkHash))) + .replace(REGEXP_CHUNKHASH, withHashLength(getReplacer(chunkHash), chunkHashWithLength)) .replace(REGEXP_ID, getReplacer(chunkId)) .replace(REGEXP_NAME, getReplacer(chunkName)) .replace(REGEXP_FILE, getReplacer(data.filename))