forked from jantimon/html-webpack-plugin
/
hooks.js
53 lines (47 loc) · 1.61 KB
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// @ts-check
/** @typedef {import("../typings").Hooks} HtmlWebpackPluginHooks */
'use strict';
/**
* This file provides access to all public htmlWebpackPlugin hooks
*/
/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
/** @typedef {import("../index.js")} HtmlWebpackPlugin */
const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
/**
* @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) {
let hooks = htmlWebpackPluginHooksMap.get(compilation);
// Setup the hooks only once
if (hooks === undefined) {
hooks = createHtmlWebpackPluginHooks();
htmlWebpackPluginHooksMap.set(compilation, hooks);
}
return hooks;
}
/**
* Add hooks to the webpack compilation object to allow foreign plugins to
* extend the HtmlWebpackPlugin
*
* @returns {HtmlWebpackPluginHooks}
*/
function createHtmlWebpackPluginHooks () {
return {
beforeAssetTagGeneration: new AsyncSeriesWaterfallHook(['pluginArgs']),
alterAssetTags: new AsyncSeriesWaterfallHook(['pluginArgs']),
alterAssetTagGroups: new AsyncSeriesWaterfallHook(['pluginArgs']),
afterTemplateExecution: new AsyncSeriesWaterfallHook(['pluginArgs']),
beforeEmit: new AsyncSeriesWaterfallHook(['pluginArgs']),
afterEmit: new AsyncSeriesWaterfallHook(['pluginArgs'])
};
}
module.exports = {
getHtmlWebpackPluginHooks
};