fixed edge case when using [chunkhash:xxx]

#427
This commit is contained in:
Tobias Koppers 2014-09-11 19:25:18 +02:00
parent dd232012ca
commit 3bc6d7b60b
2 changed files with 16 additions and 3 deletions

View File

@ -65,6 +65,14 @@ JsonpMainTemplatePlugin.prototype.apply = function(mainTemplate) {
chunk: { chunk: {
id: "\" + chunkId + \"", id: "\" + chunkId + \"",
hash: "\" + " + JSON.stringify(chunkHashMap) + "[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) + \"" name: "\" + (" + JSON.stringify(chunkNameMap) + "[chunkId]||chunkId) + \""
} }
}) + ";", }) + ";",

View File

@ -25,10 +25,14 @@ function TemplatedPathPlugin() {}
module.exports = TemplatedPathPlugin; module.exports = TemplatedPathPlugin;
function withHashLength(replacer) { function withHashLength(replacer, handlerFn) {
return function (_, hashLength) { return function (_, hashLength) {
var length = hashLength && parseInt(hashLength, 10);
if(length && handlerFn) {
return handlerFn(length);
}
var hash = replacer.apply(this, arguments); 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 chunkId = chunk && chunk.id;
var chunkName = chunk && (chunk.name || chunk.id); var chunkName = chunk && (chunk.name || chunk.id);
var chunkHash = chunk && (chunk.renderedHash || chunk.hash); var chunkHash = chunk && (chunk.renderedHash || chunk.hash);
var chunkHashWithLength = chunk && chunk.hashWithLength;
return path return path
.replace(REGEXP_HASH, withHashLength(getReplacer(data.hash))) .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_ID, getReplacer(chunkId))
.replace(REGEXP_NAME, getReplacer(chunkName)) .replace(REGEXP_NAME, getReplacer(chunkName))
.replace(REGEXP_FILE, getReplacer(data.filename)) .replace(REGEXP_FILE, getReplacer(data.filename))