Skip to content

Commit

Permalink
feat(hooks): Provide static getHook method for access to all html-web…
Browse files Browse the repository at this point in the history
…pack-plugin hooks

BREAKING CHANGE: The html-webpack-plugin doesn't add its hooks to the compilation object anymore
  • Loading branch information
jantimon committed Jul 6, 2018
1 parent bbc07a3 commit 59c8e8f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -308,8 +308,7 @@ function MyPlugin(options) {
MyPlugin.prototype.apply = function (compiler) {
compiler.hooks.compilation.tap('MyPlugin', (compilation) => {
console.log('The compiler is starting a new compilation...');

compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(
HtmlWebpackPlugin.getHook(compilation, 'htmlWebpackPluginAfterHtmlProcessing').tapAsync(
'MyPlugin',
(data, cb) => {
data.html += 'The Magic Footer'
Expand Down
21 changes: 16 additions & 5 deletions index.js
Expand Up @@ -28,7 +28,7 @@ const fsReadFileAsync = promisify(fs.readFile);

class HtmlWebpackPlugin {
/**
* @param {Partial<HtmlWebpackPluginOptions>} options
* @param {Partial<HtmlWebpackPluginOptions>} [options]
*/
constructor (options) {
// Default options
Expand Down Expand Up @@ -60,10 +60,7 @@ class HtmlWebpackPlugin {
this.childCompilationOutputName = undefined;
this.assetJson = undefined;
this.hash = undefined;
/**
* The major version number of this plugin
*/
this.version = 4;
this.version = HtmlWebpackPlugin.version;
}

/**
Expand Down Expand Up @@ -697,4 +694,18 @@ function templateParametersGenerator (compilation, assets, options) {
}
};
}

// Statics:
/**
* The major version number of this plugin
*/
HtmlWebpackPlugin.version = 4;

/**
* A static helper to get the hooks for this plugin
*
* Usage: HtmlWebpackPlugin.getHook(compilation, 'HookName').tap('YourPluginName', () => { ... });
*/
HtmlWebpackPlugin.getHook = getHtmlWebpackPluginHook;

module.exports = HtmlWebpackPlugin;
1 change: 0 additions & 1 deletion lib/compiler.js
Expand Up @@ -22,7 +22,6 @@ const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
* for multile HtmlWebpackPlugin instances to improve the compilation performance.
*/
class HtmlWebpackChildCompiler {

constructor () {
/**
* @type {string[]} templateIds
Expand Down
43 changes: 18 additions & 25 deletions lib/hooks.js
Expand Up @@ -5,18 +5,6 @@
'use strict';
/**
* This file provides access to all public htmlWebpackPlugin hooks
*
* Usage:
* ```js
* const getHtmlWebpackPluginHooks = require('html-webpack-plugin/lib/hooks').getHtmlWebpackPluginHooks;
*
* compiler.hooks.compilation.tap('YOUR_PLUGIN_NAME', (compilation) => {
* const htmlWebpackPluginHooks = getHtmlWebpackPluginHooks(compilation);
* htmlWebpackPluginHooks.htmlWebpackPluginAfterEmit.tap('YOUR_PLUGIN_NAME', (pluginArgs) => {
* // your code
* });
* });
* ```
*/

/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
Expand Down Expand Up @@ -75,18 +63,23 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
}} HtmlWebpackPluginHooks
*/

/**
* @type {WeakMap<WebpackCompilation, HtmlWebpackPluginHooks>}}
*/
const htmlWebpackPluginHooksMap = new WeakMap();

/**
* Returns all public hooks of the html webpack plugin for the given compilation
*
* @param {WebpackCompilation} compilation
* @returns {HtmlWebpackPluginHooks}
*/
function getHtmlWebpackPluginHooks (compilation) {
/** @type {HtmlWebpackPluginHooks} */
const hooks = compilation.hooks;
let hooks = htmlWebpackPluginHooksMap.get(compilation);
// Setup the hooks only once
if (!hooks.htmlWebpackPluginAfterEmit) {
attachHooksToCompilation(compilation);
if (hooks === undefined) {
hooks = createHtmlWebpackPluginHooks();
htmlWebpackPluginHooksMap.set(compilation, hooks);
}
return {
htmlWebpackPluginBeforeHtmlGeneration: hooks.htmlWebpackPluginBeforeHtmlGeneration,
Expand All @@ -101,16 +94,16 @@ function getHtmlWebpackPluginHooks (compilation) {
* Add hooks to the webpack compilation object to allow foreign plugins to
* extend the HtmlWebpackPlugin
*
* @param {WebpackCompilation} compilation
* @returns {HtmlWebpackPluginHooks}
*/
function attachHooksToCompilation (compilation) {
/** @type {HtmlWebpackPluginHooks} */
const hooks = compilation.hooks;
hooks.htmlWebpackPluginBeforeHtmlGeneration = new AsyncSeriesWaterfallHook(['pluginArgs']);
hooks.htmlWebpackPluginBeforeHtmlProcessing = new AsyncSeriesWaterfallHook(['pluginArgs']);
hooks.htmlWebpackPluginAlterAssetTags = new AsyncSeriesWaterfallHook(['pluginArgs']);
hooks.htmlWebpackPluginAfterHtmlProcessing = new AsyncSeriesWaterfallHook(['pluginArgs']);
hooks.htmlWebpackPluginAfterEmit = new AsyncSeriesWaterfallHook(['pluginArgs']);
function createHtmlWebpackPluginHooks () {
return {
htmlWebpackPluginBeforeHtmlGeneration: new AsyncSeriesWaterfallHook(['pluginArgs']),
htmlWebpackPluginBeforeHtmlProcessing: new AsyncSeriesWaterfallHook(['pluginArgs']),
htmlWebpackPluginAlterAssetTags: new AsyncSeriesWaterfallHook(['pluginArgs']),
htmlWebpackPluginAfterHtmlProcessing: new AsyncSeriesWaterfallHook(['pluginArgs']),
htmlWebpackPluginAfterEmit: new AsyncSeriesWaterfallHook(['pluginArgs'])
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion spec/BasicSpec.js
Expand Up @@ -136,7 +136,7 @@ function getChunksInfoFromStats (stats) {
function tapCompilationEvent (compilation, eventName, handler) {
// Webpack 4 has a new interface
if (compilation.hooks) {
return compilation.hooks[trainCaseToCamelCase(eventName)].tapAsync(
return HtmlWebpackPlugin.getHook(compilation, trainCaseToCamelCase(eventName)).tapAsync(
'AsyncPlugin' + tapCompilationEvent.counter++,
handler
);
Expand Down
2 changes: 1 addition & 1 deletion spec/ExampleSpec.js
Expand Up @@ -3,7 +3,7 @@
* and matches them against their dist folder
*/

/* eslint-env jasmine */
/* eslint-env jasmine */
'use strict';

var path = require('path');
Expand Down

0 comments on commit 59c8e8f

Please sign in to comment.