You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

117 lines
3.1 KiB

  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. /** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
  10. /** @typedef {import("../index.js")} HtmlWebpackPlugin */
  11. const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
  12. // The following typedef holds the API definition for all available hooks
  13. // to allow easier access when using ts-check or typescript inside plugins
  14. /** @typedef {{
  15. beforeAssetTagGeneration:
  16. AsyncSeriesWaterfallHook<{
  17. assets: {
  18. publicPath: string,
  19. js: Array<string>,
  20. css: Array<string>,
  21. favicon?: string | undefined,
  22. manifest?: string | undefined
  23. },
  24. outputName: string,
  25. plugin: HtmlWebpackPlugin
  26. }>,
  27. alterAssetTags:
  28. AsyncSeriesWaterfallHook<{
  29. assetTags: {
  30. scripts: Array<HtmlTagObject>,
  31. styles: Array<HtmlTagObject>,
  32. meta: Array<HtmlTagObject>,
  33. },
  34. outputName: string,
  35. plugin: HtmlWebpackPlugin
  36. }>,
  37. alterAssetTagGroups:
  38. AsyncSeriesWaterfallHook<{
  39. headTags: Array<HtmlTagObject | HtmlTagObject>,
  40. bodyTags: Array<HtmlTagObject | HtmlTagObject>,
  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. }} HtmlWebpackPluginHooks
  64. */
  65. /**
  66. * @type {WeakMap<WebpackCompilation, HtmlWebpackPluginHooks>}}
  67. */
  68. const htmlWebpackPluginHooksMap = new WeakMap();
  69. /**
  70. * Returns all public hooks of the html webpack plugin for the given compilation
  71. *
  72. * @param {WebpackCompilation} compilation
  73. * @returns {HtmlWebpackPluginHooks}
  74. */
  75. function getHtmlWebpackPluginHooks (compilation) {
  76. let hooks = htmlWebpackPluginHooksMap.get(compilation);
  77. // Setup the hooks only once
  78. if (hooks === undefined) {
  79. hooks = createHtmlWebpackPluginHooks();
  80. htmlWebpackPluginHooksMap.set(compilation, hooks);
  81. }
  82. return hooks;
  83. }
  84. /**
  85. * Add hooks to the webpack compilation object to allow foreign plugins to
  86. * extend the HtmlWebpackPlugin
  87. *
  88. * @returns {HtmlWebpackPluginHooks}
  89. */
  90. function createHtmlWebpackPluginHooks () {
  91. return {
  92. beforeAssetTagGeneration: new AsyncSeriesWaterfallHook(['pluginArgs']),
  93. alterAssetTags: new AsyncSeriesWaterfallHook(['pluginArgs']),
  94. alterAssetTagGroups: new AsyncSeriesWaterfallHook(['pluginArgs']),
  95. afterTemplateExecution: new AsyncSeriesWaterfallHook(['pluginArgs']),
  96. beforeEmit: new AsyncSeriesWaterfallHook(['pluginArgs']),
  97. afterEmit: new AsyncSeriesWaterfallHook(['pluginArgs'])
  98. };
  99. }
  100. module.exports = {
  101. getHtmlWebpackPluginHooks
  102. };