From bfed3a29cc8fc10564c90015751ff0714f6924cb Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Mon, 18 Apr 2016 13:00:07 +0200 Subject: [PATCH] Add support for dynamic filenames --- CHANGELOG.md | 4 ++++ index.js | 22 ++++++++++++++++------ package.json | 2 +- spec/BasicSpec.js | 29 ++++++++++++++++++++++------- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd709da..4fe3ae1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Change History ============== +v2.16.0 +---- +* Add support for dynamic filenames like index[hash].html + v2.15.0 ---- * Add full unit test coverage for the webpack 2 beta version diff --git a/index.js b/index.js index aedabde..7753670 100644 --- a/index.js +++ b/index.js @@ -109,10 +109,14 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { // Allow plugins to make changes to the assets before invoking the template // This only makes sense to use if `inject` is `false` .then(function (compilationResult) { - return applyPluginsAsyncWaterfall('html-webpack-plugin-before-html-generation', {assets: assets, plugin: self}) - .then(function () { - return compilationResult; - }); + return applyPluginsAsyncWaterfall('html-webpack-plugin-before-html-generation', { + assets: assets, + outputName: self.childCompilationOutputName, + plugin: self + }) + .then(function () { + return compilationResult; + }); }) // Execute the template .then(function (compilationResult) { @@ -124,7 +128,7 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { }) // Allow plugins to change the html before assets are injected .then(function (html) { - var pluginArgs = {html: html, assets: assets, plugin: self}; + var pluginArgs = {html: html, assets: assets, plugin: self, outputName: self.childCompilationOutputName}; return applyPluginsAsyncWaterfall('html-webpack-plugin-before-html-processing', pluginArgs) .then(function () { return pluginArgs.html; @@ -136,7 +140,7 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { }) // Allow plugins to change the html after assets are injected .then(function (html) { - var pluginArgs = {html: html, assets: assets, plugin: self}; + var pluginArgs = {html: html, assets: assets, plugin: self, outputName: self.childCompilationOutputName}; return applyPluginsAsyncWaterfall('html-webpack-plugin-after-html-processing', pluginArgs) .then(function () { return pluginArgs.html; @@ -165,7 +169,13 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { // Let other plugins know that we are done: return applyPluginsAsyncWaterfall('html-webpack-plugin-after-emit', { html: compilation.assets[self.childCompilationOutputName], + outputName: self.childCompilationOutputName, plugin: self + }).catch(function (err) { + console.error(err); + return null; + }).then(function () { + return null; }); }) // Let webpack continue with it diff --git a/package.json b/package.json index c67c5c0..b8854bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "html-webpack-plugin", - "version": "2.15.0", + "version": "2.16.0", "description": "Simplifies creation of HTML files to serve your webpack bundles", "main": "index.js", "files": [ diff --git a/spec/BasicSpec.js b/spec/BasicSpec.js index 32b074d..bf8de01 100644 --- a/spec/BasicSpec.js +++ b/spec/BasicSpec.js @@ -38,14 +38,16 @@ 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({ - exists: outputFileExists, - filename: outputFile - }).toEqual({ - exists: true, - filename: outputFile - }); + expect(outputFileExists).toBe(true); if (!outputFileExists) { return done(); } @@ -579,6 +581,19 @@ describe('HtmlWebpackPlugin', function () { }, ['