typings.d.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { AsyncSeriesWaterfallHook } from "tapable";
  2. import { Compiler, Compilation } from "webpack";
  3. import { Options as HtmlMinifierOptions } from "html-minifier-terser";
  4. export = HtmlWebpackPlugin;
  5. declare class HtmlWebpackPlugin {
  6. constructor(options?: HtmlWebpackPlugin.Options);
  7. userOptions: HtmlWebpackPlugin.Options;
  8. /** Current HtmlWebpackPlugin Major */
  9. version: number;
  10. /**
  11. * Options after html-webpack-plugin has been initialized with defaults
  12. */
  13. options?: HtmlWebpackPlugin.ProcessedOptions;
  14. apply(compiler: Compiler): void;
  15. static getHooks(compilation: Compilation): HtmlWebpackPlugin.Hooks;
  16. /**
  17. * Static helper to create a tag object to be get injected into the dom
  18. */
  19. static createHtmlTagObject(
  20. tagName: string,
  21. attributes?: { [attributeName: string]: string | boolean },
  22. innerHTML?: string
  23. ): HtmlWebpackPlugin.HtmlTagObject;
  24. static readonly version: number;
  25. }
  26. declare namespace HtmlWebpackPlugin {
  27. type MinifyOptions = HtmlMinifierOptions;
  28. interface Options {
  29. /**
  30. * Emit the file only if it was changed.
  31. * @default true
  32. */
  33. cache?: boolean;
  34. /**
  35. * List all entries which should be injected
  36. */
  37. chunks?: "all" | string[];
  38. /**
  39. * Allows to control how chunks should be sorted before they are included to the html.
  40. * @default 'auto'
  41. */
  42. chunksSortMode?:
  43. | "auto"
  44. | "manual"
  45. | ((entryNameA: string, entryNameB: string) => number);
  46. /**
  47. * List all entries which should not be injected
  48. */
  49. excludeChunks?: string[];
  50. /**
  51. * Path to the favicon icon
  52. */
  53. favicon?: false | string;
  54. /**
  55. * The file to write the HTML to.
  56. * Supports subdirectories eg: `assets/admin.html`
  57. * [name] will be replaced by the entry name
  58. * Supports a function to generate the name
  59. *
  60. * @default 'index.html'
  61. */
  62. filename?: string | ((entryName: string) => string);
  63. /**
  64. * By default the public path is set to `auto` - that way the html-webpack-plugin will try
  65. * to set the publicPath according to the current filename and the webpack publicPath setting
  66. */
  67. publicPath?: string | "auto";
  68. /**
  69. * If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files.
  70. * This is useful for cache busting
  71. */
  72. hash?: boolean;
  73. /**
  74. * Inject all assets into the given `template` or `templateContent`.
  75. */
  76. inject?:
  77. | false // Don't inject scripts
  78. | true // Inject scripts into body
  79. | "body" // Inject scripts into body
  80. | "head"; // Inject scripts into head
  81. /**
  82. * Set up script loading
  83. * blocking will result in <script src="..."></script>
  84. * defer will result in <script defer src="..."></script>
  85. *
  86. * @default 'defer'
  87. */
  88. scriptLoading?: "blocking" | "defer";
  89. /**
  90. * Inject meta tags
  91. */
  92. meta?:
  93. | false // Disable injection
  94. | {
  95. [name: string]:
  96. | string
  97. | false // name content pair e.g. {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`
  98. | { [attributeName: string]: string | boolean }; // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" }
  99. };
  100. /**
  101. * HTML Minification options accepts the following values:
  102. * - Set to `false` to disable minifcation
  103. * - Set to `'auto'` to enable minifcation only for production mode
  104. * - Set to custom minification according to
  105. * {@link https://github.com/kangax/html-minifier#options-quick-reference}
  106. */
  107. minify?: "auto" | boolean | MinifyOptions;
  108. /**
  109. * Render errors into the HTML page
  110. */
  111. showErrors?: boolean;
  112. /**
  113. * The `webpack` require path to the template.
  114. * @see https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md
  115. */
  116. template?: string;
  117. /**
  118. * Allow to use a html string instead of reading from a file
  119. */
  120. templateContent?:
  121. | false // Use the template option instead to load a file
  122. | string
  123. | ((templateParameters: {
  124. [option: string]: any;
  125. }) => string | Promise<string>)
  126. | Promise<string>;
  127. /**
  128. * Allows to overwrite the parameters used in the template
  129. */
  130. templateParameters?:
  131. | false // Pass an empty object to the template function
  132. | ((
  133. compilation: any,
  134. assets: {
  135. publicPath: string;
  136. js: Array<string>;
  137. css: Array<string>;
  138. manifest?: string;
  139. favicon?: string;
  140. },
  141. assetTags: {
  142. headTags: HtmlTagObject[];
  143. bodyTags: HtmlTagObject[];
  144. },
  145. options: ProcessedOptions
  146. ) => { [option: string]: any } | Promise<{ [option: string]: any }>)
  147. | { [option: string]: any };
  148. /**
  149. * The title to use for the generated HTML document
  150. */
  151. title?: string;
  152. /**
  153. * Enforce self closing tags e.g. <link />
  154. */
  155. xhtml?: boolean;
  156. /**
  157. * In addition to the options actually used by this plugin, you can use this hash to pass arbitrary data through
  158. * to your template.
  159. */
  160. [option: string]: any;
  161. }
  162. /**
  163. * The plugin options after adding default values
  164. */
  165. interface ProcessedOptions extends Required<Options> {
  166. filename: string;
  167. }
  168. /**
  169. * The values which are available during template execution
  170. *
  171. * Please keep in mind that the `templateParameter` options allows to change them
  172. */
  173. interface TemplateParameter {
  174. compilation: any;
  175. htmlWebpackPlugin: {
  176. tags: {
  177. headTags: HtmlTagObject[];
  178. bodyTags: HtmlTagObject[];
  179. };
  180. files: {
  181. publicPath: string;
  182. js: Array<string>;
  183. css: Array<string>;
  184. manifest?: string;
  185. favicon?: string;
  186. };
  187. options: Options;
  188. };
  189. webpackConfig: any;
  190. }
  191. interface Hooks {
  192. alterAssetTags: AsyncSeriesWaterfallHook<{
  193. assetTags: {
  194. scripts: HtmlTagObject[];
  195. styles: HtmlTagObject[];
  196. meta: HtmlTagObject[];
  197. };
  198. publicPath: string,
  199. outputName: string;
  200. plugin: HtmlWebpackPlugin;
  201. }>;
  202. alterAssetTagGroups: AsyncSeriesWaterfallHook<{
  203. headTags: HtmlTagObject[];
  204. bodyTags: HtmlTagObject[];
  205. outputName: string;
  206. publicPath: string,
  207. plugin: HtmlWebpackPlugin;
  208. }>;
  209. afterTemplateExecution: AsyncSeriesWaterfallHook<{
  210. html: string;
  211. headTags: HtmlTagObject[];
  212. bodyTags: HtmlTagObject[];
  213. outputName: string;
  214. plugin: HtmlWebpackPlugin;
  215. }>;
  216. beforeAssetTagGeneration: AsyncSeriesWaterfallHook<{
  217. assets: {
  218. publicPath: string;
  219. js: Array<string>;
  220. css: Array<string>;
  221. favicon?: string;
  222. manifest?: string;
  223. };
  224. outputName: string;
  225. plugin: HtmlWebpackPlugin;
  226. }>;
  227. beforeEmit: AsyncSeriesWaterfallHook<{
  228. html: string;
  229. outputName: string;
  230. plugin: HtmlWebpackPlugin;
  231. }>;
  232. afterEmit: AsyncSeriesWaterfallHook<{
  233. outputName: string;
  234. plugin: HtmlWebpackPlugin;
  235. }>;
  236. }
  237. /**
  238. * A tag element according to the htmlWebpackPlugin object notation
  239. */
  240. interface HtmlTagObject {
  241. /**
  242. * Attributes of the html tag
  243. * E.g. `{'disabled': true, 'value': 'demo'}`
  244. */
  245. attributes: {
  246. [attributeName: string]: string | boolean | null | undefined;
  247. };
  248. /**
  249. * The tag name e.g. `'div'`
  250. */
  251. tagName: string;
  252. /**
  253. * The inner HTML
  254. */
  255. innerHTML?: string;
  256. /**
  257. * Whether this html must not contain innerHTML
  258. * @see https://www.w3.org/TR/html5/syntax.html#void-elements
  259. */
  260. voidTag: boolean;
  261. /**
  262. * Meta information about the tag
  263. * E.g. `{'plugin': 'html-webpack-plugin'}`
  264. */
  265. meta: {
  266. plugin?: string,
  267. [metaAttributeName: string]: any;
  268. };
  269. }
  270. }