Expose webpack configuration to templates

This commit is contained in:
Charles Blaxland 2015-03-30 11:26:31 +11:00
parent 51c0b876dd
commit 6ae0e85202
5 changed files with 35 additions and 2 deletions

View File

@ -87,7 +87,9 @@
// jasmine helpers
"expect": false,
"describe": false,
"ddescribe": false,
"beforeEach": false,
"it": false
"it": false,
"iit": false
}
}
}

View File

@ -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`).

View File

@ -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';

View File

@ -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);
});
});

11
spec/fixtures/webpackconfig.html vendored Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>Test</title>
</head>
<body>
<p>Public path is {%=o.webpackConfig.output.publicPath%}</p>
<script src="{%=o.htmlWebpackPlugin.files.chunks.app.entry%}"></script>
</body>
</html>