html-minifier-next 1.2.1 → 1.3.3
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 +107 -42
- package/cli.js +40 -5
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
# HTML Minifier Next
|
|
1
|
+
# HTML Minifier Next
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/html-minifier-next)
|
|
4
|
-
|
|
4
|
+
[](https://github.com/j9t/html-minifier-next/actions?workflow=CI)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
HTML Minifier is a highly **configurable, well-tested, JavaScript-based HTML minifier**.
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
The project has been based on [Terser’s html-minifier-terser](https://github.com/terser/html-minifier-terser), which in turn had been based on [Juriy Zaytsev’s html-minifier](https://github.com/kangax/html-minifier). It was set up because as of 2025, both html-minifier-terser and html-minifier have been unmaintained for some time. As the project seems maintainable [to me, [Jens](https://meiert.com/)]—even more so with community support—, it will be updated and documented further in this place.
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
@@ -25,23 +25,100 @@ npm i html-minifier-next
|
|
|
25
25
|
|
|
26
26
|
**Note** that almost all options are disabled by default. Experiment and find what works best for you and your project.
|
|
27
27
|
|
|
28
|
-
For command line usage please see `html-minifier-next --help` for a list of available options.
|
|
29
|
-
|
|
30
28
|
**Sample command line:**
|
|
31
29
|
|
|
32
30
|
```bash
|
|
33
31
|
html-minifier-next --collapse-whitespace --remove-comments --minify-js true --input-dir=. --output-dir=example
|
|
34
32
|
```
|
|
35
33
|
|
|
34
|
+
**Process specific file extensions:**
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
# Process only HTML files (CLI method)
|
|
38
|
+
html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --file-ext=html
|
|
39
|
+
|
|
40
|
+
# Process multiple file extensions (CLI method)
|
|
41
|
+
html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist --file-ext=html,htm,php
|
|
42
|
+
|
|
43
|
+
# Using configuration file that sets `fileExt` (e.g., `"fileExt": "html,htm"`)
|
|
44
|
+
html-minifier-next --config-file=html-minifier.json --input-dir=src --output-dir=dist
|
|
45
|
+
|
|
46
|
+
# Process all files (default behavior)
|
|
47
|
+
html-minifier-next --collapse-whitespace --input-dir=src --output-dir=dist
|
|
48
|
+
# Note: When processing all files, non-HTML files will also be read as UTF‑8 and passed to the minifier.
|
|
49
|
+
# Consider restricting with “--file-ext” to avoid touching binaries (e.g., images, archives).
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### CLI options
|
|
53
|
+
|
|
54
|
+
Use `html-minifier-next --help` to check all available options:
|
|
55
|
+
|
|
56
|
+
| Option | Description | Example |
|
|
57
|
+
| --- | --- | --- |
|
|
58
|
+
| `--input-dir <dir>` | Specify an input directory | `--input-dir=src` |
|
|
59
|
+
| `--output-dir <dir>` | Specify an output directory | `--output-dir=dist` |
|
|
60
|
+
| `--file-ext <extensions>` | Specify file extension(s) to process (overrides config file setting) | `--file-ext=html` or `--file-ext=html,htm,php` |
|
|
61
|
+
| `-o --output <file>` | Specify output file (single file mode) | `-o minified.html` |
|
|
62
|
+
| `-c --config-file <file>` | Use a configuration file | `--config-file=html-minifier.json` |
|
|
63
|
+
|
|
64
|
+
### Configuration file
|
|
65
|
+
|
|
66
|
+
You can also use a configuration file to specify options. The file can be either JSON format or a JavaScript module that exports the configuration object:
|
|
67
|
+
|
|
68
|
+
**JSON configuration example:**
|
|
69
|
+
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"collapseWhitespace": true,
|
|
73
|
+
"removeComments": true,
|
|
74
|
+
"fileExt": "html,htm"
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**JavaScript module configuration example:**
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
module.exports = {
|
|
82
|
+
collapseWhitespace: true,
|
|
83
|
+
removeComments: true,
|
|
84
|
+
fileExt: "html,htm"
|
|
85
|
+
};
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Using a configuration file:**
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
# Specify config file
|
|
92
|
+
html-minifier-next --config-file=html-minifier.json --input-dir=src --output-dir=dist
|
|
93
|
+
|
|
94
|
+
# CLI arguments override config file settings
|
|
95
|
+
html-minifier-next --config-file=html-minifier.json --file-ext=xml --input-dir=src --output-dir=dist
|
|
96
|
+
```
|
|
97
|
+
|
|
36
98
|
### Node.js
|
|
37
99
|
|
|
100
|
+
ESM with Node.js ≥16.14:
|
|
101
|
+
|
|
38
102
|
```js
|
|
39
|
-
|
|
103
|
+
import { minify } from 'html-minifier-next';
|
|
40
104
|
|
|
41
105
|
const result = await minify('<p title="blah" id="moo">foo</p>', {
|
|
42
106
|
removeAttributeQuotes: true,
|
|
43
107
|
});
|
|
44
|
-
result; // “<p title=blah id=moo>foo</p>”
|
|
108
|
+
console.log(result); // “<p title=blah id=moo>foo</p>”
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
CommonJS:
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
const { minify } = require('html-minifier-next');
|
|
115
|
+
|
|
116
|
+
(async () => {
|
|
117
|
+
const result = await minify('<p title="blah" id="moo">foo</p>', {
|
|
118
|
+
removeAttributeQuotes: true,
|
|
119
|
+
});
|
|
120
|
+
console.log(result);
|
|
121
|
+
})();
|
|
45
122
|
```
|
|
46
123
|
|
|
47
124
|
See [the original blog post](http://perfectionkills.com/experimenting-with-html-minifier) for details of [how it works](http://perfectionkills.com/experimenting-with-html-minifier#how_it_works), [description of each option](http://perfectionkills.com/experimenting-with-html-minifier#options), [testing results](http://perfectionkills.com/experimenting-with-html-minifier#field_testing), and [conclusions](http://perfectionkills.com/experimenting-with-html-minifier#cost_and_benefits).
|
|
@@ -50,26 +127,26 @@ For lint-like capabilities take a look at [HTMLLint](https://github.com/kangax/h
|
|
|
50
127
|
|
|
51
128
|
## Minification comparison
|
|
52
129
|
|
|
53
|
-
How does
|
|
130
|
+
How does HTML Minifier compare to other solutions, like [minimize](https://github.com/Swaagie/minimize) or [htmlcompressor.com](http://htmlcompressor.com/)?
|
|
54
131
|
|
|
55
132
|
| Site | Original size (KB) | HTMLMinifier | minimize | htmlcompressor.com |
|
|
56
133
|
| --- | --- | --- | --- | --- |
|
|
57
134
|
| [A List Apart](https://alistapart.com/) | 64 | **54** | 59 | 57 |
|
|
58
|
-
| [Amazon](https://www.amazon.com/) |
|
|
59
|
-
| [BBC](https://www.bbc.co.uk/) |
|
|
60
|
-
| [CSS-Tricks](https://css-tricks.com/) |
|
|
61
|
-
| [ECMAScript](https://tc39.es/ecma262/) |
|
|
62
|
-
| [EFF](https://www.eff.org/) |
|
|
63
|
-
| [
|
|
64
|
-
| [
|
|
65
|
-
| [
|
|
66
|
-
| [
|
|
67
|
-
| [
|
|
68
|
-
| [
|
|
69
|
-
| [
|
|
135
|
+
| [Amazon](https://www.amazon.com/) | 707 | **635** | 693 | n/a |
|
|
136
|
+
| [BBC](https://www.bbc.co.uk/) | 700 | **642** | 694 | n/a |
|
|
137
|
+
| [CSS-Tricks](https://css-tricks.com/) | 167 | **124** | 153 | 149 |
|
|
138
|
+
| [ECMAScript](https://tc39.es/ecma262/) | 7205 | **6365** | 6585 | n/a |
|
|
139
|
+
| [EFF](https://www.eff.org/) | 58 | **49** | 52 | 52 |
|
|
140
|
+
| [Eloquent JavaScript](https://eloquentjavascript.net/) | 6 | **5** | 6 | 5 |
|
|
141
|
+
| [FAZ](https://www.faz.net/aktuell/) | 1848 | **1727** | 1763 | n/a |
|
|
142
|
+
| [Frontend Dogma](https://frontenddogma.com/) | 118 | **113** | 127 | 117 |
|
|
143
|
+
| [Google](https://www.google.com/) | 50 | **46** | 50 | 50 |
|
|
144
|
+
| [HTMLMinifier](https://github.com/kangax/html-minifier) | 371 | **249** | 347 | n/a |
|
|
145
|
+
| [Mastodon](https://mastodon.social/explore) | 35 | **26** | 34 | 34 |
|
|
146
|
+
| [NBC](https://www.nbc.com/) | 579 | **528** | 572 | n/a |
|
|
147
|
+
| [New York Times](https://www.nytimes.com/) | 733 | **625** | 722 | n/a |
|
|
70
148
|
| [United Nations](https://www.un.org/) | 9 | **7** | 8 | 8 |
|
|
71
|
-
| [W3C](https://www.w3.org/) |
|
|
72
|
-
| [Wikipedia](https://en.wikipedia.org/wiki/Main_Page) | 225 | **204** | 215 | 215 |
|
|
149
|
+
| [W3C](https://www.w3.org/) | 51 | **36** | 42 | 40 |
|
|
73
150
|
|
|
74
151
|
## Options quick reference
|
|
75
152
|
|
|
@@ -77,16 +154,16 @@ Most of the options are disabled by default.
|
|
|
77
154
|
|
|
78
155
|
| Option | Description | Default |
|
|
79
156
|
| --- | --- | --- |
|
|
80
|
-
| `caseSensitive` | Treat attributes in case
|
|
157
|
+
| `caseSensitive` | Treat attributes in case-sensitive manner (useful for custom HTML elements) | `false` |
|
|
81
158
|
| `collapseBooleanAttributes` | [Omit attribute values from boolean attributes](http://perfectionkills.com/experimenting-with-html-minifier#collapse_boolean_attributes) | `false` |
|
|
82
159
|
| `customFragmentQuantifierLimit` | Set maximum quantifier limit for custom fragments to prevent ReDoS attacks | `200` |
|
|
83
160
|
| `collapseInlineTagWhitespace` | Don’t leave any spaces between `display:inline;` elements when collapsing. Must be used in conjunction with `collapseWhitespace=true` | `false` |
|
|
84
161
|
| `collapseWhitespace` | [Collapse white space that contributes to text nodes in a document tree](http://perfectionkills.com/experimenting-with-html-minifier#collapse_whitespace) | `false` |
|
|
85
162
|
| `conservativeCollapse` | Always collapse to 1 space (never remove it entirely). Must be used in conjunction with `collapseWhitespace=true` | `false` |
|
|
86
163
|
| `continueOnParseError` | [Handle parse errors](https://html.spec.whatwg.org/multipage/parsing.html#parse-errors) instead of aborting. | `false` |
|
|
87
|
-
| `customAttrAssign` | Arrays of regex’es that allow to support custom attribute assign expressions (e.g. `'<div flex?="{{mode != cover}}"></div>'`) | `[
|
|
164
|
+
| `customAttrAssign` | Arrays of regex’es that allow to support custom attribute assign expressions (e.g. `'<div flex?="{{mode != cover}}"></div>'`) | `[]` |
|
|
88
165
|
| `customAttrCollapse` | Regex that specifies custom attribute to strip newlines from (e.g. `/ng-class/`) | |
|
|
89
|
-
| `customAttrSurround` | Arrays of regexes that allow to support custom attribute surround expressions (e.g. `<input {{#if value}}checked="checked"{{/if}}>`) | `[
|
|
166
|
+
| `customAttrSurround` | Arrays of regexes that allow to support custom attribute surround expressions (e.g. `<input {{#if value}}checked="checked"{{/if}}>`) | `[]` |
|
|
90
167
|
| `customEventAttributes` | Arrays of regexes that allow to support custom event attributes for `minifyJS` (e.g. `ng-click`) | `[ /^on[a-z]{3,}$/ ]` |
|
|
91
168
|
| `decodeEntities` | Use direct Unicode characters whenever possible | `false` |
|
|
92
169
|
| `html5` | Parse input according to HTML5 specifications | `true` |
|
|
@@ -104,7 +181,7 @@ Most of the options are disabled by default.
|
|
|
104
181
|
| `preserveLineBreaks` | Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break. Must be used in conjunction with `collapseWhitespace=true` | `false` |
|
|
105
182
|
| `preventAttributesEscaping` | Prevents the escaping of the values of attributes | `false` |
|
|
106
183
|
| `processConditionalComments` | Process contents of conditional comments through minifier | `false` |
|
|
107
|
-
| `processScripts` | Array of strings corresponding to types of script elements to process through minifier (e.g. `text/ng-template`, `text/x-handlebars-template`, etc.) | `[
|
|
184
|
+
| `processScripts` | Array of strings corresponding to types of script elements to process through minifier (e.g. `text/ng-template`, `text/x-handlebars-template`, etc.) | `[]` |
|
|
108
185
|
| `quoteCharacter` | Type of quote to use for attribute values (“'” or “"”) | |
|
|
109
186
|
| `removeAttributeQuotes` | [Remove quotes around attributes when possible](http://perfectionkills.com/experimenting-with-html-minifier#remove_attribute_quotes) | `false` |
|
|
110
187
|
| `removeComments` | [Strip HTML comments](http://perfectionkills.com/experimenting-with-html-minifier#remove_comments) | `false` |
|
|
@@ -140,23 +217,11 @@ SVG elements are automatically recognized, and when they are minified, both case
|
|
|
140
217
|
|
|
141
218
|
### Working with invalid markup
|
|
142
219
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
Input markup (e.g. `<p id="">foo`)
|
|
146
|
-
|
|
147
|
-
↓
|
|
148
|
-
|
|
149
|
-
Internal representation of markup in a form of tree (e.g. `{ tag: "p", attr: "id", children: ["foo"] }`)
|
|
150
|
-
|
|
151
|
-
↓
|
|
152
|
-
|
|
153
|
-
Transformation of internal representation (e.g. removal of `id` attribute)
|
|
154
|
-
|
|
155
|
-
↓
|
|
220
|
+
HTML Minifier **can’t work with invalid or partial chunks of markup**. This is because it parses markup into a tree structure, then modifies it (removing anything that was specified for removal, ignoring anything that was specified to be ignored, etc.), then it creates a markup out of that tree and returns it.
|
|
156
221
|
|
|
157
|
-
Output of resulting markup (e.g. `<p>foo</p>`)
|
|
222
|
+
Input markup (e.g. `<p id="">foo`) → Internal representation of markup in a form of tree (e.g. `{ tag: "p", attr: "id", children: ["foo"] }`) → Transformation of internal representation (e.g. removal of `id` attribute) → Output of resulting markup (e.g. `<p>foo</p>`)
|
|
158
223
|
|
|
159
|
-
|
|
224
|
+
HTML Minifier can’t know that original markup was only half of the tree; it does its best to try to parse it as a full tree and it loses information about tree being malformed or partial in the beginning. As a result, it can’t create a partial/malformed tree at the time of the output.
|
|
160
225
|
|
|
161
226
|
## Security
|
|
162
227
|
|
package/cli.js
CHANGED
|
@@ -186,10 +186,18 @@ program.option('-c --config-file <file>', 'Use config file', function (configPat
|
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
188
|
});
|
|
189
|
+
|
|
190
|
+
// Handle fileExt in config file
|
|
191
|
+
if ('fileExt' in config) {
|
|
192
|
+
// Support both string (`html,htm`) and array (`["html", "htm"]`) formats
|
|
193
|
+
if (Array.isArray(config.fileExt)) {
|
|
194
|
+
config.fileExt = config.fileExt.join(',');
|
|
195
|
+
}
|
|
196
|
+
}
|
|
189
197
|
});
|
|
190
198
|
program.option('--input-dir <dir>', 'Specify an input directory');
|
|
191
199
|
program.option('--output-dir <dir>', 'Specify an output directory');
|
|
192
|
-
program.option('--file-ext <
|
|
200
|
+
program.option('--file-ext <extensions>', 'Specify file extension(s) to process (comma-separated), e.g., “html” or “html,htm,php”');
|
|
193
201
|
|
|
194
202
|
let content;
|
|
195
203
|
program.arguments('[files...]').action(function (files) {
|
|
@@ -241,7 +249,30 @@ function processFile(inputFile, outputFile) {
|
|
|
241
249
|
});
|
|
242
250
|
}
|
|
243
251
|
|
|
244
|
-
function
|
|
252
|
+
function parseFileExtensions(fileExt) {
|
|
253
|
+
if (!fileExt) return [];
|
|
254
|
+
const list = fileExt
|
|
255
|
+
.split(',')
|
|
256
|
+
.map(ext => ext.trim().replace(/^\.+/, '').toLowerCase())
|
|
257
|
+
.filter(ext => ext.length > 0);
|
|
258
|
+
return [...new Set(list)];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function shouldProcessFile(filename, fileExtensions) {
|
|
262
|
+
if (!fileExtensions || fileExtensions.length === 0) {
|
|
263
|
+
return true; // No extensions specified, process all files
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const fileExt = path.extname(filename).replace(/^\.+/, '').toLowerCase();
|
|
267
|
+
return fileExtensions.includes(fileExt);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function processDirectory(inputDir, outputDir, extensions) {
|
|
271
|
+
// If first call provided a string, normalize once; otherwise assume pre-parsed array
|
|
272
|
+
if (typeof extensions === 'string') {
|
|
273
|
+
extensions = parseFileExtensions(extensions);
|
|
274
|
+
}
|
|
275
|
+
|
|
245
276
|
fs.readdir(inputDir, function (err, files) {
|
|
246
277
|
if (err) {
|
|
247
278
|
fatal('Cannot read directory ' + inputDir + '\n' + err.message);
|
|
@@ -255,8 +286,8 @@ function processDirectory(inputDir, outputDir, fileExt) {
|
|
|
255
286
|
if (err) {
|
|
256
287
|
fatal('Cannot read ' + inputFile + '\n' + err.message);
|
|
257
288
|
} else if (stat.isDirectory()) {
|
|
258
|
-
processDirectory(inputFile, outputFile,
|
|
259
|
-
} else if (
|
|
289
|
+
processDirectory(inputFile, outputFile, extensions);
|
|
290
|
+
} else if (shouldProcessFile(file, extensions)) {
|
|
260
291
|
mkdir(outputDir, function () {
|
|
261
292
|
processFile(inputFile, outputFile);
|
|
262
293
|
});
|
|
@@ -290,13 +321,17 @@ const writeMinify = async () => {
|
|
|
290
321
|
|
|
291
322
|
const { inputDir, outputDir, fileExt } = programOptions;
|
|
292
323
|
|
|
324
|
+
// Resolve file extensions: CLI argument takes priority over config file, even if empty string
|
|
325
|
+
const hasCliFileExt = program.getOptionValueSource('fileExt') === 'cli';
|
|
326
|
+
const resolvedFileExt = hasCliFileExt ? fileExt : config.fileExt;
|
|
327
|
+
|
|
293
328
|
if (inputDir || outputDir) {
|
|
294
329
|
if (!inputDir) {
|
|
295
330
|
fatal('The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.');
|
|
296
331
|
} else if (!outputDir) {
|
|
297
332
|
fatal('You need to specify where to write the output files with the option --output-dir');
|
|
298
333
|
}
|
|
299
|
-
processDirectory(inputDir, outputDir,
|
|
334
|
+
processDirectory(inputDir, outputDir, resolvedFileExt);
|
|
300
335
|
} else if (content) { // Minifying one or more files specified on the CMD line
|
|
301
336
|
writeMinify();
|
|
302
337
|
} else { // Minifying input coming from STDIN
|
package/package.json
CHANGED
|
@@ -22,14 +22,14 @@
|
|
|
22
22
|
"@rollup/plugin-terser": "^0.4.4",
|
|
23
23
|
"alpinejs": "^3.14.9",
|
|
24
24
|
"commitlint-config-non-conventional": "^1.0.1",
|
|
25
|
-
"eslint": "^9.
|
|
25
|
+
"eslint": "^9.33.0",
|
|
26
26
|
"husky": "^9.1.7",
|
|
27
27
|
"is-ci": "^4.1.0",
|
|
28
28
|
"jest": "^30.0.5",
|
|
29
29
|
"lint-staged": "^16.1.5",
|
|
30
|
-
"rollup": "^4.
|
|
30
|
+
"rollup": "^4.46.2",
|
|
31
31
|
"rollup-plugin-polyfill-node": "^0.13.0",
|
|
32
|
-
"vite": "^7.
|
|
32
|
+
"vite": "^7.1.2"
|
|
33
33
|
},
|
|
34
34
|
"exports": {
|
|
35
35
|
".": {
|
|
@@ -88,5 +88,5 @@
|
|
|
88
88
|
"test:watch": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --watch"
|
|
89
89
|
},
|
|
90
90
|
"type": "module",
|
|
91
|
-
"version": "1.
|
|
91
|
+
"version": "1.3.3"
|
|
92
92
|
}
|