hooks.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // @ts-check
  2. 'use strict';
  3. /**
  4. * This file provides access to all public htmlWebpackPlugin hooks
  5. */
  6. /** @typedef {import("webpack").Compilation} WebpackCompilation */
  7. /** @typedef {import("../index.js")} HtmlWebpackPlugin */
  8. /** @typedef {import("../typings").Hooks} HtmlWebpackPluginHooks */
  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. publicPath: string,
  33. outputName: string,
  34. plugin: HtmlWebpackPlugin
  35. }>,
  36. alterAssetTagGroups:
  37. AsyncSeriesWaterfallHook<{
  38. headTags: Array<HtmlTagObject | HtmlTagObject>,
  39. bodyTags: Array<HtmlTagObject | HtmlTagObject>,
  40. publicPath: string,
  41. outputName: string,
  42. plugin: HtmlWebpackPlugin
  43. }>,
  44. afterTemplateExecution:
  45. AsyncSeriesWaterfallHook<{
  46. html: string,
  47. headTags: Array<HtmlTagObject | HtmlTagObject>,
  48. bodyTags: Array<HtmlTagObject | HtmlTagObject>,
  49. outputName: string,
  50. plugin: HtmlWebpackPlugin,
  51. }>,
  52. beforeEmit:
  53. AsyncSeriesWaterfallHook<{
  54. html: string,
  55. outputName: string,
  56. plugin: HtmlWebpackPlugin,
  57. }>,
  58. afterEmit:
  59. AsyncSeriesWaterfallHook<{
  60. outputName: string,
  61. plugin: HtmlWebpackPlugin
  62. }>
  63. */
  64. /**
  65. * @type {WeakMap<WebpackCompilation, HtmlWebpackPluginHooks>}}
  66. */
  67. const htmlWebpackPluginHooksMap = new WeakMap();
  68. /**
  69. * Returns all public hooks of the html webpack plugin for the given compilation
  70. *
  71. * @param {WebpackCompilation} compilation
  72. * @returns {HtmlWebpackPluginHooks}
  73. */
  74. function getHtmlWebpackPluginHooks (compilation) {
  75. let hooks = htmlWebpackPluginHooksMap.get(compilation);
  76. // Setup the hooks only once
  77. if (hooks === undefined) {
  78. hooks = createHtmlWebpackPluginHooks();
  79. htmlWebpackPluginHooksMap.set(compilation, hooks);
  80. }
  81. return hooks;
  82. }
  83. /**
  84. * Add hooks to the webpack compilation object to allow foreign plugins to
  85. * extend the HtmlWebpackPlugin
  86. *
  87. * @returns {HtmlWebpackPluginHooks}
  88. */
  89. function createHtmlWebpackPluginHooks () {
  90. return {
  91. beforeAssetTagGeneration: new AsyncSeriesWaterfallHook(['pluginArgs']),
  92. alterAssetTags: new AsyncSeriesWaterfallHook(['pluginArgs']),
  93. alterAssetTagGroups: new AsyncSeriesWaterfallHook(['pluginArgs']),
  94. afterTemplateExecution: new AsyncSeriesWaterfallHook(['pluginArgs']),
  95. beforeEmit: new AsyncSeriesWaterfallHook(['pluginArgs']),
  96. afterEmit: new AsyncSeriesWaterfallHook(['pluginArgs'])
  97. };
  98. }
  99. module.exports = {
  100. getHtmlWebpackPluginHooks
  101. };