|
| 1 | +import { Plugin } from "webpack"; |
| 2 | +import { AsyncSeriesWaterfallHook } from "tapable"; |
| 3 | +import { Options as HtmlMinifierOptions } from "html-minifier"; |
1 | 4 |
|
2 |
| -/** |
3 |
| - * The plugin options |
4 |
| - */ |
5 |
| -interface HtmlWebpackPluginOptions { |
| 5 | +// https://github.com/Microsoft/TypeScript/issues/15012#issuecomment-365453623 |
| 6 | +type Required<T> = T extends object |
| 7 | + ? { [P in keyof T]-?: NonNullable<T[P]> } |
| 8 | + : T; |
| 9 | +// https://stackoverflow.com/questions/48215950/exclude-property-from-type |
| 10 | +type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; |
| 11 | + |
| 12 | +export = HtmlWebpackPlugin; |
| 13 | + |
| 14 | +declare class HtmlWebpackPlugin extends Plugin { |
| 15 | + constructor(options?: HtmlWebpackPlugin.Options); |
| 16 | +} |
| 17 | + |
| 18 | +declare namespace HtmlWebpackPlugin { |
| 19 | + type MinifyOptions = HtmlMinifierOptions; |
| 20 | + |
| 21 | + /** |
| 22 | + * The plugin options |
| 23 | + */ |
| 24 | + interface Options { |
6 | 25 | /**
|
7 |
| - * The title to use for the generated HTML document |
| 26 | + * Emit the file only if it was changed. |
| 27 | + * Default: `true`. |
8 | 28 | */
|
9 |
| - title: string, |
| 29 | + cache?: boolean; |
10 | 30 | /**
|
11 |
| - * The `webpack` require path to the template. |
12 |
| - * @see https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md |
| 31 | + * List all entries which should be injected |
13 | 32 | */
|
14 |
| - template: string, |
| 33 | + chunks?: "all" | string[]; |
15 | 34 | /**
|
16 |
| - * Allow to use a html string instead of reading from a file |
| 35 | + * Allows to control how chunks should be sorted before they are included to the html. |
| 36 | + * Default: `'auto'`. |
17 | 37 | */
|
18 |
| - templateContent: |
19 |
| - false // Use the template option instead to load a file |
20 |
| - | string |
21 |
| - | Promise<string>, |
| 38 | + chunksSortMode?: |
| 39 | + | "auto" |
| 40 | + | "manual" |
| 41 | + | (((entryNameA: string, entryNameB: string) => number)); |
22 | 42 | /**
|
23 |
| - * Allows to overwrite the parameters used in the template |
| 43 | + * List all entries which should not be injeccted |
24 | 44 | */
|
25 |
| - templateParameters: |
26 |
| - false // Pass an empty object to the template function |
27 |
| - | ((compilation: any, assets, assetTags: { headTags: Array<HtmlTagObject>, bodyTags: Array<HtmlTagObject> }, options: HtmlWebpackPluginOptions) => {[option: string]: any}) |
28 |
| - | ((compilation: any, assets, assetTags: { headTags: Array<HtmlTagObject>, bodyTags: Array<HtmlTagObject> }, options: HtmlWebpackPluginOptions) => Promise<{[option: string]: any}>) |
29 |
| - | {[option: string]: any} |
| 45 | + excludeChunks?: string[]; |
| 46 | + /** |
| 47 | + * Path to the favicon icon |
| 48 | + */ |
| 49 | + favicon?: false | string; |
30 | 50 | /**
|
31 | 51 | * The file to write the HTML to.
|
32 | 52 | * Defaults to `index.html`.
|
33 | 53 | * Supports subdirectories eg: `assets/admin.html`
|
34 | 54 | */
|
35 |
| - filename: string, |
| 55 | + filename?: string; |
36 | 56 | /**
|
37 | 57 | * If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files.
|
38 | 58 | * This is useful for cache busting
|
39 | 59 | */
|
40 |
| - hash: boolean, |
| 60 | + hash?: boolean; |
41 | 61 | /**
|
42 | 62 | * Inject all assets into the given `template` or `templateContent`.
|
43 | 63 | */
|
44 |
| - inject: false // Don't inject scripts |
45 |
| - | true // Inject scripts into body |
46 |
| - | 'body' // Inject scripts into body |
47 |
| - | 'head' // Inject scripts into head |
| 64 | + inject?: |
| 65 | + | false // Don't inject scripts |
| 66 | + | true // Inject scripts into body |
| 67 | + | "body" // Inject scripts into body |
| 68 | + | "head"; // Inject scripts into head |
48 | 69 | /**
|
49 |
| - * Path to the favicon icon |
| 70 | + * Inject meta tags |
50 | 71 | */
|
51 |
| - favicon: false | string, |
| 72 | + meta?: |
| 73 | + | false // Disable injection |
| 74 | + | { |
| 75 | + [name: string]: |
| 76 | + | string |
| 77 | + | false // name content pair e.g. {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}` |
| 78 | + | { [attributeName: string]: string | boolean }; // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" } |
| 79 | + }; |
52 | 80 | /**
|
53 | 81 | * HTML Minification options
|
54 | 82 | * @https://github.com/kangax/html-minifier#options-quick-reference
|
55 | 83 | */
|
56 |
| - minify?: boolean | {}, |
57 |
| - cache: boolean, |
| 84 | + minify?: boolean | MinifyOptions; |
58 | 85 | /**
|
59 | 86 | * Render errors into the HTML page
|
60 | 87 | */
|
61 |
| - showErrors: boolean, |
| 88 | + showErrors?: boolean; |
62 | 89 | /**
|
63 |
| - * List all entries which should be injected |
| 90 | + * The `webpack` require path to the template. |
| 91 | + * @see https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md |
64 | 92 | */
|
65 |
| - chunks: 'all' | string[], |
| 93 | + template?: string; |
66 | 94 | /**
|
67 |
| - * List all entries which should not be injeccted |
| 95 | + * Allow to use a html string instead of reading from a file |
68 | 96 | */
|
69 |
| - excludeChunks: string[], |
70 |
| - chunksSortMode: 'auto' | 'manual' | (((entryNameA: string, entryNameB: string) => number)), |
| 97 | + templateContent?: |
| 98 | + | false // Use the template option instead to load a file |
| 99 | + | string |
| 100 | + | Promise<string>; |
71 | 101 | /**
|
72 |
| - * Inject meta tags |
| 102 | + * Allows to overwrite the parameters used in the template |
73 | 103 | */
|
74 |
| - meta: false // Disable injection |
75 |
| - | { |
76 |
| - [name: string]: string|false // name content pair e.g. {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}` |
77 |
| - | {[attributeName: string]: string|boolean} // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" } |
78 |
| - }, |
| 104 | + templateParameters?: |
| 105 | + | false // Pass an empty object to the template function |
| 106 | + | (( |
| 107 | + compilation: any, |
| 108 | + assets, |
| 109 | + assetTags: { |
| 110 | + headTags: HtmlTagObject[]; |
| 111 | + bodyTags: HtmlTagObject[]; |
| 112 | + }, |
| 113 | + options: Options |
| 114 | + ) => { [option: string]: any }) |
| 115 | + | (( |
| 116 | + compilation: any, |
| 117 | + assets, |
| 118 | + assetTags: { |
| 119 | + headTags: HtmlTagObject[]; |
| 120 | + bodyTags: HtmlTagObject[]; |
| 121 | + }, |
| 122 | + options: Options |
| 123 | + ) => Promise<{ [option: string]: any }>) |
| 124 | + | { [option: string]: any }; |
| 125 | + /** |
| 126 | + * The title to use for the generated HTML document |
| 127 | + */ |
| 128 | + title?: string; |
79 | 129 | /**
|
80 | 130 | * Enforce self closing tags e.g. <link />
|
81 | 131 | */
|
82 |
| - xhtml: boolean |
83 |
| - |
| 132 | + xhtml?: boolean; |
84 | 133 | /**
|
85 | 134 | * In addition to the options actually used by this plugin, you can use this hash to pass arbitrary data through
|
86 | 135 | * to your template.
|
87 | 136 | */
|
88 | 137 | [option: string]: any;
|
89 |
| -} |
| 138 | + } |
90 | 139 |
|
91 |
| -/** |
92 |
| - * A tag element according to the htmlWebpackPlugin object notation |
93 |
| - */ |
94 |
| -interface HtmlTagObject { |
95 | 140 | /**
|
96 |
| - * Attributes of the html tag |
97 |
| - * E.g. `{'disabled': true, 'value': 'demo'}` |
| 141 | + * Options interface that matches the expectations of the index.js API: |
| 142 | + * - All fields are required |
| 143 | + * - The minify property matches what html-minifier expects (eg either a MinifyOptions or undefined). |
| 144 | + * html-minifier does not accept a boolean value. As TypeScript does not allow a property to be redefined |
| 145 | + * in an extended interface we need to omit it and then define it properly |
| 146 | + * |
| 147 | + * The Required and Omit types are defined at the top of the file |
98 | 148 | */
|
99 |
| - attributes: { |
100 |
| - [attributeName: string]: string|boolean |
101 |
| - }, |
102 |
| - /** |
103 |
| - * Wether this html must not contain innerHTML |
104 |
| - * @see https://www.w3.org/TR/html5/syntax.html#void-elements |
105 |
| - */ |
106 |
| - voidTag: boolean, |
| 149 | + interface ProcessedOptions extends Required<Omit<Options, "minify">> { |
| 150 | + minify: MinifyOptions | undefined; |
| 151 | + } |
| 152 | + |
107 | 153 | /**
|
108 |
| - * The tag name e.g. `'div'` |
| 154 | + * The values which are available during template execution |
| 155 | + * |
| 156 | + * Please keep in mind that the `templateParameter` options allows to change them |
109 | 157 | */
|
110 |
| - tagName: string, |
| 158 | + interface TemplateParameter { |
| 159 | + compilation: any; |
| 160 | + htmlWebpackPlugin: { |
| 161 | + tags: { |
| 162 | + headTags: HtmlTagObject[]; |
| 163 | + bodyTags: HtmlTagObject[]; |
| 164 | + }; |
| 165 | + files: { |
| 166 | + publicPath: string; |
| 167 | + js: Array<string>; |
| 168 | + css: Array<string>; |
| 169 | + manifest?: string; |
| 170 | + favicon?: string; |
| 171 | + }; |
| 172 | + options: Options; |
| 173 | + }; |
| 174 | + webpackConfig: any; |
| 175 | + } |
| 176 | + |
| 177 | + interface Hooks { |
| 178 | + alterAssetTags: AsyncSeriesWaterfallHook<{ |
| 179 | + assetTags: { |
| 180 | + scripts: HtmlTagObject[]; |
| 181 | + styles: HtmlTagObject[]; |
| 182 | + meta: HtmlTagObject[]; |
| 183 | + }; |
| 184 | + outputName: string; |
| 185 | + plugin: HtmlWebpackPlugin; |
| 186 | + }>; |
| 187 | + |
| 188 | + alterAssetTagGroups: AsyncSeriesWaterfallHook<{ |
| 189 | + headTags: HtmlTagObject[]; |
| 190 | + bodyTags: HtmlTagObject[]; |
| 191 | + outputName: string; |
| 192 | + plugin: HtmlWebpackPlugin; |
| 193 | + }>; |
| 194 | + |
| 195 | + afterTemplateExecution: AsyncSeriesWaterfallHook<{ |
| 196 | + html: string; |
| 197 | + headTags: HtmlTagObject[]; |
| 198 | + bodyTags: HtmlTagObject[]; |
| 199 | + outputName: string; |
| 200 | + plugin: HtmlWebpackPlugin; |
| 201 | + }>; |
| 202 | + |
| 203 | + beforeAssetTagGeneration: AsyncSeriesWaterfallHook<{ |
| 204 | + assets: { |
| 205 | + publicPath: string; |
| 206 | + js: Array<string>; |
| 207 | + css: Array<string>; |
| 208 | + favicon?: string | undefined; |
| 209 | + manifest?: string | undefined; |
| 210 | + }; |
| 211 | + outputName: string; |
| 212 | + plugin: HtmlWebpackPlugin; |
| 213 | + }>; |
| 214 | + |
| 215 | + beforeEmit: AsyncSeriesWaterfallHook<{ |
| 216 | + html: string; |
| 217 | + outputName: string; |
| 218 | + plugin: HtmlWebpackPlugin; |
| 219 | + }>; |
| 220 | + |
| 221 | + afterEmit: AsyncSeriesWaterfallHook<{ |
| 222 | + outputName: string; |
| 223 | + plugin: HtmlWebpackPlugin; |
| 224 | + }>; |
| 225 | + } |
| 226 | + |
111 | 227 | /**
|
112 |
| - * Inner HTML The |
| 228 | + * A tag element according to the htmlWebpackPlugin object notation |
113 | 229 | */
|
114 |
| - innerHTML?: string |
115 |
| -} |
116 |
| - |
117 |
| -/** |
118 |
| - * The values which are available during template execution |
119 |
| - * |
120 |
| - * Please keep in mind that the `templateParameter` options allows to change them |
121 |
| - */ |
122 |
| -interface HtmlWebpackPluginTemplateParameter { |
123 |
| - compilation: any, |
124 |
| - webpackConfig: any |
125 |
| - htmlWebpackPlugin: { |
126 |
| - tags: { |
127 |
| - headTags: HtmlTagObject[], |
128 |
| - bodyTags: HtmlTagObject[] |
129 |
| - }, |
130 |
| - files: { |
131 |
| - publicPath: string, |
132 |
| - js: Array<string>, |
133 |
| - css: Array<string>, |
134 |
| - manifest?: string, |
135 |
| - favicon?: string |
136 |
| - }, |
137 |
| - options: HtmlWebpackPluginOptions |
| 230 | + interface HtmlTagObject { |
| 231 | + /** |
| 232 | + * Attributes of the html tag |
| 233 | + * E.g. `{'disabled': true, 'value': 'demo'}` |
| 234 | + */ |
| 235 | + attributes: { |
| 236 | + [attributeName: string]: string | boolean; |
| 237 | + }; |
| 238 | + /** |
| 239 | + * The tag name e.g. `'div'` |
| 240 | + */ |
| 241 | + tagName: string; |
| 242 | + /** |
| 243 | + * The inner HTML |
| 244 | + */ |
| 245 | + innerHTML?: string; |
| 246 | + /** |
| 247 | + * Whether this html must not contain innerHTML |
| 248 | + * @see https://www.w3.org/TR/html5/syntax.html#void-elements |
| 249 | + */ |
| 250 | + voidTag: boolean; |
138 | 251 | }
|
139 | 252 | }
|
0 commit comments