html-minifier-next 4.17.2 → 4.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -21
- package/cli.js +3 -0
- package/dist/htmlminifier.cjs +344 -99
- package/dist/htmlminifier.esm.bundle.js +344 -99
- package/dist/types/htmlminifier.d.ts +28 -0
- package/dist/types/htmlminifier.d.ts.map +1 -1
- package/dist/types/htmlparser.d.ts.map +1 -1
- package/dist/types/lib/attributes.d.ts.map +1 -1
- package/dist/types/lib/constants.d.ts +1 -0
- package/dist/types/lib/constants.d.ts.map +1 -1
- package/dist/types/lib/options.d.ts +1 -2
- package/dist/types/lib/options.d.ts.map +1 -1
- package/dist/types/lib/svg.d.ts.map +1 -1
- package/dist/types/presets.d.ts +1 -0
- package/dist/types/presets.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/htmlminifier.js +206 -12
- package/src/htmlparser.js +111 -63
- package/src/lib/attributes.js +4 -1
- package/src/lib/constants.js +6 -0
- package/src/lib/options.js +8 -14
- package/src/lib/svg.js +15 -8
- package/src/presets.js +1 -0
package/README.md
CHANGED
|
@@ -130,6 +130,8 @@ Options can be used in config files (camelCase) or via CLI flags (kebab-case wit
|
|
|
130
130
|
|
|
131
131
|
| Option (config/CLI) | Description | Default |
|
|
132
132
|
| --- | --- | --- |
|
|
133
|
+
| `cacheCSS`<br>`--cache-css` | Set CSS minification cache size; higher values improve performance for batch processing | `500` (or `1000` when `CI=true`) |
|
|
134
|
+
| `cacheJS`<br>`--cache-js` | Set JavaScript minification cache size; higher values improve performance for batch processing | `500` (or `1000` when `CI=true`) |
|
|
133
135
|
| `caseSensitive`<br>`--case-sensitive` | Treat attributes in case-sensitive manner (useful for custom HTML elements) | `false` |
|
|
134
136
|
| `collapseAttributeWhitespace`<br>`--collapse-attribute-whitespace` | Trim and collapse whitespace characters within attribute values | `false` |
|
|
135
137
|
| `collapseBooleanAttributes`<br>`--collapse-boolean-attributes` | [Omit attribute values from boolean attributes](https://perfectionkills.com/experimenting-with-html-minifier/#collapse_boolean_attributes) | `false` |
|
|
@@ -152,6 +154,7 @@ Options can be used in config files (camelCase) or via CLI flags (kebab-case wit
|
|
|
152
154
|
| `keepClosingSlash`<br>`--keep-closing-slash` | Keep the trailing slash on void elements | `false` |
|
|
153
155
|
| `maxInputLength`<br>`--max-input-length` | Maximum input length to prevent ReDoS attacks (disabled by default) | `undefined` |
|
|
154
156
|
| `maxLineLength`<br>`--max-line-length` | Specify a maximum line length; compressed output will be split by newlines at valid HTML split-points | `undefined` |
|
|
157
|
+
| `mergeScripts`<br>`--merge-scripts` | Merge consecutive inline `script` elements into one (only merges compatible scripts with same `type`, matching `async`/`defer`/`nomodule`/`nonce`) | `false` |
|
|
155
158
|
| `minifyCSS`<br>`--minify-css` | Minify CSS in `style` elements and attributes (uses [Lightning CSS](https://lightningcss.dev/)) | `false` (could be `true`, `Object`, `Function(text, type)`) |
|
|
156
159
|
| `minifyJS`<br>`--minify-js` | Minify JavaScript in `script` elements and event attributes (uses [Terser](https://github.com/terser/terser) or [SWC](https://swc.rs/)) | `false` (could be `true`, `Object`, `Function(text, inline)`) |
|
|
157
160
|
| `minifySVG`<br>`--minify-svg` | Minify SVG elements and attributes (numeric precision, default attributes, colors) | `false` (could be `true`, `Object`) |
|
|
@@ -283,6 +286,75 @@ const result = await minify(html, {
|
|
|
283
286
|
});
|
|
284
287
|
```
|
|
285
288
|
|
|
289
|
+
### CSS and JavaScript cache configuration
|
|
290
|
+
|
|
291
|
+
HTML Minifier Next uses in-memory caches to improve performance when processing multiple files or repeated content. The cache sizes can be configured for optimal performance based on your use case:
|
|
292
|
+
|
|
293
|
+
```javascript
|
|
294
|
+
const result = await minify(html, {
|
|
295
|
+
minifyCSS: true,
|
|
296
|
+
minifyJS: true,
|
|
297
|
+
// Configure cache sizes (in number of entries)
|
|
298
|
+
cacheCSS: 750, // CSS cache size, default: 500 (1000 in CI mode)
|
|
299
|
+
cacheJS: 250 // JS cache size, default: 500 (1000 in CI mode)
|
|
300
|
+
});
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
**Via CLI flags:**
|
|
304
|
+
|
|
305
|
+
```shell
|
|
306
|
+
html-minifier-next --minify-css --cache-css 750 --minify-js --cache-js 250 input.html
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
**Via environment variables:**
|
|
310
|
+
|
|
311
|
+
```shell
|
|
312
|
+
export HMN_CACHE_CSS=750
|
|
313
|
+
export HMN_CACHE_JS=250
|
|
314
|
+
html-minifier-next --minify-css --minify-js input.html
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
**Configuration file:**
|
|
318
|
+
|
|
319
|
+
```json
|
|
320
|
+
{
|
|
321
|
+
"minifyCSS": true,
|
|
322
|
+
"cacheCSS": 750,
|
|
323
|
+
"minifyJS": true,
|
|
324
|
+
"cacheJS": 250
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
**For batch/CI processing:**
|
|
329
|
+
|
|
330
|
+
Set `CI=true` to double default cache sizes (optimal for processing many files):
|
|
331
|
+
|
|
332
|
+
```shell
|
|
333
|
+
# Single command
|
|
334
|
+
CI=true html-minifier-next --minify-css --minify-js input.html
|
|
335
|
+
|
|
336
|
+
# Or export for multiple commands
|
|
337
|
+
export CI=true
|
|
338
|
+
html-minifier-next --minify-css --minify-js file1.html
|
|
339
|
+
html-minifier-next --minify-css --minify-js file2.html
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
This is particularly useful in CI/CD pipelines where you’re processing multiple files and want better performance without manually tuning cache sizes.
|
|
343
|
+
|
|
344
|
+
**When to adjust cache sizes:**
|
|
345
|
+
|
|
346
|
+
* Single file processing: Default `500` is sufficient
|
|
347
|
+
* Batch processing (CI/CD): Increase to `1000` or higher for better cache hit rates
|
|
348
|
+
* Memory-constrained environments: Reduce to `200`–`300` to save memory
|
|
349
|
+
* Hundreds/thousands of files: Increase to `1000`–`2000` for optimal performance
|
|
350
|
+
|
|
351
|
+
**Important:**
|
|
352
|
+
|
|
353
|
+
* Cache locking: Caches are created on the first `minify()` call and persist for the process lifetime. Cache sizes are locked after first initialization—subsequent calls reuse the same caches even if different `cacheCSS`/`cacheJS` options are provided. The first call’s options determine the cache sizes.
|
|
354
|
+
* Zero values: Explicit `0` values are coerced to `1` (minimum functional cache size) to avoid immediate eviction. If you want to minimize memory usage, use a small number like `10` or `50` instead of `0`.
|
|
355
|
+
|
|
356
|
+
The caches persist across multiple `minify()` calls, making them particularly effective when processing many files in a batch operation.
|
|
357
|
+
|
|
286
358
|
### SVG minification
|
|
287
359
|
|
|
288
360
|
When `minifySVG` is set to `true`, HTML Minifier Next applies SVG-specific optimizations to SVG elements and their attributes. These optimizations are lightweight, fast, and safe:
|
|
@@ -344,38 +416,38 @@ How does HTML Minifier Next compare to other minifiers? (All minification with t
|
|
|
344
416
|
| Site | Original Size (KB) | [HTML Minifier Next](https://github.com/j9t/html-minifier-next) ([config](https://github.com/j9t/html-minifier-next/blob/main/benchmarks/html-minifier.json))<br>[](https://socket.dev/npm/package/html-minifier-next) | [htmlnano](https://github.com/posthtml/htmlnano)<br>[](https://socket.dev/npm/package/htmlnano) | [@swc/html](https://github.com/swc-project/swc)<br>[](https://socket.dev/npm/package/@swc/html) | [minify-html](https://github.com/wilsonzlin/minify-html)<br>[](https://socket.dev/npm/package/@minify-html/node) | [minimize](https://github.com/Swaagie/minimize)<br>[](https://socket.dev/npm/package/minimize) | [htmlcompressor.com](https://htmlcompressor.com/) |
|
|
345
417
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
346
418
|
| [A List Apart](https://alistapart.com/) | 59 | **49** | 51 | 52 | 51 | 54 | 52 |
|
|
347
|
-
| [Apple](https://www.apple.com/) |
|
|
348
|
-
| [BBC](https://www.bbc.co.uk/) |
|
|
349
|
-
| [CERN](https://home.cern/) | 151 | **
|
|
350
|
-
| [CSS-Tricks](https://css-tricks.com/) | 161 | **119** |
|
|
419
|
+
| [Apple](https://www.apple.com/) | 255 | **197** | 226 | 230 | 231 | 233 | 233 |
|
|
420
|
+
| [BBC](https://www.bbc.co.uk/) | 620 | **562** | 582 | 583 | 584 | 615 | n/a |
|
|
421
|
+
| [CERN](https://home.cern/) | 151 | **82** | 90 | 90 | 91 | 93 | 95 |
|
|
422
|
+
| [CSS-Tricks](https://css-tricks.com/) | 161 | **119** | 126 | 142 | 142 | 147 | 143 |
|
|
351
423
|
| [ECMAScript](https://tc39.es/ecma262/) | 7250 | **6401** | 6573 | 6455 | 6578 | 6626 | n/a |
|
|
352
424
|
| [EDRi](https://edri.org/) | 80 | **59** | 70 | 70 | 71 | 75 | 73 |
|
|
353
|
-
| [EFF](https://www.eff.org/) |
|
|
425
|
+
| [EFF](https://www.eff.org/) | 54 | **45** | 48 | 47 | 48 | 49 | 49 |
|
|
354
426
|
| [European Alternatives](https://european-alternatives.eu/) | 48 | **30** | 32 | 32 | 32 | 32 | 32 |
|
|
355
|
-
| [FAZ](https://www.faz.net/aktuell/) |
|
|
427
|
+
| [FAZ](https://www.faz.net/aktuell/) | 1596 | 1458 | **1430** | 1520 | 1531 | 1542 | n/a |
|
|
356
428
|
| [French Tech](https://lafrenchtech.gouv.fr/) | 153 | **122** | 126 | 126 | 126 | 132 | 127 |
|
|
357
|
-
| [Frontend Dogma](https://frontenddogma.com/) |
|
|
429
|
+
| [Frontend Dogma](https://frontenddogma.com/) | 228 | **220** | 241 | 226 | 228 | 247 | 228 |
|
|
358
430
|
| [Google](https://www.google.com/) | 18 | **16** | 17 | 17 | 17 | 18 | 18 |
|
|
359
|
-
| [Ground News](https://ground.news/) |
|
|
431
|
+
| [Ground News](https://ground.news/) | 1658 | **1431** | 1526 | 1548 | 1553 | 1645 | n/a |
|
|
360
432
|
| [HTML Living Standard](https://html.spec.whatwg.org/multipage/) | 149 | 148 | 153 | **147** | 149 | 155 | 149 |
|
|
361
|
-
| [Igalia](https://www.igalia.com/) |
|
|
362
|
-
| [Leanpub](https://leanpub.com/) |
|
|
363
|
-
| [Mastodon](https://mastodon.social/explore) |
|
|
433
|
+
| [Igalia](https://www.igalia.com/) | 49 | **33** | 36 | 35 | 36 | 36 | 36 |
|
|
434
|
+
| [Leanpub](https://leanpub.com/) | 233 | **204** | 219 | 219 | 219 | 228 | 230 |
|
|
435
|
+
| [Mastodon](https://mastodon.social/explore) | 38 | **28** | 32 | 35 | 35 | 36 | 36 |
|
|
364
436
|
| [MDN](https://developer.mozilla.org/en-US/) | 109 | **62** | 64 | 65 | 65 | 68 | 68 |
|
|
365
|
-
| [Middle East Eye](https://www.middleeasteye.net/) | 222 | **196** | 202 | 200 | 200 |
|
|
366
|
-
| [Mistral AI](https://mistral.ai/) |
|
|
437
|
+
| [Middle East Eye](https://www.middleeasteye.net/) | 222 | **196** | 202 | 200 | 200 | 201 | 202 |
|
|
438
|
+
| [Mistral AI](https://mistral.ai/) | 364 | **319** | 326 | 329 | 330 | 360 | n/a |
|
|
367
439
|
| [Mozilla](https://www.mozilla.org/) | 45 | **31** | 35 | 34 | 34 | 35 | 35 |
|
|
368
|
-
| [Nielsen Norman Group](https://www.nngroup.com/) | 93 | 70 | **57** |
|
|
369
|
-
| [SitePoint](https://www.sitepoint.com/) |
|
|
440
|
+
| [Nielsen Norman Group](https://www.nngroup.com/) | 93 | 70 | **57** | 76 | 77 | 78 | 78 |
|
|
441
|
+
| [SitePoint](https://www.sitepoint.com/) | 522 | **391** | 463 | 496 | 501 | 519 | n/a |
|
|
370
442
|
| [Startup-Verband](https://startupverband.de/) | 43 | **30** | 31 | **30** | 31 | 31 | 31 |
|
|
371
|
-
| [TetraLogical](https://tetralogical.com/) |
|
|
372
|
-
| [TPGi](https://www.tpgi.com/) |
|
|
373
|
-
| [United Nations](https://www.un.org/en/) |
|
|
443
|
+
| [TetraLogical](https://tetralogical.com/) | 48 | **22** | 39 | 41 | 42 | 42 | 42 |
|
|
444
|
+
| [TPGi](https://www.tpgi.com/) | 173 | **157** | 159 | 163 | 164 | 170 | 170 |
|
|
445
|
+
| [United Nations](https://www.un.org/en/) | 151 | **112** | 121 | 125 | 125 | 130 | 123 |
|
|
374
446
|
| [Vivaldi](https://vivaldi.com/) | 93 | **74** | n/a | 79 | 81 | 84 | 82 |
|
|
375
|
-
| [W3C](https://www.w3.org/) |
|
|
376
|
-
| **Average processing time** | |
|
|
447
|
+
| [W3C](https://www.w3.org/) | 51 | **36** | 39 | 39 | 39 | 41 | 39 |
|
|
448
|
+
| **Average processing time** | | 95 ms (30/30) | 143 ms (29/30) | 46 ms (30/30) | **14 ms (30/30)** | 274 ms (30/30) | 1297 ms (24/30) |
|
|
377
449
|
|
|
378
|
-
(Last updated: Jan
|
|
450
|
+
(Last updated: Jan 20, 2026)
|
|
379
451
|
<!-- End auto-generated -->
|
|
380
452
|
|
|
381
453
|
Notes: Minimize does not minify CSS and JS. [HTML Minifier Terser](https://github.com/terser/html-minifier-terser) is currently not included due to issues around whitespace collapsing and removal of code using modern CSS features, issues which appeared to distort the data.
|
package/cli.js
CHANGED
|
@@ -144,6 +144,7 @@ const mainOptions = {
|
|
|
144
144
|
keepClosingSlash: 'Keep the trailing slash on void elements',
|
|
145
145
|
maxInputLength: ['Maximum input length to prevent ReDoS attacks', parseValidInt('maxInputLength')],
|
|
146
146
|
maxLineLength: ['Specify a maximum line length; compressed output will be split by newlines at valid HTML split-points', parseValidInt('maxLineLength')],
|
|
147
|
+
mergeScripts: 'Merge consecutive inline `script` elements into one',
|
|
147
148
|
minifyCSS: ['Minify CSS in `style` elements and attributes (uses Lightning CSS)', parseJSON],
|
|
148
149
|
minifyJS: ['Minify JavaScript in `script` elements and event attributes (uses Terser or SWC; pass `{"engine": "swc"}` for SWC)', parseJSON],
|
|
149
150
|
minifySVG: ['Minify SVG elements and attributes (numeric precision, default attributes, colors)', parseJSON],
|
|
@@ -282,6 +283,8 @@ program.option('--input-dir <dir>', 'Specify an input directory');
|
|
|
282
283
|
program.option('--ignore-dir <patterns>', 'Exclude directories—relative to input directory—from processing (comma-separated), e.g., “libs” or “libs,vendor,node_modules”');
|
|
283
284
|
program.option('--output-dir <dir>', 'Specify an output directory');
|
|
284
285
|
program.option('--file-ext <extensions>', 'Specify file extension(s) to process (comma-separated), e.g., “html” or “html,htm,php”');
|
|
286
|
+
program.option('--cache-css <size>', 'Set CSS minification cache size (number of entries, default: 500 [1000 in CI])', parseValidInt('cacheCSS'));
|
|
287
|
+
program.option('--cache-js <size>', 'Set JavaScript minification cache size (number of entries, default: 500 [1000 in CI])', parseValidInt('cacheJS'));
|
|
285
288
|
|
|
286
289
|
(async () => {
|
|
287
290
|
let content;
|