Skip to content

Commit

Permalink
fix: add options back for events
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jan 20, 2021
1 parent c27b0b2 commit 0c5e4ff
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class HtmlWebpackPlugin {

/** @type {ProcessedHtmlWebpackOptions} */
const options = Object.assign(defaultOptions, userOptions);
this.options = options;

// Assert correct option spelling
assert(options.scriptLoading === 'defer' || options.scriptLoading === 'blocking', 'scriptLoading needs to be set to "defer" or "blocking');
Expand Down
68 changes: 68 additions & 0 deletions spec/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,74 @@ describe('HtmlWebpackPlugin', () => {
null, done, false, false);
});

it('provides the options to the afterEmit event', done => {
let eventArgs;
const examplePlugin = {
apply: function (compiler) {
compiler.hooks.compilation.tap('HtmlWebpackPlugin', compilation => {
HtmlWebpackPlugin.getHooks(compilation).afterEmit.tapAsync('HtmlWebpackPluginTest', (pluginArgs, callback) => {
eventArgs = pluginArgs;
callback(null, pluginArgs);
});
});
}
};
testHtmlPlugin({
mode: 'production',
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [
new HtmlWebpackPlugin({
foo: 'bar'
}),
examplePlugin
]
},
[/<script defer="defer" src="app_bundle.js"><\/script>[\s]*<\/head>/],
null, () => {
expect(eventArgs.plugin.options.foo).toBe('bar');
done();
}, false, false);
});

it('provides the outputName to the afterEmit event', done => {
let eventArgs;
const examplePlugin = {
apply: function (compiler) {
compiler.hooks.compilation.tap('HtmlWebpackPlugin', compilation => {
HtmlWebpackPlugin.getHooks(compilation).afterEmit.tapAsync('HtmlWebpackPluginTest', (pluginArgs, callback) => {
eventArgs = pluginArgs;
callback(null, pluginArgs);
});
});
}
};
testHtmlPlugin({
mode: 'production',
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [
new HtmlWebpackPlugin(),
examplePlugin
]
},
[/<script defer="defer" src="app_bundle.js"><\/script>[\s]*<\/head>/],
null, () => {
expect(eventArgs.outputName).toBe('index.html');
done();
}, false, false);
});

it('fires the html-webpack-plugin-after-template-execution event', done => {
let eventFired = false;
const examplePlugin = {
Expand Down

0 comments on commit 0c5e4ff

Please sign in to comment.