Merge pull request #240 from SimenB/event-before-function-invocation
Add before-html-generation event
This commit is contained in:
commit
f707c98d32
|
|
@ -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`
|
||||
|
|
|
|||
10
index.js
10
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
]
|
||||
}, ['<script src="funky-script.js"'], null, function () {
|
||||
expect(eventFired).toBe(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('works with commons chunk plugin', function (done) {
|
||||
testHtmlPlugin({
|
||||
debug: true,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
|
||||
<title>Test</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Some unique text</p>
|
||||
<% for(var i = 0; i <htmlWebpackPlugin.files.js.length; i++) {
|
||||
var filename = htmlWebpackPlugin.files.js[i];
|
||||
if (filename.indexOf('funky-script') < 0) continue;%>
|
||||
<script src="<%=filename%>"></script>
|
||||
<% } %>
|
||||
<script src="<%=htmlWebpackPlugin.files.chunks.app.entry%>"></script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue