hooks.js 3.0 KB

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