html-minifier-next 6.2.7 → 6.2.9

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 CHANGED
@@ -4,17 +4,11 @@
4
4
 
5
5
  Your web page optimization precision tool: HTML Minifier Next (HMN) is a **super-configurable, well-tested, JavaScript-based HTML minifier** able to also handle in-document CSS, JavaScript, and SVG minification.
6
6
 
7
- The project was based on [HTML Minifier Terser (HMT)](https://github.com/terser/html-minifier-terser), which in turn had been based on [Juriy “kangax” Zaytsev’s HTML Minifier (HM)](https://github.com/kangax/html-minifier); as of 2025, both HTML Minifier Terser and HTML Minifier had been unmaintained for several years. HMN offers additional features and has been optimized for speed. While an independent project, it is still backwards-compatible with HMT and HM.
7
+ The project was based on [HTML Minifier Terser (HMT)](https://github.com/terser/html-minifier-terser), which in turn had been based on [Juriy “kangax” Zaytsev’s HTML Minifier (HM)](https://github.com/kangax/html-minifier). It is now **the official successor to HTML Minifier**. HMN is maintained, easier to use, offers new features, and has been optimized for speed.
8
8
 
9
9
  ## Installation
10
10
 
11
- From npm for use as a command-line app:
12
-
13
- ```shell
14
- npm i -g html-minifier-next
15
- ```
16
-
17
- Directly with npx:
11
+ For use as a command-line app, use npx:
18
12
 
19
13
  ```shell
20
14
  npx html-minifier-next --help
@@ -22,30 +16,30 @@ npx html-minifier-next --help
22
16
 
23
17
  (For immediate, zero-config use in the current folder: `npx html-minifier-next --zero`)
24
18
 
25
- From npm for programmatic use:
19
+ For programmatic use, install as a development dependency:
26
20
 
27
21
  ```shell
28
- npm i html-minifier-next
22
+ npm i -D html-minifier-next
29
23
  ```
30
24
 
31
25
  ## General usage
32
26
 
33
27
  ### CLI
34
28
 
35
- Use `html-minifier-next --help` to check all available options:
29
+ Use `npx html-minifier-next --help` to check all available options:
36
30
 
37
31
  | Option | Description | Example |
38
32
  | --- | --- | --- |
39
- | `--zero`, `-z` | Minify all HTML files in the current folder and its subfolders in place (except node_modules), using comprehensive settings (standalone—flag is ignored when combined with other options) | `html-minifier-next --zero` |
33
+ | `--zero`, `-z` | Minify all HTML files in the current folder and its subfolders in place (except node_modules), using comprehensive settings (standalone—flag is ignored when combined with other options) | `npx html-minifier-next --zero` |
40
34
  | `--input-dir <dir>`, `-I <dir>` | Specify an input directory | `--input-dir=src` |
41
35
  | `--ignore-dir <patterns>`, `-X <patterns>` | Exclude directories—relative to input directory—from processing (comma-separated, overrides config file setting) | `--ignore-dir=libs`, `--ignore-dir=libs,vendor,node_modules` |
42
36
  | `--output-dir <dir>`, `-O <dir>` | Specify an output directory | `--output-dir=dist` |
43
- | `--output <file>`, `-o <file>` | Specify output file (reads from file arguments or STDIN) | File to file: `html-minifier-next input.html -o output.html`<br>Pipe to file: `cat input.html \| html-minifier-next -o output.html`<br>File to STDOUT: `html-minifier-next input.html` |
37
+ | `--output <file>`, `-o <file>` | Specify output file (reads from file arguments or STDIN) | File to file: `npx html-minifier-next input.html -o output.html`<br>Pipe to file: `cat input.html \| npx html-minifier-next -o output.html`<br>File to STDOUT: `npx html-minifier-next input.html` |
44
38
  | `--file-ext <extensions>`, `-f <extensions>` | Specify file extension(s) to process (comma-separated, overrides config file setting); defaults to `html,htm,shtml,shtm`; use `*` for all files | `--file-ext=html,php`, `--file-ext='*'` |
45
39
  | `--preset <name>`, `-p <name>` | Use a preset configuration (conservative or comprehensive) | `--preset=conservative` |
46
40
  | `--config-file <file>`, `-c <file>` | Use a configuration file | `--config-file=html-minifier.json` |
47
- | `--verbose`, `-v` | Show detailed processing information (active options, file statistics) | `html-minifier-next --input-dir=src --output-dir=dist --verbose --collapse-whitespace` |
48
- | `--dry`, `-d` | Dry run: Process and report statistics without writing output | `html-minifier-next input.html --dry --collapse-whitespace` |
41
+ | `--verbose`, `-v` | Show detailed processing information (active options, file statistics) | `npx html-minifier-next --input-dir=src --output-dir=dist --verbose --collapse-whitespace` |
42
+ | `--dry`, `-d` | Dry run: Process and report statistics without writing output | `npx html-minifier-next input.html --dry --collapse-whitespace` |
49
43
 
50
44
  ### Configuration file
51
45
 
@@ -113,14 +107,14 @@ To review the specific options set, [presets.js](https://github.com/j9t/html-min
113
107
 
114
108
  ```shell
115
109
  # Via CLI flag
116
- html-minifier-next --preset conservative input.html
110
+ npx html-minifier-next --preset conservative input.html
117
111
 
118
112
  # Via config file
119
- html-minifier-next --config-file=html-minifier.json input.html
113
+ npx html-minifier-next --config-file=html-minifier.json input.html
120
114
  # where html-minifier.json contains: { "preset": "conservative" }
121
115
 
122
116
  # Override preset options
123
- html-minifier-next --preset conservative --remove-empty-attributes input.html
117
+ npx html-minifier-next --preset conservative --remove-empty-attributes input.html
124
118
  ```
125
119
 
126
120
  **Priority order:** Presets are applied first, then config file options, then CLI flags. This allows you to start with a preset and customize as needed.
@@ -246,14 +240,12 @@ const result = await minify(html, {
246
240
  * `terser` (default): The standard JavaScript minifier with excellent compression
247
241
  * [`swc`](https://swc.rs/): Rust-based minifier that’s significantly faster than Terser (requires separate installation)
248
242
 
249
- **To use SWC**, install it as a dependency:
243
+ **To use SWC**, install it as a development dependency:
250
244
 
251
245
  ```shell
252
- npm i @swc/core
246
+ npm i -D @swc/core
253
247
  ```
254
248
 
255
- (Build-only users may want to install it as a dev dependency: `npm i -D @swc/core`.)
256
-
257
249
  **Important:** Inline event handlers (e.g., `onclick="return false"`) always use Terser regardless of the `engine` setting, as SWC doesn’t support bare return statements. This is handled automatically—you don’t need to do anything special.
258
250
 
259
251
  You can pass engine-specific configuration options:
@@ -340,7 +332,7 @@ const result = await minify(html, {
340
332
  **Via CLI flags:**
341
333
 
342
334
  ```shell
343
- html-minifier-next --minify-css --cache-css 750 --minify-js --cache-js 250 --minify-svg --cache-svg 100 input.html
335
+ npx html-minifier-next --minify-css --cache-css 750 --minify-js --cache-js 250 --minify-svg --cache-svg 100 input.html
344
336
  ```
345
337
 
346
338
  **Via environment variables:**
@@ -349,7 +341,7 @@ html-minifier-next --minify-css --cache-css 750 --minify-js --cache-js 250 --min
349
341
  export HMN_CACHE_CSS=750
350
342
  export HMN_CACHE_JS=250
351
343
  export HMN_CACHE_SVG=100
352
- html-minifier-next --minify-css --minify-js --minify-svg input.html
344
+ npx html-minifier-next --minify-css --minify-js --minify-svg input.html
353
345
  ```
354
346
 
355
347
  **Configuration file:**
@@ -390,12 +382,8 @@ Please see [**the Minifier Benchmarks project**](https://github.com/j9t/minifier
390
382
  **Sample command line:**
391
383
 
392
384
  ```shell
393
- html-minifier-next --collapse-whitespace --remove-comments --minify-js --input-dir=. --output-dir=example
394
- ```
395
-
396
- Example using npx:
385
+ npx html-minifier-next --collapse-whitespace --remove-comments --minify-js --input-dir=. --output-dir=example
397
386
 
398
- ```shell
399
387
  npx html-minifier-next --input-dir=test --preset comprehensive --output-dir example
400
388
  ```
401
389
 
@@ -403,39 +391,39 @@ npx html-minifier-next --input-dir=test --preset comprehensive --output-dir exam
403
391
 
404
392
  ```shell
405
393
  # Process default extensions (html, htm, shtml, shtm)
406
- html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist
394
+ npx html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist
407
395
 
408
396
  # Process only specific extensions
409
- html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --file-ext=html,php
397
+ npx html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --file-ext=html,php
410
398
 
411
399
  # Using configuration file that sets `fileExt` (e.g., `"fileExt": "html,php"`)
412
- html-minifier-next --config-file=html-minifier.json --input-dir=src --output-dir=dist
400
+ npx html-minifier-next --config-file=html-minifier.json --input-dir=src --output-dir=dist
413
401
 
414
402
  # Process all files (explicit wildcard)
415
- html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --file-ext='*'
403
+ npx html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --file-ext='*'
416
404
  ```
417
405
 
418
406
  **Exclude directories from processing:**
419
407
 
420
408
  ```shell
421
409
  # Ignore a single directory
422
- html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --ignore-dir=libs
410
+ npx html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --ignore-dir=libs
423
411
 
424
412
  # Ignore multiple directories
425
- html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --ignore-dir=libs,vendor,node_modules
413
+ npx html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --ignore-dir=libs,vendor,node_modules
426
414
 
427
415
  # Ignore by relative path (only ignores src/static/libs, not other “libs” directories)
428
- html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --ignore-dir=static/libs
416
+ npx html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --ignore-dir=static/libs
429
417
  ```
430
418
 
431
419
  **Dry run mode (preview outcome without writing files):**
432
420
 
433
421
  ```shell
434
422
  # Preview with output file
435
- html-minifier-next input.html -o output.html --dry --collapse-whitespace
423
+ npx html-minifier-next input.html -o output.html --dry --collapse-whitespace
436
424
 
437
425
  # Preview directory processing with statistics per file and total
438
- html-minifier-next --input-dir=src --output-dir=dist --dry --collapse-whitespace
426
+ npx html-minifier-next --input-dir=src --output-dir=dist --dry --collapse-whitespace
439
427
  # Output: [DRY RUN] Would process directory: src → dist
440
428
  # index.html: 1,234 → 892 bytes (-342, 27.7%)
441
429
  # about.html: 2,100 → 1,654 bytes (-446, 21.2%)
@@ -447,7 +435,7 @@ html-minifier-next --input-dir=src --output-dir=dist --dry --collapse-whitespace
447
435
 
448
436
  ```shell
449
437
  # Show processing details while minifying
450
- html-minifier-next --input-dir=src --output-dir=dist --verbose --collapse-whitespace
438
+ npx html-minifier-next --input-dir=src --output-dir=dist --verbose --collapse-whitespace
451
439
  # Output: CLI options: collapseWhitespace
452
440
  # ✓ src/index.html: 1,234 → 892 bytes (-342, 27.7%)
453
441
  # ✓ src/about.html: 2,100 → 1,654 bytes (-446, 21.2%)
@@ -455,7 +443,7 @@ html-minifier-next --input-dir=src --output-dir=dist --verbose --collapse-whites
455
443
  # Total: 3,334 → 2,546 bytes (-788, 23.6%)
456
444
 
457
445
  # `--dry` automatically enables verbose output
458
- html-minifier-next --input-dir=src --output-dir=dist --dry --collapse-whitespace
446
+ npx html-minifier-next --input-dir=src --output-dir=dist --dry --collapse-whitespace
459
447
  ```
460
448
 
461
449
  ## Special cases
@@ -560,13 +548,13 @@ ignoreCustomFragments: [/\{%[\s\S]{0,1000}?%\}/, /\{\{[\s\S]{0,500}?\}\}/]
560
548
  **CLI (via config file—recommended):**
561
549
 
562
550
  ```shell
563
- html-minifier-next --config-file=config.json input.html
551
+ npx html-minifier-next --config-file=config.json input.html
564
552
  ```
565
553
 
566
554
  **CLI (inline—not recommended due to complex escaping):**
567
555
 
568
556
  ```shell
569
- html-minifier-next --ignore-custom-fragments '[\\\"\\\\{%[\\\\s\\\\S]{0,1000}?%\\\\}\\\"]' input.html
557
+ npx html-minifier-next --ignore-custom-fragments '[\\\"\\\\{%[\\\\s\\\\S]{0,1000}?%\\\\}\\\"]' input.html
570
558
  ```
571
559
 
572
560
  For CLI usage, using a config file is strongly recommended to avoid complex shell and JSON escaping.
@@ -577,7 +565,9 @@ For CLI usage, using a config file is strongly recommended to avoid complex shel
577
565
  \{%[\s\S]{0,1000}?%\} \{\{[\s\S]{0,500}?\}\}
578
566
  ```
579
567
 
580
- ## Running HTML Minifier Next locally
568
+ ## Working on HTML Minifier Next
569
+
570
+ Note: This section assumes working with main dependencies installed (`npm i`).
581
571
 
582
572
  ### Local server
583
573
 
@@ -593,7 +583,7 @@ npm i;
593
583
  npm run backtest
594
584
  ```
595
585
 
596
- The backtest tool tracks minification performance across Git history. Results are saved in the backtest folder as CSV and JSON files.
586
+ The backtest tool tracks minification performance across Git history. Results are saved in the backtest folder as a JSON file, results.json.
597
587
 
598
588
  Parameters:
599
589
 
@@ -601,6 +591,38 @@ Parameters:
601
591
  * `COUNT`: Tests last `COUNT` commits (e.g., `npm run backtest 100`)
602
592
  * `COUNT/STEP`: Tests last `COUNT` commits, sampling every `STEP`th commit (e.g., `npm run backtest 500/10` tests 50 commits)
603
593
 
594
+ ### Working tree benchmarks
595
+
596
+ Where the backtest walks Git history, the benchmark times the code as it is _right now_—useful for A/B testing a branch against a saved baseline:
597
+
598
+ ```shell
599
+ cd backtest;
600
+ npm i;
601
+ npm run benchmark
602
+ ```
603
+
604
+ It reuses the backtest corpus (run `npm run backtest` once to download it) and reports per-file output size and median processing time.
605
+
606
+ Parameters:
607
+
608
+ * No argument: Runs and, if a baseline exists, shows size and time deltas
609
+ * `--save`: Saves the run as the baseline (e.g., on `main` before switching to a branch)
610
+ * `--core`: Disables the external minifiers (CSS, JS, SVG, URLs) to isolate HMN’s own processing time
611
+ * `--iterations=N`: Sets the number of timed iterations (default 5; the median is reported)
612
+ * `--config=PATH`: Uses an alternative options file (default `html-minifier.json`)
613
+
614
+ To compare branches (A/B run), execute `npm run benchmark -- --save` on `main`, then `npm run benchmark` on the branch to see the deltas. Add `--core` on both ends when measuring changes to HMN’s own code rather than the bundled minifiers.
615
+
616
+ #### Profiling
617
+
618
+ To profile the current working tree, run the benchmark with Node’s built-in CPU profiler:
619
+
620
+ ```shell
621
+ node --cpu-prof benchmark.js
622
+ ```
623
+
624
+ This writes a `.cpuprofile` file to the working directory. Load it with `npx speedscope *.cpuprofile` for a flamegraph, or drag it into Chrome DevTools → Sources → JavaScript Profiler. Compare self-time per function against a clean baseline run on `main`. Pay attention to unexpectedly heavy callbacks in hot paths—V8 de-optimization from variable object shapes or unnecessary method calls can show up there.
625
+
604
626
  ## Acknowledgements
605
627
 
606
628
  With many thanks to the previous authors of and contributors to HTML Minifier, especially [Juriy “kangax” Zaytsev](https://github.com/kangax), and to everyone who helped make this new edition better, particularly [Daniel Ruf](https://github.com/DanielRuf), [Jonas Geiler](https://github.com/jonasgeiler), and [Chris Morgan](https://github.com/chris-morgan)!
@@ -609,6 +631,6 @@ With many thanks to the previous authors of and contributors to HTML Minifier, e
609
631
 
610
632
  You might like some of my other work:
611
633
 
612
- * Optimization tools: HTML Minifier Next · [ObsoHTML](https://github.com/j9t/obsohtml) · [Image Guard](https://github.com/j9t/image-guard) · [Compressor.js Next](https://github.com/j9t/compressorjs-next) · [.htaccess Punk](https://github.com/j9t/htaccess-punk)
634
+ * Optimization tools: [hihtml](https://github.com/j9t/hihtml) · HTML Minifier Next · [ObsoHTML](https://github.com/j9t/obsohtml) · [Image Guard](https://github.com/j9t/image-guard) · [Compressor.js Next](https://github.com/j9t/compressorjs-next) · [.htaccess Punk](https://github.com/j9t/htaccess-punk)
613
635
  * Defense tools: [IA Defensa](https://iadefensa.com/solutions/)
614
636
  * Resources for quality web development: [Articles](https://meiert.com/topics/development/) · [Books](https://meiert.com/topics/books/) (including [_On Web Development_](https://meiert.com/blog/on-web-development-2/)) · [News](https://frontenddogma.com/) · [Terminology](https://webglossary.info/)