|
| 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