hooks.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // @ts-check
  2. /* eslint-disable */
  3. /// <reference path="../typings.d.ts" />
  4. /* eslint-enable */
  5. 'use strict';
  6. /**
  7. * This file provides access to all public htmlWebpackPlugin hooks
  8. *
  9. * Usage:
  10. * ```js
  11. * const getHtmlWebpackPluginHooks = require('html-webpack-plugin/lib/hooks').getHtmlWebpackPluginHooks;
  12. *
  13. * compiler.hooks.compilation.tap('YOUR_PLUGIN_NAME', (compilation) => {
  14. * const htmlWebpackPluginHooks = getHtmlWebpackPluginHooks(compilation);
  15. * htmlWebpackPluginHooks.htmlWebpackPluginAfterEmit.tap('YOUR_PLUGIN_NAME', (pluginArgs) => {
  16. * // your code
  17. * });
  18. * });
  19. * ```
  20. */
  21. /** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
  22. /** @typedef {import("../index.js")} HtmlWebpackPlugin */
  23. const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
  24. // The following typedef holds the API definition for all available hooks
  25. // to allow easier access when using ts-check or typescript inside plugins
  26. /** @typedef {{
  27. htmlWebpackPluginBeforeHtmlGeneration:
  28. AsyncSeriesWaterfallHook<{
  29. assets: {
  30. publicPath: string,
  31. js: Array<{entryName: string, path: string}>,
  32. css: Array<{entryName: string, path: string}>,
  33. },
  34. outputName: string,
  35. plugin: HtmlWebpackPlugin
  36. }>,
  37. htmlWebpackPluginBeforeHtmlProcessing:
  38. AsyncSeriesWaterfallHook<{
  39. html: string,
  40. assets: {
  41. publicPath: string,
  42. js: Array<{entryName: string, path: string}>,
  43. css: Array<{entryName: string, path: string}>,
  44. },
  45. outputName: string,
  46. plugin: HtmlWebpackPlugin,
  47. }>,
  48. htmlWebpackPluginAfterHtmlProcessing:
  49. AsyncSeriesWaterfallHook<{
  50. html: string,
  51. assets: {
  52. publicPath: string,
  53. js: Array<{entryName: string, path: string}>,
  54. css: Array<{entryName: string, path: string}>,
  55. },
  56. outputName: string,
  57. plugin: HtmlWebpackPlugin,
  58. }>,
  59. htmlWebpackPluginAlterAssetTags:
  60. AsyncSeriesWaterfallHook<{
  61. head: Array<HtmlTagObject>,
  62. body: Array<HtmlTagObject>,
  63. outputName: string,
  64. plugin: HtmlWebpackPlugin
  65. }>,
  66. htmlWebpackPluginAfterEmit:
  67. AsyncSeriesWaterfallHook<{
  68. html: string,
  69. outputName: string,
  70. plugin: HtmlWebpackPlugin
  71. }>,
  72. }} HtmlWebpackPluginHooks
  73. */
  74. /**
  75. * Returns all public hooks of the html webpack plugin for the given compilation
  76. *
  77. * @param {WebpackCompilation} compilation
  78. * @returns {HtmlWebpackPluginHooks}
  79. */
  80. function getHtmlWebpackPluginHooks (compilation) {
  81. /** @type {HtmlWebpackPluginHooks} */
  82. const hooks = compilation.hooks;
  83. // Setup the hooks only once
  84. if (!hooks.htmlWebpackPluginAfterEmit) {
  85. attachHooksToCompilation(compilation);
  86. }
  87. return {
  88. htmlWebpackPluginBeforeHtmlGeneration: hooks.htmlWebpackPluginBeforeHtmlGeneration,
  89. htmlWebpackPluginBeforeHtmlProcessing: hooks.htmlWebpackPluginBeforeHtmlProcessing,
  90. htmlWebpackPluginAlterAssetTags: hooks.htmlWebpackPluginAlterAssetTags,
  91. htmlWebpackPluginAfterHtmlProcessing: hooks.htmlWebpackPluginAfterHtmlProcessing,
  92. htmlWebpackPluginAfterEmit: hooks.htmlWebpackPluginAfterEmit
  93. };
  94. }
  95. /**
  96. * Add hooks to the webpack compilation object to allow foreign plugins to
  97. * extend the HtmlWebpackPlugin
  98. *
  99. * @param {WebpackCompilation} compilation
  100. */
  101. function attachHooksToCompilation (compilation) {
  102. /** @type {HtmlWebpackPluginHooks} */
  103. const hooks = compilation.hooks;
  104. hooks.htmlWebpackPluginBeforeHtmlGeneration = new AsyncSeriesWaterfallHook(['pluginArgs']);
  105. hooks.htmlWebpackPluginBeforeHtmlProcessing = new AsyncSeriesWaterfallHook(['pluginArgs']);
  106. hooks.htmlWebpackPluginAlterAssetTags = new AsyncSeriesWaterfallHook(['pluginArgs']);
  107. hooks.htmlWebpackPluginAfterHtmlProcessing = new AsyncSeriesWaterfallHook(['pluginArgs']);
  108. hooks.htmlWebpackPluginAfterEmit = new AsyncSeriesWaterfallHook(['pluginArgs']);
  109. }
  110. /**
  111. * Small workaround helper to work around https://github.com/Microsoft/TypeScript/issues/1178
  112. * Returns the hook of the given name
  113. *
  114. * @type {
  115. <T extends keyof HtmlWebpackPluginHooks>(compilation: WebpackCompilation, hookName: T) => HtmlWebpackPluginHooks[T]
  116. }
  117. */
  118. const getHtmlWebpackPluginHook = (compilation, hookName) => {
  119. const hooks = getHtmlWebpackPluginHooks(compilation);
  120. return /** @type {any} */hooks[hookName];
  121. };
  122. module.exports = {
  123. getHtmlWebpackPluginHooks,
  124. getHtmlWebpackPluginHook
  125. };