UNPKG

3.12 kBJavaScriptView Raw
1// @ts-check
2/** @typedef {import("../typings").Hooks} HtmlWebpackPluginHooks */
3'use strict';
4/**
5 * This file provides access to all public htmlWebpackPlugin hooks
6 */
7
8/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
9/** @typedef {import("../index.js")} HtmlWebpackPlugin */
10
11const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
12
13// The following is the API definition for all available hooks
14// For the TypeScript definition, see the Hooks type in typings.d.ts
15/**
16 beforeAssetTagGeneration:
17 AsyncSeriesWaterfallHook<{
18 assets: {
19 publicPath: string,
20 js: Array<string>,
21 css: Array<string>,
22 favicon?: string | undefined,
23 manifest?: string | undefined
24 },
25 outputName: string,
26 plugin: HtmlWebpackPlugin
27 }>,
28 alterAssetTags:
29 AsyncSeriesWaterfallHook<{
30 assetTags: {
31 scripts: Array<HtmlTagObject>,
32 styles: Array<HtmlTagObject>,
33 meta: Array<HtmlTagObject>,
34 },
35 publicPath: string,
36 outputName: string,
37 plugin: HtmlWebpackPlugin
38 }>,
39 alterAssetTagGroups:
40 AsyncSeriesWaterfallHook<{
41 headTags: Array<HtmlTagObject | HtmlTagObject>,
42 bodyTags: Array<HtmlTagObject | HtmlTagObject>,
43 publicPath: string,
44 outputName: string,
45 plugin: HtmlWebpackPlugin
46 }>,
47 afterTemplateExecution:
48 AsyncSeriesWaterfallHook<{
49 html: string,
50 headTags: Array<HtmlTagObject | HtmlTagObject>,
51 bodyTags: Array<HtmlTagObject | HtmlTagObject>,
52 outputName: string,
53 plugin: HtmlWebpackPlugin,
54 }>,
55 beforeEmit:
56 AsyncSeriesWaterfallHook<{
57 html: string,
58 outputName: string,
59 plugin: HtmlWebpackPlugin,
60 }>,
61 afterEmit:
62 AsyncSeriesWaterfallHook<{
63 outputName: string,
64 plugin: HtmlWebpackPlugin
65 }>
66*/
67
68/**
69 * @type {WeakMap<WebpackCompilation, HtmlWebpackPluginHooks>}}
70 */
71const htmlWebpackPluginHooksMap = new WeakMap();
72
73/**
74 * Returns all public hooks of the html webpack plugin for the given compilation
75 *
76 * @param {WebpackCompilation} compilation
77 * @returns {HtmlWebpackPluginHooks}
78 */
79function getHtmlWebpackPluginHooks (compilation) {
80 let hooks = htmlWebpackPluginHooksMap.get(compilation);
81 // Setup the hooks only once
82 if (hooks === undefined) {
83 hooks = createHtmlWebpackPluginHooks();
84 htmlWebpackPluginHooksMap.set(compilation, hooks);
85 }
86 return hooks;
87}
88
89/**
90 * Add hooks to the webpack compilation object to allow foreign plugins to
91 * extend the HtmlWebpackPlugin
92 *
93 * @returns {HtmlWebpackPluginHooks}
94 */
95function createHtmlWebpackPluginHooks () {
96 return {
97 beforeAssetTagGeneration: new AsyncSeriesWaterfallHook(['pluginArgs']),
98 alterAssetTags: new AsyncSeriesWaterfallHook(['pluginArgs']),
99 alterAssetTagGroups: new AsyncSeriesWaterfallHook(['pluginArgs']),
100 afterTemplateExecution: new AsyncSeriesWaterfallHook(['pluginArgs']),
101 beforeEmit: new AsyncSeriesWaterfallHook(['pluginArgs']),
102 afterEmit: new AsyncSeriesWaterfallHook(['pluginArgs'])
103 };
104}
105
106module.exports = {
107 getHtmlWebpackPluginHooks
108};