html-minifier-next 5.0.5 → 5.1.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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/html-minifier-next.svg)](https://www.npmjs.com/package/html-minifier-next) [![Build status](https://github.com/j9t/html-minifier-next/workflows/Tests/badge.svg)](https://github.com/j9t/html-minifier-next/actions) [![Socket](https://badge.socket.dev/npm/package/html-minifier-next)](https://socket.dev/npm/package/html-minifier-next)
4
4
 
5
- Your HTML 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.
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
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.
8
8
 
@@ -132,6 +132,7 @@ Options can be used in config files (camelCase) or via CLI flags (kebab-case wit
132
132
  | --- | --- | --- |
133
133
  | `cacheCSS`<br>`--cache-css` | Set CSS minification cache size; higher values improve performance for batch processing | `500` |
134
134
  | `cacheJS`<br>`--cache-js` | Set JavaScript minification cache size; higher values improve performance for batch processing | `500` |
135
+ | `cacheSVG`<br>`--cache-svg` | Set SVG minification cache size; higher values improve performance for batch processing | `500` |
135
136
  | `caseSensitive`<br>`--case-sensitive` | Treat attributes in case-sensitive manner (useful for custom HTML elements) | `false` |
136
137
  | `collapseAttributeWhitespace`<br>`--collapse-attribute-whitespace` | Trim and collapse whitespace characters within attribute values | `false` |
137
138
  | `collapseBooleanAttributes`<br>`--collapse-boolean-attributes` | [Omit attribute values from boolean attributes](https://perfectionkills.com/experimenting-with-html-minifier/#collapse_boolean_attributes) | `false` |
@@ -155,8 +156,8 @@ Options can be used in config files (camelCase) or via CLI flags (kebab-case wit
155
156
  | `maxLineLength`<br>`--max-line-length` | Specify a maximum line length; compressed output will be split by newlines at valid HTML split-points | `undefined` |
156
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` |
157
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)`) |
158
- | `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)`) |
159
- | `minifySVG`<br>`--minify-svg` | Minify SVG elements and attributes (numeric precision, default attributes, colors) | `false` (could be `true`, `Object`) |
159
+ | `minifyJS`<br>`--minify-js` | Minify JavaScript in `script` elements and event attributes (uses [Terser](https://terser.org/) or [SWC](https://swc.rs/)) | `false` (could be `true`, `Object`, `Function(text, inline)`) |
160
+ | `minifySVG`<br>`--minify-svg` | Minify SVG elements (uses [SVGO](https://svgo.dev/)) | `false` (could be `true`, `Object`) |
160
161
  | `minifyURLs`<br>`--minify-urls` | Minify URLs in various attributes | `false` (could be `true`, `String`, `Object`, `Function(text)`) |
161
162
  | `noNewlinesBeforeTagClose`<br>`--no-newlines-before-tag-close` | Never add a newline before a tag that closes an element | `false` |
162
163
  | `partialMarkup`<br>`--partial-markup` | Treat input as a partial HTML fragment, preserving stray end tags (closing tags without opening tags) and preventing auto-closing of unclosed tags at end of input | `false` |
@@ -225,7 +226,7 @@ const result = await minify(html, {
225
226
 
226
227
  ### JavaScript minification
227
228
 
228
- When `minifyJS` is set to `true`, HTML Minifier Next uses [Terser](https://github.com/terser/terser) by default to minify JavaScript in `<script>` elements and event attributes.
229
+ When `minifyJS` is set to `true`, HTML Minifier Next uses [Terser](https://terser.org/) by default to minify JavaScript in `<script>` elements and event attributes.
229
230
 
230
231
  You can choose between different JS minifiers using the `engine` field:
231
232
 
@@ -285,7 +286,40 @@ const result = await minify(html, {
285
286
  });
286
287
  ```
287
288
 
288
- ### CSS and JavaScript cache configuration
289
+ ### SVG minification
290
+
291
+ When `minifySVG` is set to `true`, HTML Minifier Next uses [SVGO](https://svgo.dev/) to optimize inline SVG elements. Complete `<svg>` subtrees are extracted and processed as a block, enabling deep structural optimization:
292
+
293
+ ```javascript
294
+ const result = await minify(html, {
295
+ minifySVG: true // Enable with SVGO defaults
296
+ });
297
+ ```
298
+
299
+ You can pass custom SVGO options:
300
+
301
+ ```javascript
302
+ const result = await minify(html, {
303
+ minifySVG: {
304
+ plugins: [{
305
+ name: 'preset-default',
306
+ params: {
307
+ overrides: {
308
+ convertShapeToPath: false // Keep original shapes
309
+ }
310
+ }
311
+ }]
312
+ }
313
+ });
314
+ ```
315
+
316
+ **Important:**
317
+
318
+ * SVG minification only applies within `<svg>` elements
319
+ * Case sensitivity and self-closing slashes are automatically preserved in SVG (regardless of global settings)
320
+ * For maximum compression, use `minifySVG` together with `collapseWhitespace` and other options
321
+
322
+ ### CSS, JavaScript, and SVG cache configuration
289
323
 
290
324
  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:
291
325
 
@@ -293,16 +327,18 @@ HTML Minifier Next uses in-memory caches to improve performance when processing
293
327
  const result = await minify(html, {
294
328
  minifyCSS: true,
295
329
  minifyJS: true,
330
+ minifySVG: true,
296
331
  // Configure cache sizes (in number of entries)
297
332
  cacheCSS: 750, // CSS cache size, default: 500
298
- cacheJS: 250 // JS cache size, default: 500
333
+ cacheJS: 250, // JS cache size, default: 500
334
+ cacheSVG: 100 // SVG cache size, default: 500
299
335
  });
300
336
  ```
301
337
 
302
338
  **Via CLI flags:**
303
339
 
304
340
  ```shell
305
- html-minifier-next --minify-css --cache-css 750 --minify-js --cache-js 250 input.html
341
+ html-minifier-next --minify-css --cache-css 750 --minify-js --cache-js 250 --minify-svg --cache-svg 100 input.html
306
342
  ```
307
343
 
308
344
  **Via environment variables:**
@@ -310,7 +346,8 @@ html-minifier-next --minify-css --cache-css 750 --minify-js --cache-js 250 input
310
346
  ```shell
311
347
  export HMN_CACHE_CSS=750
312
348
  export HMN_CACHE_JS=250
313
- html-minifier-next --minify-css --minify-js input.html
349
+ export HMN_CACHE_SVG=100
350
+ html-minifier-next --minify-css --minify-js --minify-svg input.html
314
351
  ```
315
352
 
316
353
  **Configuration file:**
@@ -320,7 +357,9 @@ html-minifier-next --minify-css --minify-js input.html
320
357
  "minifyCSS": true,
321
358
  "cacheCSS": 750,
322
359
  "minifyJS": true,
323
- "cacheJS": 250
360
+ "cacheJS": 250,
361
+ "minifySVG": true,
362
+ "cacheSVG": 100
324
363
  }
325
364
  ```
326
365
 
@@ -333,68 +372,11 @@ html-minifier-next --minify-css --minify-js input.html
333
372
 
334
373
  **Important:**
335
374
 
336
- * 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.
375
+ * 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`, or `cacheSVG` options are provided. The first call’s options determine the cache sizes.
337
376
  * 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`.
338
377
 
339
378
  The caches persist across multiple `minify()` calls, making them particularly effective when processing many files in a batch operation.
340
379
 
341
- ### SVG minification
342
-
343
- 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
-
345
- ```javascript
346
- const result = await minify(html, {
347
- minifySVG: true // Enable with default settings
348
- });
349
- ```
350
-
351
- What gets optimized:
352
-
353
- 1. Numeric precision reduction: Coordinates and path data are rounded to 3 decimal places by default
354
- - `<path d="M 0.00000000 0.00000000"/>` → `<path d="M 0 0"/>`
355
- - `<circle cx="10.500000" cy="20.300000" r="5.000"/>` → `<circle cx="10.5" cy="20.3" r="5"/>`
356
-
357
- 2. Whitespace removal: Excess whitespace in numeric attribute values is removed
358
- - `transform="translate( 10 , 20 )"` → `transform="translate(10,20)"`
359
- - `points="100, 10 40, 198"` → `points="100,10 40,198"`
360
-
361
- 3. Color minification: Hex colors are shortened and RGB values converted to hex
362
- - `fill="#000000"` → `fill="#000"`
363
- - `fill="rgb(255,255,255)"` → `fill="#fff"`
364
-
365
- 4. Default attribute removal: Well-documented SVG default attributes are removed
366
- - `fill-opacity="1"` → removed
367
- - `stroke-linecap="butt"` → removed
368
-
369
- 5. Leading and trailing zero removal: Unnecessary zeros are stripped from numeric values
370
- - `0.5` → `.5`, `-0.3` → `-.3` (leading zeros)
371
- - `1.0` → `1`, `10.500` → `10.5` (trailing zeros)
372
-
373
- You can customize the optimization behavior by providing an options object:
374
-
375
- ```javascript
376
- const result = await minify(html, {
377
- minifySVG: {
378
- precision: 2, // Use 2 decimal places instead of 3
379
- removeDefaults: true, // Remove default attributes (default: true)
380
- minifyColors: true // Minify color values (default: true)
381
- }
382
- });
383
- ```
384
-
385
- Available options:
386
-
387
- * `precision`: Number of decimal places for coordinates and path data (default: `3`)
388
- * `removeDefaults`: Remove attributes with default values (default: `true`)
389
- * `minifyColors`: Minify color values with hex shortening and RGB-to-hex conversion (default: `true`)
390
-
391
- **Important:**
392
-
393
- * SVG minification only applies within `<svg>` elements
394
- * Case sensitivity and self-closing slashes are automatically preserved in SVG (regardless of global settings)
395
- * For maximum compression, use `minifySVG` together with `collapseWhitespace` and other options
396
- * This is a lightweight, built-in implementation; for more aggressive SVG optimization, consider using [SVGO](https://github.com/svg/svgo) as a separate build step
397
-
398
380
  ## Minification comparison
399
381
 
400
382
  Please see [**the Minifier Benchmarks project**](https://github.com/j9t/minifier-benchmarks) for details on how HMN compares to other minifiers.
package/cli.js CHANGED
@@ -245,6 +245,7 @@ program.option('-p --preset <name>', `Use a preset configuration (${getPresetNam
245
245
  program.option('-c --config-file <file>', 'Use config file');
246
246
  program.option('--cache-css <size>', 'Set CSS minification cache size (number of entries, default: 500)', parseValidInt('cacheCSS'));
247
247
  program.option('--cache-js <size>', 'Set JavaScript minification cache size (number of entries, default: 500)', parseValidInt('cacheJS'));
248
+ program.option('--cache-svg <size>', 'Set SVG minification cache size (number of entries, default: 500)', parseValidInt('cacheSVG'));
248
249
  program.version(pkg.version, '-V, --version', 'Output the version number');
249
250
  program.helpOption('-h, --help', 'Display help for command');
250
251