From 6ae0e8520248fae25439fcd012e87e29100cf1b0 Mon Sep 17 00:00:00 2001 From: Charles Blaxland Date: Mon, 30 Mar 2015 11:26:31 +1100 Subject: [PATCH] Expose webpack configuration to templates --- .jshintrc | 6 ++++-- README.md | 3 +++ index.js | 2 ++ spec/HtmlWebpackPluginSpec.js | 15 +++++++++++++++ spec/fixtures/webpackconfig.html | 11 +++++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/webpackconfig.html diff --git a/.jshintrc b/.jshintrc index e21bdca..441ab5e 100644 --- a/.jshintrc +++ b/.jshintrc @@ -87,7 +87,9 @@ // jasmine helpers "expect": false, "describe": false, + "ddescribe": false, "beforeEach": false, - "it": false + "it": false, + "iit": false } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 21a5c2e..1fe34e4 100644 --- a/README.md +++ b/README.md @@ -185,3 +185,6 @@ template is rendered. This variable has the following attributes: object. Note that this is the stats object as it was at the time the HTML template was emitted and as such may not have the full set of stats that are available after the wepback run is complete. + +- `webpackConfig`: the webpack configuration that was used for this compilation. This + can be used, for example, to get the `publicPath` (`webpackConfig.output.publicPath`). diff --git a/index.js b/index.js index 5a4fb9f..9406a3b 100644 --- a/index.js +++ b/index.js @@ -17,8 +17,10 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) { templateParams.htmlWebpackPlugin.assets = self.htmlWebpackPluginLegacyAssets(compilation, webpackStatsJson); templateParams.htmlWebpackPlugin.files = self.htmlWebpackPluginAssets(compilation, webpackStatsJson); templateParams.htmlWebpackPlugin.options = self.options; + // If the hash option is true append the webpack hash to all assets templateParams.htmlWebpackPlugin.querystring = self.options.hash ? '?' + webpackStatsJson.hash : ''; + templateParams.webpackConfig = compilation.options; var outputFilename = self.options.filename || 'index.html'; diff --git a/spec/HtmlWebpackPluginSpec.js b/spec/HtmlWebpackPluginSpec.js index 45e5981..f5be671 100644 --- a/spec/HtmlWebpackPluginSpec.js +++ b/spec/HtmlWebpackPluginSpec.js @@ -284,4 +284,19 @@ describe('HtmlWebpackPlugin', function() { }); }); + it('exposes the webpack configuration to templates', function(done) { + testHtmlPlugin({ + entry: { + app: path.join(__dirname, 'fixtures/index.js') + }, + output: { + path: OUTPUT_DIR, + publicPath: 'https://cdn.com', + filename: '[name]_bundle.js' + }, + plugins: [new HtmlWebpackPlugin({template: path.join(__dirname, 'fixtures/webpackconfig.html')})] + }, + ['Public path is https://cdn.com'], null, done); + }); + }); diff --git a/spec/fixtures/webpackconfig.html b/spec/fixtures/webpackconfig.html new file mode 100644 index 0000000..122c9b5 --- /dev/null +++ b/spec/fixtures/webpackconfig.html @@ -0,0 +1,11 @@ + + + + + Test + + +

Public path is {%=o.webpackConfig.output.publicPath%}

+ + +