html-minifier-next 1.2.1 → 1.3.2
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 +68 -29
- package/cli.js +35 -3
- 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,14 +25,64 @@ 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 with fileExt setting
|
|
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
|
+
```
|
|
49
|
+
|
|
50
|
+
### CLI Options
|
|
51
|
+
|
|
52
|
+
Use `html-minifier-next --help` to check all available options:
|
|
53
|
+
|
|
54
|
+
| Option | Description | Example |
|
|
55
|
+
| --- | --- | --- |
|
|
56
|
+
| `--input-dir <dir>` | Specify an input directory | `--input-dir=src` |
|
|
57
|
+
| `--output-dir <dir>` | Specify an output directory | `--output-dir=dist` |
|
|
58
|
+
| `--file-ext <extensions>` | Specify file extension(s) to process (overrides config file setting) | `--file-ext=html` or `--file-ext=html,htm,php` |
|
|
59
|
+
| `-o --output <file>` | Specify output file (single file mode) | `-o minified.html` |
|
|
60
|
+
| `-c --config-file <file>` | Use a configuration file | `--config-file=html-minifier.json` |
|
|
61
|
+
|
|
62
|
+
### Configuration Files
|
|
63
|
+
|
|
64
|
+
You can also use a configuration file to specify options:
|
|
65
|
+
|
|
66
|
+
**JSON configuration example:**
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{
|
|
70
|
+
"collapseWhitespace": true,
|
|
71
|
+
"removeComments": true,
|
|
72
|
+
"fileExt": "html,htm"
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Using a configuration file:**
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
# Specify config file
|
|
80
|
+
html-minifier-next --config-file=html-minifier.json --input-dir=src --output-dir=dist
|
|
81
|
+
|
|
82
|
+
# CLI arguments override config file settings
|
|
83
|
+
html-minifier-next --config-file=html-minifier.json --file-ext=xml --input-dir=src --output-dir=dist
|
|
84
|
+
```
|
|
85
|
+
|
|
36
86
|
### Node.js
|
|
37
87
|
|
|
38
88
|
```js
|
|
@@ -50,10 +100,10 @@ For lint-like capabilities take a look at [HTMLLint](https://github.com/kangax/h
|
|
|
50
100
|
|
|
51
101
|
## Minification comparison
|
|
52
102
|
|
|
53
|
-
How does
|
|
103
|
+
How does HTML Minifier compare to other solutions, like [minimize](https://github.com/Swaagie/minimize) or [htmlcompressor.com](http://htmlcompressor.com/)?
|
|
54
104
|
|
|
55
|
-
| Site | Original size (KB) |
|
|
56
|
-
| --- | ---
|
|
105
|
+
| Site | Original size (KB) | HTML Minifier | minimize | htmlcompressor.com |
|
|
106
|
+
| --- | --- |---------------| --- | --- |
|
|
57
107
|
| [A List Apart](https://alistapart.com/) | 64 | **54** | 59 | 57 |
|
|
58
108
|
| [Amazon](https://www.amazon.com/) | 206 | **195** | 203 | 200 |
|
|
59
109
|
| [BBC](https://www.bbc.co.uk/) | 767 | **703** | 761 | n/a |
|
|
@@ -63,7 +113,7 @@ How does HTMLMinifier compare to other solutions, like [minimize](https://github
|
|
|
63
113
|
| [FAZ](https://www.faz.net/aktuell/) | 1767 | **1641** | 1679 | n/a |
|
|
64
114
|
| [Frontend Dogma](https://frontenddogma.com/) | 119 | **114** | 128 | 118 |
|
|
65
115
|
| [Google](https://www.google.com/) | 51 | **46** | 50 | 50 |
|
|
66
|
-
| [
|
|
116
|
+
| [HTML Minifier](https://github.com/kangax/html-minifier) | 373 | **250** | 349 | n/a |
|
|
67
117
|
| [Mastodon](https://mastodon.social/explore) | 37 | **28** | 36 | 36 |
|
|
68
118
|
| [NBC](https://www.nbc.com/) | 601 | **549** | 593 | n/a |
|
|
69
119
|
| [New York Times](https://www.nytimes.com/) | 822 | **701** | 811 | n/a |
|
|
@@ -77,18 +127,19 @@ Most of the options are disabled by default.
|
|
|
77
127
|
|
|
78
128
|
| Option | Description | Default |
|
|
79
129
|
| --- | --- | --- |
|
|
80
|
-
| `caseSensitive` | Treat attributes in case
|
|
130
|
+
| `caseSensitive` | Treat attributes in case-sensitive manner (useful for custom HTML elements) | `false` |
|
|
81
131
|
| `collapseBooleanAttributes` | [Omit attribute values from boolean attributes](http://perfectionkills.com/experimenting-with-html-minifier#collapse_boolean_attributes) | `false` |
|
|
82
132
|
| `customFragmentQuantifierLimit` | Set maximum quantifier limit for custom fragments to prevent ReDoS attacks | `200` |
|
|
83
133
|
| `collapseInlineTagWhitespace` | Don’t leave any spaces between `display:inline;` elements when collapsing. Must be used in conjunction with `collapseWhitespace=true` | `false` |
|
|
84
134
|
| `collapseWhitespace` | [Collapse white space that contributes to text nodes in a document tree](http://perfectionkills.com/experimenting-with-html-minifier#collapse_whitespace) | `false` |
|
|
85
135
|
| `conservativeCollapse` | Always collapse to 1 space (never remove it entirely). Must be used in conjunction with `collapseWhitespace=true` | `false` |
|
|
86
136
|
| `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>'`) | `[
|
|
137
|
+
| `customAttrAssign` | Arrays of regex’es that allow to support custom attribute assign expressions (e.g. `'<div flex?="{{mode != cover}}"></div>'`) | `[]` |
|
|
88
138
|
| `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}}>`) | `[
|
|
139
|
+
| `customAttrSurround` | Arrays of regexes that allow to support custom attribute surround expressions (e.g. `<input {{#if value}}checked="checked"{{/if}}>`) | `[]` |
|
|
90
140
|
| `customEventAttributes` | Arrays of regexes that allow to support custom event attributes for `minifyJS` (e.g. `ng-click`) | `[ /^on[a-z]{3,}$/ ]` |
|
|
91
141
|
| `decodeEntities` | Use direct Unicode characters whenever possible | `false` |
|
|
142
|
+
| `fileExt` | File extensions to process | `[]` (process all files) |
|
|
92
143
|
| `html5` | Parse input according to HTML5 specifications | `true` |
|
|
93
144
|
| `ignoreCustomComments` | Array of regexes that allow to ignore certain comments, when matched | `[ /^!/, /^\s*#/ ]` |
|
|
94
145
|
| `ignoreCustomFragments` | Array of regexes that allow to ignore certain fragments, when matched (e.g. `<?php ... ?>`, `{{ ... }}`, etc.) | `[ /<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/ ]` |
|
|
@@ -104,7 +155,7 @@ Most of the options are disabled by default.
|
|
|
104
155
|
| `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
156
|
| `preventAttributesEscaping` | Prevents the escaping of the values of attributes | `false` |
|
|
106
157
|
| `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.) | `[
|
|
158
|
+
| `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
159
|
| `quoteCharacter` | Type of quote to use for attribute values (“'” or “"”) | |
|
|
109
160
|
| `removeAttributeQuotes` | [Remove quotes around attributes when possible](http://perfectionkills.com/experimenting-with-html-minifier#remove_attribute_quotes) | `false` |
|
|
110
161
|
| `removeComments` | [Strip HTML comments](http://perfectionkills.com/experimenting-with-html-minifier#remove_comments) | `false` |
|
|
@@ -140,23 +191,11 @@ SVG elements are automatically recognized, and when they are minified, both case
|
|
|
140
191
|
|
|
141
192
|
### Working with invalid markup
|
|
142
193
|
|
|
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
|
-
↓
|
|
194
|
+
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
195
|
|
|
157
|
-
Output of resulting markup (e.g. `<p>foo</p>`)
|
|
196
|
+
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
197
|
|
|
159
|
-
|
|
198
|
+
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
199
|
|
|
161
200
|
## Security
|
|
162
201
|
|
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 <text>', 'Specify
|
|
200
|
+
program.option('--file-ext <text>', 'Specify file extension(s) to be read, e.g. “html” or “html,htm”');
|
|
193
201
|
|
|
194
202
|
let content;
|
|
195
203
|
program.arguments('[files...]').action(function (files) {
|
|
@@ -241,7 +249,28 @@ function processFile(inputFile, outputFile) {
|
|
|
241
249
|
});
|
|
242
250
|
}
|
|
243
251
|
|
|
252
|
+
function parseFileExtensions(fileExt) {
|
|
253
|
+
if (!fileExt) {
|
|
254
|
+
return [];
|
|
255
|
+
}
|
|
256
|
+
return fileExt
|
|
257
|
+
.split(',')
|
|
258
|
+
.map(ext => ext.trim().replace(/^\.+/, '').toLowerCase())
|
|
259
|
+
.filter(ext => ext.length > 0);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function shouldProcessFile(filename, fileExtensions) {
|
|
263
|
+
if (fileExtensions.length === 0) {
|
|
264
|
+
return true; // No extensions specified, process all files
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const fileExt = path.extname(filename).replace(/^\.+/, '').toLowerCase();
|
|
268
|
+
return fileExtensions.includes(fileExt);
|
|
269
|
+
}
|
|
270
|
+
|
|
244
271
|
function processDirectory(inputDir, outputDir, fileExt) {
|
|
272
|
+
const extensions = parseFileExtensions(fileExt);
|
|
273
|
+
|
|
245
274
|
fs.readdir(inputDir, function (err, files) {
|
|
246
275
|
if (err) {
|
|
247
276
|
fatal('Cannot read directory ' + inputDir + '\n' + err.message);
|
|
@@ -256,7 +285,7 @@ function processDirectory(inputDir, outputDir, fileExt) {
|
|
|
256
285
|
fatal('Cannot read ' + inputFile + '\n' + err.message);
|
|
257
286
|
} else if (stat.isDirectory()) {
|
|
258
287
|
processDirectory(inputFile, outputFile, fileExt);
|
|
259
|
-
} else if (
|
|
288
|
+
} else if (shouldProcessFile(file, extensions)) {
|
|
260
289
|
mkdir(outputDir, function () {
|
|
261
290
|
processFile(inputFile, outputFile);
|
|
262
291
|
});
|
|
@@ -290,13 +319,16 @@ const writeMinify = async () => {
|
|
|
290
319
|
|
|
291
320
|
const { inputDir, outputDir, fileExt } = programOptions;
|
|
292
321
|
|
|
322
|
+
// Resolve file extensions: CLI argument takes priority over config file
|
|
323
|
+
const resolvedFileExt = fileExt || config.fileExt;
|
|
324
|
+
|
|
293
325
|
if (inputDir || outputDir) {
|
|
294
326
|
if (!inputDir) {
|
|
295
327
|
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
328
|
} else if (!outputDir) {
|
|
297
329
|
fatal('You need to specify where to write the output files with the option --output-dir');
|
|
298
330
|
}
|
|
299
|
-
processDirectory(inputDir, outputDir,
|
|
331
|
+
processDirectory(inputDir, outputDir, resolvedFileExt);
|
|
300
332
|
} else if (content) { // Minifying one or more files specified on the CMD line
|
|
301
333
|
writeMinify();
|
|
302
334
|
} 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.2
|
|
91
|
+
"version": "1.3.2"
|
|
92
92
|
}
|