From af6673800818d9bdb4d94461e6a6ae5049b5c359 Mon Sep 17 00:00:00 2001 From: Christian Weiss Date: Fri, 1 Apr 2016 10:34:59 -0700 Subject: [PATCH 1/2] add support for [hash] in filename --- index.js | 14 ++++++++++---- spec/BasicSpec.js | 21 +++++++++++++++++++++ spec/CachingSpec.js | 24 +++++++++++++++++------- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 762d0962..0931ddc6 100644 --- a/index.js +++ b/index.js @@ -43,8 +43,12 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { } compiler.plugin('make', function (compilation, callback) { + // Remove occurences of '[hash]' in the filename so the child + // compiler does not try to replace it. + var escapedFilename = self.options.filename.replace('[hash]', ''); + // Compile the template (queued) - compilationPromise = childCompiler.compileTemplate(self.options.template, compiler.context, self.options.filename, compilation) + compilationPromise = childCompiler.compileTemplate(self.options.template, compiler.context, escapedFilename, compilation) .catch(function (err) { compilation.errors.push(prettyError(err, compiler.context).toString()); return { @@ -149,8 +153,9 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { return self.options.showErrors ? prettyError(err, compiler.context).toHtml() : 'ERROR'; }) .then(function (html) { + var outputFilename = self.options.filename.replace('[hash]', self.childCompilerHash); // Replace the compilation result with the evaluated html code - compilation.assets[self.options.filename] = { + compilation.assets[outputFilename] = { source: function () { return html; }, @@ -158,11 +163,12 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { return html.length; } }; + return outputFilename; }) - .then(function () { + .then(function (outputFilename) { // Let other plugins know that we are done: return applyPluginsAsyncWaterfall('html-webpack-plugin-after-emit', { - html: compilation.assets[self.options.filename], + html: compilation.assets[outputFilename], plugin: self }); }) diff --git a/spec/BasicSpec.js b/spec/BasicSpec.js index fc70fd09..bf8de01c 100644 --- a/spec/BasicSpec.js +++ b/spec/BasicSpec.js @@ -38,6 +38,14 @@ function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expec } else { expect(compilationWarnings).toBe(''); } + if (outputFile instanceof RegExp) { + var matches = Object.keys(stats.compilation.assets).filter(function (item) { + return outputFile.test(item); + }); + expect(matches.length).toBe(1); + outputFile = matches[0]; + } + expect(outputFile.indexOf('[hash]') === -1).toBe(true); var outputFileExists = fs.existsSync(path.join(OUTPUT_DIR, outputFile)); expect(outputFileExists).toBe(true); if (!outputFileExists) { @@ -573,6 +581,19 @@ describe('HtmlWebpackPlugin', function () { }, ['