typings.d.ts 7.9 KB

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