From 3897896265f37e37dd88c229c63ecff6cff34276 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 27 Feb 2016 18:26:51 +0100 Subject: [PATCH] Add before-html-generation event This allows you to add assets to the html when not using injection --- README.md | 1 + index.js | 10 +++++++++- spec/HtmlWebpackPluginSpec.js | 36 +++++++++++++++++++++++++++++++++- spec/fixtures/custom_file.html | 16 +++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/custom_file.html diff --git a/README.md b/README.md index 10eabfb..1a4984d 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,7 @@ Events To allow other plugins to alter the html this plugin executes the following events: + * `html-webpack-plugin-before-html-generation` * `html-webpack-plugin-before-html-processing` * `html-webpack-plugin-after-html-processing` * `html-webpack-plugin-after-emit` diff --git a/index.js b/index.js index 1af1f30..7714d4e 100644 --- a/index.js +++ b/index.js @@ -96,9 +96,17 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { // and replace it with its content return self.evaluateCompilationResult(compilation, compiledTemplate); }) + // 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; + }); + }) // Execute the template .then(function (compilationResult) { - // If the loader result is a function execute it to retreive the html + // If the loader result is a function execute it to retrieve the html // otherwise use the returned html return typeof compilationResult !== 'function' ? compilationResult diff --git a/spec/HtmlWebpackPluginSpec.js b/spec/HtmlWebpackPluginSpec.js index 58b5167..bd567f8 100644 --- a/spec/HtmlWebpackPluginSpec.js +++ b/spec/HtmlWebpackPluginSpec.js @@ -1,4 +1,4 @@ -/* global describe, it, beforeEach, expect */ +/* eslint-env jasmine */ 'use strict'; // Workaround for css-loader issue @@ -772,6 +772,40 @@ describe('HtmlWebpackPlugin', function () { }); }); + it('allows to modify the html during html-webpack-plugin-before-html-generation event', function (done) { + var eventFired = false; + var examplePlugin = { + apply: function (compiler) { + compiler.plugin('compilation', function (compilation) { + compilation.plugin('html-webpack-plugin-before-html-generation', function (object, callback) { + eventFired = true; + object.assets.js.push('funky-script.js'); + callback(); + }); + }); + } + }; + testHtmlPlugin({ + entry: { + app: path.join(__dirname, 'fixtures/index.js') + }, + output: { + path: OUTPUT_DIR, + filename: '[name]_bundle.js' + }, + plugins: [ + new HtmlWebpackPlugin({ + inject: false, + template: 'underscore-template-loader!' + path.join(__dirname, 'fixtures/custom_file.html') + }), + examplePlugin + ] + }, [' + <% } %> + + +