html-minifier-next 4.18.0 → 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 +87 -16
- package/cli.js +2 -0
- package/dist/htmlminifier.cjs +187 -85
- package/dist/htmlminifier.esm.bundle.js +187 -85
- package/dist/types/htmlminifier.d.ts +20 -0
- package/dist/types/htmlminifier.d.ts.map +1 -1
- package/dist/types/htmlparser.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/package.json +1 -1
- package/src/htmlminifier.js +69 -7
- package/src/htmlparser.js +111 -63
- package/src/lib/options.js +8 -14
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` |
|
|
@@ -284,6 +286,75 @@ const result = await minify(html, {
|
|
|
284
286
|
});
|
|
285
287
|
```
|
|
286
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
|
+
|
|
287
358
|
### SVG minification
|
|
288
359
|
|
|
289
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:
|
|
@@ -345,38 +416,38 @@ How does HTML Minifier Next compare to other minifiers? (All minification with t
|
|
|
345
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/) |
|
|
346
417
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
347
418
|
| [A List Apart](https://alistapart.com/) | 59 | **49** | 51 | 52 | 51 | 54 | 52 |
|
|
348
|
-
| [Apple](https://www.apple.com/) |
|
|
349
|
-
| [BBC](https://www.bbc.co.uk/) |
|
|
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 |
|
|
350
421
|
| [CERN](https://home.cern/) | 151 | **82** | 90 | 90 | 91 | 93 | 95 |
|
|
351
|
-
| [CSS-Tricks](https://css-tricks.com/) | 161 | **119** | 126 |
|
|
422
|
+
| [CSS-Tricks](https://css-tricks.com/) | 161 | **119** | 126 | 142 | 142 | 147 | 143 |
|
|
352
423
|
| [ECMAScript](https://tc39.es/ecma262/) | 7250 | **6401** | 6573 | 6455 | 6578 | 6626 | n/a |
|
|
353
424
|
| [EDRi](https://edri.org/) | 80 | **59** | 70 | 70 | 71 | 75 | 73 |
|
|
354
|
-
| [EFF](https://www.eff.org/) | 54 | **45** |
|
|
425
|
+
| [EFF](https://www.eff.org/) | 54 | **45** | 48 | 47 | 48 | 49 | 49 |
|
|
355
426
|
| [European Alternatives](https://european-alternatives.eu/) | 48 | **30** | 32 | 32 | 32 | 32 | 32 |
|
|
356
|
-
| [FAZ](https://www.faz.net/aktuell/) |
|
|
427
|
+
| [FAZ](https://www.faz.net/aktuell/) | 1596 | 1458 | **1430** | 1520 | 1531 | 1542 | n/a |
|
|
357
428
|
| [French Tech](https://lafrenchtech.gouv.fr/) | 153 | **122** | 126 | 126 | 126 | 132 | 127 |
|
|
358
|
-
| [Frontend Dogma](https://frontenddogma.com/) | 228 | **220** |
|
|
429
|
+
| [Frontend Dogma](https://frontenddogma.com/) | 228 | **220** | 241 | 226 | 228 | 247 | 228 |
|
|
359
430
|
| [Google](https://www.google.com/) | 18 | **16** | 17 | 17 | 17 | 18 | 18 |
|
|
360
|
-
| [Ground News](https://ground.news/) |
|
|
431
|
+
| [Ground News](https://ground.news/) | 1658 | **1431** | 1526 | 1548 | 1553 | 1645 | n/a |
|
|
361
432
|
| [HTML Living Standard](https://html.spec.whatwg.org/multipage/) | 149 | 148 | 153 | **147** | 149 | 155 | 149 |
|
|
362
433
|
| [Igalia](https://www.igalia.com/) | 49 | **33** | 36 | 35 | 36 | 36 | 36 |
|
|
363
|
-
| [Leanpub](https://leanpub.com/) |
|
|
434
|
+
| [Leanpub](https://leanpub.com/) | 233 | **204** | 219 | 219 | 219 | 228 | 230 |
|
|
364
435
|
| [Mastodon](https://mastodon.social/explore) | 38 | **28** | 32 | 35 | 35 | 36 | 36 |
|
|
365
436
|
| [MDN](https://developer.mozilla.org/en-US/) | 109 | **62** | 64 | 65 | 65 | 68 | 68 |
|
|
366
|
-
| [Middle East Eye](https://www.middleeasteye.net/) |
|
|
437
|
+
| [Middle East Eye](https://www.middleeasteye.net/) | 222 | **196** | 202 | 200 | 200 | 201 | 202 |
|
|
367
438
|
| [Mistral AI](https://mistral.ai/) | 364 | **319** | 326 | 329 | 330 | 360 | n/a |
|
|
368
|
-
| [Mozilla](https://www.mozilla.org/) |
|
|
369
|
-
| [Nielsen Norman Group](https://www.nngroup.com/) | 93 | 70 | **57** |
|
|
370
|
-
| [SitePoint](https://www.sitepoint.com/) |
|
|
439
|
+
| [Mozilla](https://www.mozilla.org/) | 45 | **31** | 35 | 34 | 34 | 35 | 35 |
|
|
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 |
|
|
371
442
|
| [Startup-Verband](https://startupverband.de/) | 43 | **30** | 31 | **30** | 31 | 31 | 31 |
|
|
372
|
-
| [TetraLogical](https://tetralogical.com/) |
|
|
443
|
+
| [TetraLogical](https://tetralogical.com/) | 48 | **22** | 39 | 41 | 42 | 42 | 42 |
|
|
373
444
|
| [TPGi](https://www.tpgi.com/) | 173 | **157** | 159 | 163 | 164 | 170 | 170 |
|
|
374
445
|
| [United Nations](https://www.un.org/en/) | 151 | **112** | 121 | 125 | 125 | 130 | 123 |
|
|
375
446
|
| [Vivaldi](https://vivaldi.com/) | 93 | **74** | n/a | 79 | 81 | 84 | 82 |
|
|
376
|
-
| [W3C](https://www.w3.org/) | 51 | **36** | 39 |
|
|
377
|
-
| **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) |
|
|
378
449
|
|
|
379
|
-
(Last updated: Jan
|
|
450
|
+
(Last updated: Jan 20, 2026)
|
|
380
451
|
<!-- End auto-generated -->
|
|
381
452
|
|
|
382
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
|
@@ -283,6 +283,8 @@ program.option('--input-dir <dir>', 'Specify an input directory');
|
|
|
283
283
|
program.option('--ignore-dir <patterns>', 'Exclude directories—relative to input directory—from processing (comma-separated), e.g., “libs” or “libs,vendor,node_modules”');
|
|
284
284
|
program.option('--output-dir <dir>', 'Specify an output directory');
|
|
285
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'));
|
|
286
288
|
|
|
287
289
|
(async () => {
|
|
288
290
|
let content;
|