Skip to content

Commit f4cb241

Browse files
authoredFeb 16, 2019
fix: Repair typings (#1166)
1 parent 267e0e0 commit f4cb241

File tree

3 files changed

+30
-45
lines changed

3 files changed

+30
-45
lines changed
 

‎index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class HtmlWebpackPlugin {
4646
inject: true,
4747
compile: true,
4848
favicon: false,
49-
minify: undefined,
49+
minify: 'auto',
5050
cache: true,
5151
showErrors: true,
5252
chunks: 'all',
@@ -112,7 +112,7 @@ class HtmlWebpackPlugin {
112112
const isProductionLikeMode = compiler.options.mode === 'production' || !compiler.options.mode;
113113

114114
const minify = this.options.minify;
115-
if (minify === true || (minify === undefined && isProductionLikeMode)) {
115+
if (minify === true || (minify === 'auto' && isProductionLikeMode)) {
116116
/** @type { import('html-minifier').Options } */
117117
this.options.minify = {
118118
// https://github.com/kangax/html-minifier#options-quick-reference
@@ -435,7 +435,7 @@ class HtmlWebpackPlugin {
435435
const htmlAfterInjection = this.options.inject
436436
? this.injectAssetsIntoHtml(html, assets, assetTags)
437437
: html;
438-
const htmlAfterMinification = this.options.minify
438+
const htmlAfterMinification = typeof this.options.minify === 'object'
439439
? require('html-minifier').minify(htmlAfterInjection, this.options.minify)
440440
: htmlAfterInjection;
441441
return Promise.resolve(htmlAfterMinification);

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"semistandard": "12.0.1",
4949
"standard-version": "^4.4.0",
5050
"style-loader": "^0.23.0",
51-
"typescript": "^2.9.2",
51+
"typescript": "^3.3.3",
5252
"webpack": "^4.20.2",
5353
"webpack-recompilation-simulator": "^3.0.0"
5454
},

‎typings.d.ts

+26-41
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ import { Plugin } from "webpack";
22
import { AsyncSeriesWaterfallHook } from "tapable";
33
import { Options as HtmlMinifierOptions } from "html-minifier";
44

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-
125
export = HtmlWebpackPlugin;
136

147
declare class HtmlWebpackPlugin extends Plugin {
@@ -18,58 +11,60 @@ declare class HtmlWebpackPlugin extends Plugin {
1811
declare namespace HtmlWebpackPlugin {
1912
type MinifyOptions = HtmlMinifierOptions;
2013

14+
interface Options extends Partial<ProcessedOptions> {}
15+
2116
/**
22-
* The plugin options
17+
* The plugin options after adding default values
2318
*/
24-
interface Options {
19+
interface ProcessedOptions {
2520
/**
2621
* Emit the file only if it was changed.
2722
* Default: `true`.
2823
*/
29-
cache?: boolean;
24+
cache: boolean;
3025
/**
3126
* List all entries which should be injected
3227
*/
33-
chunks?: "all" | string[];
28+
chunks: "all" | string[];
3429
/**
3530
* Allows to control how chunks should be sorted before they are included to the html.
3631
* Default: `'auto'`.
3732
*/
38-
chunksSortMode?:
33+
chunksSortMode:
3934
| "auto"
4035
| "manual"
4136
| (((entryNameA: string, entryNameB: string) => number));
4237
/**
4338
* List all entries which should not be injeccted
4439
*/
45-
excludeChunks?: string[];
40+
excludeChunks: string[];
4641
/**
4742
* Path to the favicon icon
4843
*/
49-
favicon?: false | string;
44+
favicon: false | string;
5045
/**
5146
* The file to write the HTML to.
5247
* Defaults to `index.html`.
5348
* Supports subdirectories eg: `assets/admin.html`
5449
*/
55-
filename?: string;
50+
filename: string;
5651
/**
5752
* If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files.
5853
* This is useful for cache busting
5954
*/
60-
hash?: boolean;
55+
hash: boolean;
6156
/**
6257
* Inject all assets into the given `template` or `templateContent`.
6358
*/
64-
inject?:
59+
inject:
6560
| false // Don't inject scripts
6661
| true // Inject scripts into body
6762
| "body" // Inject scripts into body
6863
| "head"; // Inject scripts into head
6964
/**
7065
* Inject meta tags
7166
*/
72-
meta?:
67+
meta:
7368
| false // Disable injection
7469
| {
7570
[name: string]:
@@ -78,30 +73,33 @@ declare namespace HtmlWebpackPlugin {
7873
| { [attributeName: string]: string | boolean }; // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" }
7974
};
8075
/**
81-
* HTML Minification options
76+
* HTML Minification options accepts the following valeus:
77+
* - Set to `false` to disable minifcation
78+
* - Set to `'auto'` to enable minifcation only for production mode
79+
* - Set to custom minification according to
8280
* @https://github.com/kangax/html-minifier#options-quick-reference
8381
*/
84-
minify?: boolean | MinifyOptions;
82+
minify: 'auto' | boolean | MinifyOptions;
8583
/**
8684
* Render errors into the HTML page
8785
*/
88-
showErrors?: boolean;
86+
showErrors: boolean;
8987
/**
9088
* The `webpack` require path to the template.
9189
* @see https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md
9290
*/
93-
template?: string;
91+
template: string;
9492
/**
9593
* Allow to use a html string instead of reading from a file
9694
*/
97-
templateContent?:
95+
templateContent:
9896
| false // Use the template option instead to load a file
9997
| string
10098
| Promise<string>;
10199
/**
102100
* Allows to overwrite the parameters used in the template
103101
*/
104-
templateParameters?:
102+
templateParameters:
105103
| false // Pass an empty object to the template function
106104
| ((
107105
compilation: any,
@@ -110,7 +108,7 @@ declare namespace HtmlWebpackPlugin {
110108
headTags: HtmlTagObject[];
111109
bodyTags: HtmlTagObject[];
112110
},
113-
options: Options
111+
options: ProcessedOptions
114112
) => { [option: string]: any })
115113
| ((
116114
compilation: any,
@@ -119,37 +117,24 @@ declare namespace HtmlWebpackPlugin {
119117
headTags: HtmlTagObject[];
120118
bodyTags: HtmlTagObject[];
121119
},
122-
options: Options
120+
options: ProcessedOptions
123121
) => Promise<{ [option: string]: any }>)
124122
| { [option: string]: any };
125123
/**
126124
* The title to use for the generated HTML document
127125
*/
128-
title?: string;
126+
title: string;
129127
/**
130128
* Enforce self closing tags e.g. <link />
131129
*/
132-
xhtml?: boolean;
130+
xhtml: boolean;
133131
/**
134132
* In addition to the options actually used by this plugin, you can use this hash to pass arbitrary data through
135133
* to your template.
136134
*/
137135
[option: string]: any;
138136
}
139137

140-
/**
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
148-
*/
149-
interface ProcessedOptions extends Required<Omit<Options, "minify">> {
150-
minify: MinifyOptions | undefined;
151-
}
152-
153138
/**
154139
* The values which are available during template execution
155140
*

0 commit comments

Comments
 (0)
Please sign in to comment.