Skip to content

Commit fc1e024

Browse files
authored
🤖 Merge PR DefinitelyTyped#73426 feat: add types for html-minifier-next by @orpheus6678
1 parent a967960 commit fc1e024

File tree

6 files changed

+693
-0
lines changed

6 files changed

+693
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import required = require("html-minifier-next")
2+
import imported, { minify, MinifierOptions } from "html-minifier-next"
3+
import * as namespaced from "html-minifier-next"
4+
5+
// $ExpectType Promise<string>
6+
imported.minify("<p title=\"blah\" id=\"moo\">foo</p>")
7+
8+
// $ExpectType Promise<string>
9+
required.minify("<p title=\"blah\" id=\"moo\">foo</p>")
10+
11+
// $ExpectType Promise<string>
12+
namespaced.minify("<p title=\"blah\" id=\"moo\">foo</p>")
13+
14+
const options: MinifierOptions = {
15+
caseSensitive: true,
16+
collapseBooleanAttributes: true,
17+
collapseInlineTagWhitespace: true,
18+
collapseWhitespace: true,
19+
conservativeCollapse: true,
20+
continueOnParseError: true,
21+
customAttrAssign: [/.*/],
22+
customAttrCollapse: /ng-class/,
23+
customAttrSurround: [/btn-class/],
24+
customEventAttributes: [/on-click/],
25+
customFragmentQuantifierLimit: 144,
26+
decodeEntities: true,
27+
html5: false,
28+
ignoreCustomComments: [/^!/, /^\s*#/],
29+
ignoreCustomFragments: [/<%[\s\S]*?%>/, /<\?[\s\S]\*?\?>/],
30+
includeAutoGeneratedTags: false,
31+
keepClosingSlash: false,
32+
maxLineLength: 20,
33+
maxInputLength: 1000,
34+
minifyCSS: true,
35+
minifyJS: true,
36+
minifyURLs: true,
37+
noNewlinesBeforeTagClose: true,
38+
preserveLineBreaks: true,
39+
preventAttributesEscaping: true,
40+
processConditionalComments: true,
41+
processScripts: ["text/v-text"],
42+
quoteCharacter: "\"",
43+
removeAttributeQuotes: true,
44+
removeComments: true,
45+
removeEmptyAttributes: true,
46+
removeEmptyElements: true,
47+
removeOptionalTags: true,
48+
removeRedundantAttributes: true,
49+
removeScriptTypeAttributes: true,
50+
removeStyleLinkTypeAttributes: true,
51+
removeTagWhitespace: true,
52+
sortAttributes: true,
53+
sortClassName: true,
54+
trimCustomFragments: true,
55+
useShortDoctype: true,
56+
}
57+
58+
const optionsImported: imported.MinifierOptions = options
59+
const optionsRequired: required.MinifierOptions = options
60+
const optionsNamespaced: namespaced.MinifierOptions = options
61+
62+
// $ExpectType Promise<string>
63+
minify("<p title=\"blah\" id=\"moo\">foo</p>", options)
64+
65+
// $ExpectType Promise<string>
66+
namespaced.default.minify("<p title=\"blah\" id=\"moo\">foo</p>", options)
67+
68+
// $ExpectType Promise<string>
69+
required.default.minify("<p title=\"blah\" id=\"moo\">foo</p>", options)
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
import * as CleanCSS from "clean-css"
2+
import RelateUrl = require("relateurl")
3+
import type * as Terser from "terser" with { "resolution-mode": "import" }
4+
5+
export function minify(value: string, options?: MinifierOptions): Promise<string>
6+
7+
declare namespace _default {
8+
export { minify, type MinifierOptions }
9+
}
10+
11+
export { _default as default }
12+
13+
/**
14+
* Most of the options are disabled by default
15+
*/
16+
export interface MinifierOptions {
17+
/**
18+
* Treat attributes in case sensitive manner (useful for custom HTML tags)
19+
* @default false
20+
*/
21+
caseSensitive?: boolean
22+
23+
/**
24+
* [Omit attribute values from boolean attributes](http://perfectionkills.com/experimenting-with-html-minifier#collapse_boolean_attributes)
25+
* @default false
26+
*/
27+
collapseBooleanAttributes?: boolean
28+
29+
/**
30+
* Don't leave any spaces between `display:inline;` elements when collapsing.
31+
* Must be used in conjunction with `collapseWhitespace=true`
32+
*
33+
* @default false
34+
*/
35+
collapseInlineTagWhitespace?: boolean
36+
37+
/**
38+
* [Collapse white space that contributes to text nodes in a document tree](http://perfectionkills.com/experimenting-with-html-minifier#collapse_whitespace)
39+
* @default false
40+
*/
41+
collapseWhitespace?: boolean
42+
43+
/**
44+
* Always collapse to 1 space (never remove it entirely).
45+
* Must be used in conjunction with `collapseWhitespace=true`
46+
*
47+
* @default false
48+
*/
49+
conservativeCollapse?: boolean
50+
51+
/**
52+
* [Handle parse errors](https://html.spec.whatwg.org/multipage/parsing.html#parse-errors) instead of aborting
53+
* @default false
54+
*/
55+
continueOnParseError?: boolean
56+
57+
/**
58+
* Arrays of regexes that allow to support custom attribute assign expressions
59+
* (e.g. `'<div flex?="{{mode != cover}}"></div>'`)
60+
*
61+
* @default []
62+
*/
63+
customAttrAssign?: RegExp[]
64+
65+
/**
66+
* Regex that specifies custom attribute to strip newlines from (e.g. `/ng-class/`)
67+
*/
68+
customAttrCollapse?: RegExp
69+
70+
/**
71+
* Arrays of regexes that allow to support custom attribute surround expressions
72+
* (e.g. `<input {{#if value}}checked="checked"{{/if}}>`)
73+
*
74+
* @default []
75+
*/
76+
customAttrSurround?: RegExp[]
77+
78+
/**
79+
* Arrays of regexes that allow to support custom event attributes for `minifyJS` (e.g. `ng-click`)
80+
* @default [/^on[a-z]{3,}$/]
81+
*/
82+
customEventAttributes?: RegExp[]
83+
84+
/**
85+
* Set maximum quantifier limit for custom fragments to prevent ReDoS attacks
86+
* @default 200
87+
*/
88+
customFragmentQuantifierLimit?: number
89+
90+
/**
91+
* Use direct Unicode characters whenever possible
92+
* @default false
93+
*/
94+
decodeEntities?: boolean
95+
96+
/**
97+
* Parse input according to HTML5 specifications
98+
* @default true
99+
*/
100+
html5?: boolean
101+
102+
/**
103+
* Array of regexes that allow to ignore certain comments, when matched
104+
* @default [/^!/, /^\s*#/]
105+
*/
106+
ignoreCustomComments?: RegExp[]
107+
108+
/**
109+
* Array of regexes that allow to ignore certain fragments, when matched
110+
* (e.g. `<?php ... ?>`, `{{ ... }}`, etc.)
111+
*
112+
* @default [/<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/]
113+
*/
114+
ignoreCustomFragments?: RegExp[]
115+
116+
/**
117+
* Insert tags generated by HTML parser
118+
* @default true
119+
*/
120+
includeAutoGeneratedTags?: boolean
121+
122+
/**
123+
* Keep the trailing slash on singleton elements
124+
* @default false
125+
*/
126+
keepClosingSlash?: boolean
127+
128+
/**
129+
* Maximum input length to prevent ReDoS attacks (disabled by default)
130+
* @default undefined
131+
*/
132+
maxInputLength?: number
133+
134+
/**
135+
* Specify a maximum line length.
136+
* Compressed output will be split by newlines at valid HTML split-points
137+
*/
138+
maxLineLength?: number
139+
140+
/**
141+
* Minify CSS in style elements and style attributes
142+
* (uses [clean-css](https://github.com/jakubpawlowicz/clean-css))
143+
*
144+
* @default false
145+
*/
146+
minifyCSS?: boolean | CleanCSS.Options | ((text: string, type?: string) => string)
147+
148+
/**
149+
* Minify JavaScript in script elements and event attributes
150+
* (uses [Terser](https://github.com/terser/terser))
151+
*
152+
* This property is loosely typed due to dtslint restrictions.
153+
* It also accepts `import("terser").MinifyOptions`
154+
*
155+
* @default false
156+
*/
157+
minifyJS?: boolean | Terser.MinifyOptions | ((text: string, inline?: boolean) => string)
158+
159+
/**
160+
* Minify URLs in various attributes
161+
* (uses [relateurl](https://github.com/stevenvachon/relateurl))
162+
*
163+
* @default false
164+
*/
165+
minifyURLs?: boolean | string | RelateUrl.Options | ((text: string) => string)
166+
167+
/**
168+
* Never add a newline before a tag that closes an element
169+
* @default false
170+
*/
171+
noNewlinesBeforeTagClose?: boolean
172+
173+
/**
174+
* Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break.
175+
* Must be used in conjunction with `collapseWhitespace=true`
176+
*
177+
* @default false
178+
*/
179+
preserveLineBreaks?: boolean
180+
181+
/**
182+
* Prevents the escaping of the values of attributes
183+
* @default false
184+
*/
185+
preventAttributesEscaping?: boolean
186+
187+
/**
188+
* Process contents of conditional comments through minifier
189+
* @default false
190+
*/
191+
processConditionalComments?: boolean
192+
193+
/**
194+
* Array of strings corresponding to types of script elements to process through minifier
195+
* (e.g. `text/ng-template`, `text/x-handlebars-template`, etc.)
196+
*
197+
* @default []
198+
*/
199+
processScripts?: string[]
200+
201+
/**
202+
* Type of quote to use for attribute values (“'” or “"”)
203+
*/
204+
quoteCharacter?: string
205+
206+
/**
207+
* [Remove quotes around attributes when possible](http://perfectionkills.com/experimenting-with-html-minifier#remove_attribute_quotes)
208+
* @default false
209+
*/
210+
removeAttributeQuotes?: boolean
211+
212+
/**
213+
* [Strip HTML comments](http://perfectionkills.com/experimenting-with-html-minifier#remove_comments)
214+
* @default false
215+
*/
216+
removeComments?: boolean
217+
218+
/**
219+
* [Remove all attributes with whitespace-only values](http://perfectionkills.com/experimenting-with-html-minifier#remove_empty_or_blank_attributes)
220+
* @default false
221+
*/
222+
removeEmptyAttributes?: boolean | ((attrName: string, tag: string) => boolean)
223+
224+
/**
225+
* [Remove all elements with empty contents](http://perfectionkills.com/experimenting-with-html-minifier#remove_empty_elements)
226+
* @default false
227+
*/
228+
removeEmptyElements?: boolean
229+
230+
/**
231+
* [Remove optional tags](http://perfectionkills.com/experimenting-with-html-minifier#remove_optional_tags)
232+
* @default false
233+
*/
234+
removeOptionalTags?: boolean
235+
236+
/**
237+
* [Remove attributes when value matches default](http://perfectionkills.com/experimenting-with-html-minifier#remove_redundant_attributes)
238+
* @default false
239+
*/
240+
removeRedundantAttributes?: boolean
241+
242+
/**
243+
* Remove `type="text/javascript"` from `script` tags.
244+
* Other `type` attribute values are left intact
245+
*
246+
* @default false
247+
*/
248+
removeScriptTypeAttributes?: boolean
249+
250+
/**
251+
* Remove `type="text/css"` from `style` and `link` tags.
252+
* Other `type` attribute values are left intact
253+
*
254+
* @default false
255+
*/
256+
removeStyleLinkTypeAttributes?: boolean
257+
258+
/**
259+
* Remove space between attributes whenever possible. **Note that this will result in invalid HTML!**
260+
* @default false
261+
*/
262+
removeTagWhitespace?: boolean
263+
264+
/**
265+
* [Sort attributes by frequency](https://github.com/j9t/html-minifier-next?tab=readme-ov-file#sorting-attributes--style-classes)
266+
* @default false
267+
*/
268+
sortAttributes?: boolean
269+
270+
/**
271+
* [Sort style classes by frequency](https://github.com/j9t/html-minifier-next?tab=readme-ov-file#sorting-attributes--style-classes)
272+
* @default false
273+
*/
274+
sortClassName?: boolean
275+
276+
/**
277+
* Trim white space around `ignoreCustomFragments`
278+
* @default false
279+
*/
280+
trimCustomFragments?: boolean
281+
282+
/**
283+
* Replaces the `doctype` with the short (HTML5) doctype
284+
* @default false
285+
*/
286+
useShortDoctype?: boolean
287+
}

0 commit comments

Comments
 (0)