htmlnano 1.0.0 → 2.0.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/CHANGELOG.md +55 -2
- package/README.md +12 -893
- package/docs/README.md +33 -0
- package/docs/babel.config.js +3 -0
- package/docs/docs/010-introduction.md +22 -0
- package/docs/docs/020-usage.md +77 -0
- package/docs/docs/030-config.md +21 -0
- package/docs/docs/040-presets.md +75 -0
- package/docs/docs/050-modules.md +838 -0
- package/docs/docs/060-contribute.md +16 -0
- package/docs/docusaurus.config.js +60 -0
- package/docs/netlify.toml +4 -0
- package/docs/package-lock.json +27251 -0
- package/docs/package.json +39 -0
- package/docs/sidebars.js +26 -0
- package/docs/versioned_docs/version-1.1.1/010-introduction.md +22 -0
- package/docs/versioned_docs/version-1.1.1/020-usage.md +77 -0
- package/docs/versioned_docs/version-1.1.1/030-config.md +21 -0
- package/docs/versioned_docs/version-1.1.1/040-presets.md +75 -0
- package/docs/versioned_docs/version-1.1.1/050-modules.md +786 -0
- package/docs/versioned_docs/version-1.1.1/060-contribute.md +16 -0
- package/docs/versioned_docs/version-2.0.0/010-introduction.md +22 -0
- package/docs/versioned_docs/version-2.0.0/020-usage.md +77 -0
- package/docs/versioned_docs/version-2.0.0/030-config.md +21 -0
- package/docs/versioned_docs/version-2.0.0/040-presets.md +75 -0
- package/docs/versioned_docs/version-2.0.0/050-modules.md +838 -0
- package/docs/versioned_docs/version-2.0.0/060-contribute.md +16 -0
- package/docs/versioned_sidebars/version-1.1.1-sidebars.json +8 -0
- package/docs/versioned_sidebars/version-2.0.0-sidebars.json +8 -0
- package/docs/versions.json +4 -0
- package/lib/helpers.js +19 -1
- package/lib/htmlnano.js +67 -6
- package/lib/modules/collapseAttributeWhitespace.js +63 -7
- package/lib/modules/collapseWhitespace.js +42 -17
- package/lib/modules/minifyCss.js +8 -8
- package/lib/modules/minifyJs.js +9 -10
- package/lib/modules/minifySvg.js +5 -2
- package/lib/modules/minifyUrls.js +71 -29
- package/lib/modules/normalizeAttributeValues.js +48 -0
- package/lib/modules/removeComments.js +25 -1
- package/lib/modules/removeEmptyAttributes.js +53 -8
- package/lib/modules/removeRedundantAttributes.js +69 -14
- package/lib/modules/removeUnusedCss.js +10 -10
- package/lib/presets/max.js +2 -0
- package/lib/presets/safe.js +13 -13
- package/package.json +58 -21
- package/test.js +25 -16
package/README.md
CHANGED
|
@@ -1,903 +1,22 @@
|
|
|
1
1
|
# htmlnano
|
|
2
2
|
[](http://badge.fury.io/js/htmlnano)
|
|
3
|
-
|
|
3
|
+

|
|
4
4
|
|
|
5
5
|
Modular HTML minifier, built on top of the [PostHTML](https://github.com/posthtml/posthtml). Inspired by [cssnano](http://cssnano.co/).
|
|
6
6
|
|
|
7
|
-
> The author of htmlnano is available for hire as a full stack web developer: https://kirillmaltsev.net/services
|
|
8
|
-
|
|
9
|
-
|
|
10
7
|
## [Benchmark](https://github.com/maltsev/html-minifiers-benchmark/blob/master/README.md)
|
|
11
|
-
[html-minifier@
|
|
12
|
-
[htmlnano@
|
|
8
|
+
[html-minifier-terser@6.0.2]: https://www.npmjs.com/package/html-minifier-terser
|
|
9
|
+
[htmlnano@1.1.1]: https://www.npmjs.com/package/htmlnano
|
|
13
10
|
|
|
14
|
-
| Website | Source (KB) | [html-minifier@
|
|
11
|
+
| Website | Source (KB) | [html-minifier-terser@6.0.2] | [htmlnano@1.1.1] |
|
|
15
12
|
|---------|------------:|----------------:|-----------:|
|
|
16
|
-
| [stackoverflow.blog](https://stackoverflow.blog/) |
|
|
17
|
-
| [github.com](https://github.com/) |
|
|
18
|
-
| [en.wikipedia.org](https://en.wikipedia.org/wiki/Main_Page) |
|
|
19
|
-
| [npmjs.com](https://www.npmjs.com/features) |
|
|
20
|
-
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
## Usage
|
|
24
|
-
### Gulp
|
|
25
|
-
```bash
|
|
26
|
-
npm install --save-dev gulp-htmlnano
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
```js
|
|
30
|
-
const gulp = require('gulp');
|
|
31
|
-
const htmlnano = require('gulp-htmlnano');
|
|
32
|
-
const options = {
|
|
33
|
-
removeComments: false
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
gulp.task('default', function() {
|
|
37
|
-
return gulp
|
|
38
|
-
.src('./index.html')
|
|
39
|
-
.pipe(htmlnano(options))
|
|
40
|
-
.pipe(gulp.dest('./build'));
|
|
41
|
-
});
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
### Javascript
|
|
46
|
-
```js
|
|
47
|
-
const htmlnano = require('htmlnano');
|
|
48
|
-
const options = {
|
|
49
|
-
removeEmptyAttributes: false, // Disable the module "removeEmptyAttributes"
|
|
50
|
-
collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace"
|
|
51
|
-
};
|
|
52
|
-
// posthtml, posthtml-render, and posthtml-parse options
|
|
53
|
-
const postHtmlOptions = {
|
|
54
|
-
sync: true, // https://github.com/posthtml/posthtml#usage
|
|
55
|
-
lowerCaseTags: true, // https://github.com/posthtml/posthtml-parser#options
|
|
56
|
-
quoteAllAttributes: false, // https://github.com/posthtml/posthtml-render#options
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
htmlnano
|
|
60
|
-
// "preset" arg might be skipped (see "Presets" section below for more info)
|
|
61
|
-
// "postHtmlOptions" arg might be skipped
|
|
62
|
-
.process(html, options, preset, postHtmlOptions)
|
|
63
|
-
.then(function (result) {
|
|
64
|
-
// result.html is minified
|
|
65
|
-
})
|
|
66
|
-
.catch(function (err) {
|
|
67
|
-
console.error(err);
|
|
68
|
-
});
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
### PostHTML
|
|
73
|
-
Just add `htmlnano` as a final plugin:
|
|
74
|
-
```js
|
|
75
|
-
const posthtml = require('posthtml');
|
|
76
|
-
const options = {
|
|
77
|
-
removeComments: false, // Disable the module "removeComments"
|
|
78
|
-
collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace"
|
|
79
|
-
};
|
|
80
|
-
const posthtmlPlugins = [
|
|
81
|
-
/* other PostHTML plugins */
|
|
82
|
-
|
|
83
|
-
require('htmlnano')(options)
|
|
84
|
-
];
|
|
85
|
-
|
|
86
|
-
const postHtmlOptions = {
|
|
87
|
-
// See PostHTML docs
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
posthtml(posthtmlPlugins)
|
|
91
|
-
.process(html, posthtmlOptions)
|
|
92
|
-
.then(function (result) {
|
|
93
|
-
// result.html is minified
|
|
94
|
-
})
|
|
95
|
-
.catch(function (err) {
|
|
96
|
-
console.error(err);
|
|
97
|
-
});
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
## Presets
|
|
103
|
-
A preset is just an object with modules config.
|
|
104
|
-
|
|
105
|
-
Currently the following presets are available:
|
|
106
|
-
- [safe](https://github.com/posthtml/htmlnano/blob/master/lib/presets/safe.es6) — a default preset for minifying a regular HTML in a safe way (without breaking anything)
|
|
107
|
-
- [ampSafe](https://github.com/posthtml/htmlnano/blob/master/lib/presets/ampSafe.es6) - same as `safe` preset but for [AMP pages](https://www.ampproject.org/)
|
|
108
|
-
- [max](https://github.com/posthtml/htmlnano/blob/master/lib/presets/max.es6) - maximal minification (might break some pages)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
You can use them the following way:
|
|
112
|
-
```js
|
|
113
|
-
const htmlnano = require('htmlnano');
|
|
114
|
-
const ampSafePreset = require('htmlnano').presets.ampSafe;
|
|
115
|
-
const options = {
|
|
116
|
-
// Your options
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
htmlnano
|
|
120
|
-
.process(html, options, ampSafePreset)
|
|
121
|
-
.then(function (result) {
|
|
122
|
-
// result.html is minified
|
|
123
|
-
})
|
|
124
|
-
.catch(function (err) {
|
|
125
|
-
console.error(err);
|
|
126
|
-
});
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
If you skip `preset` argument [`safe`](https://github.com/posthtml/htmlnano/blob/master/lib/presets/safe.es6) preset would be used by default.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
If you'd like to define your very own config without any presets pass an empty object as a preset:
|
|
133
|
-
```js
|
|
134
|
-
const htmlnano = require('htmlnano');
|
|
135
|
-
const options = {
|
|
136
|
-
// Your options
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
htmlnano
|
|
140
|
-
.process(html, options, {})
|
|
141
|
-
.then(function (result) {
|
|
142
|
-
// result.html is minified
|
|
143
|
-
})
|
|
144
|
-
.catch(function (err) {
|
|
145
|
-
console.error(err);
|
|
146
|
-
});
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
You might create also your own presets:
|
|
151
|
-
```js
|
|
152
|
-
const htmlnano = require('htmlnano');
|
|
153
|
-
// Preset for minifying email templates
|
|
154
|
-
const emailPreset = {
|
|
155
|
-
mergeStyles: true,
|
|
156
|
-
minifyCss: {
|
|
157
|
-
safe: true
|
|
158
|
-
},
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
const options = {
|
|
162
|
-
// Some specific options
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
htmlnano
|
|
166
|
-
.process(html, options, emailPreset)
|
|
167
|
-
.then(function (result) {
|
|
168
|
-
// result.html is minified
|
|
169
|
-
})
|
|
170
|
-
.catch(function (err) {
|
|
171
|
-
console.error(err);
|
|
172
|
-
});
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
Feel free [to submit a PR](https://github.com/posthtml/htmlnano/issues/new) with your preset if it might be useful for other developers as well.
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
## Modules
|
|
180
|
-
By default the modules should only perform safe transforms, see the module documentation below for details.
|
|
181
|
-
You can disable modules by passing `false` as option, and enable them by passing `true`.
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
### collapseAttributeWhitespace
|
|
185
|
-
Collapse redundant white spaces in list-like attributes (`class`, `rel`, `ping`).
|
|
186
|
-
|
|
187
|
-
##### Example
|
|
188
|
-
Source:
|
|
189
|
-
```html
|
|
190
|
-
<div class=" content page "></div>
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
Minified:
|
|
194
|
-
```html
|
|
195
|
-
<div class="content page"></div>
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
### collapseWhitespace
|
|
201
|
-
Collapses redundant white spaces (including new lines). It doesn’t affect white spaces in the elements `<style>`, `<textarea>`, `<script>` and `<pre>`.
|
|
202
|
-
|
|
203
|
-
##### Options
|
|
204
|
-
- `conservative` — collapses all redundant white spaces to 1 space (default)
|
|
205
|
-
- `aggressive` — collapses all whitespaces that are redundant and safe to remove
|
|
206
|
-
- `all` — collapses all redundant white spaces
|
|
207
|
-
|
|
208
|
-
##### Side effects
|
|
209
|
-
|
|
210
|
-
*all*
|
|
211
|
-
`<i>hello</i> <i>world</i>` or `<i>hello</i><br><i>world</i>` after minification will be rendered as `helloworld`.
|
|
212
|
-
To prevent that use either the default `conservative` option, or the `aggressive` option.
|
|
213
|
-
|
|
214
|
-
##### Example
|
|
215
|
-
Source:
|
|
216
|
-
```html
|
|
217
|
-
<div>
|
|
218
|
-
hello world!
|
|
219
|
-
<a href="#">answer</a>
|
|
220
|
-
<style>div { color: red; } </style>
|
|
221
|
-
<main></main>
|
|
222
|
-
</div>
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
Minified (with `all`):
|
|
226
|
-
```html
|
|
227
|
-
<div>hello world!<a href="#">answer</a><style>div { color: red; } </style><main></main></div>
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
Minified (with `aggressive`):
|
|
231
|
-
```html
|
|
232
|
-
<div> hello world! <a href="#">answer</a> <style>div { color: red; } </style><main></main></div>
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
Minified (with `conservative`):
|
|
236
|
-
```html
|
|
237
|
-
<div> hello world! <a href="#">answer</a> <style>div { color: red; } </style> <main></main> </div>
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
### deduplicateAttributeValues
|
|
242
|
-
Remove duplicate values from list-like attributes (`class`, `rel`, `ping`).
|
|
243
|
-
|
|
244
|
-
##### Example
|
|
245
|
-
Source:
|
|
246
|
-
```html
|
|
247
|
-
<div class="sidebar left sidebar"></div>
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
Minified:
|
|
251
|
-
```html
|
|
252
|
-
<div class="sidebar left"></div>
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
### removeComments
|
|
257
|
-
##### Options
|
|
258
|
-
- `safe` – removes all HTML comments except the conditional comments and [`<!--noindex--><!--/noindex-->`](https://yandex.com/support/webmaster/controlling-robot/html.xml) (default)
|
|
259
|
-
- `all` — removes all HTML comments
|
|
260
|
-
|
|
261
|
-
##### Example
|
|
262
|
-
Source:
|
|
263
|
-
```html
|
|
264
|
-
<div><!-- test --></div>
|
|
265
|
-
```
|
|
266
|
-
|
|
267
|
-
Minified:
|
|
268
|
-
```html
|
|
269
|
-
<div></div>
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
### removeEmptyAttributes
|
|
274
|
-
Removes empty [safe-to-remove](https://github.com/posthtml/htmlnano/blob/master/lib/modules/removeEmptyAttributes.es6) attributes.
|
|
275
|
-
|
|
276
|
-
##### Side effects
|
|
277
|
-
This module could break your styles or JS if you use selectors with attributes:
|
|
278
|
-
```CSS
|
|
279
|
-
img[style=""] {
|
|
280
|
-
margin: 10px;
|
|
281
|
-
}
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
##### Example
|
|
285
|
-
Source:
|
|
286
|
-
```html
|
|
287
|
-
<img src="foo.jpg" alt="" style="">
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
Minified:
|
|
291
|
-
```html
|
|
292
|
-
<img src="foo.jpg" alt="">
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
### removeAttributeQuotes
|
|
296
|
-
Remove quotes around attributes when possible, see [HTML Standard - 12.1.2.3 Attributes - Unquoted attribute value syntax](https://html.spec.whatwg.org/multipage/syntax.html#attributes-2).
|
|
297
|
-
|
|
298
|
-
##### Example
|
|
299
|
-
Source:
|
|
300
|
-
```html
|
|
301
|
-
<div class="foo" title="hello world"></div>
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
Minified:
|
|
305
|
-
```html
|
|
306
|
-
<div class=foo title="hello world"></div>
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
##### Notice
|
|
310
|
-
The feature is implemented by [posthtml-render's `quoteAllAttributes`](https://github.com/posthtml/posthtml-render#options), which is one of the PostHTML's option. So `removeAttributeQuotes` could be overriden by other PostHTML's plugins and PostHTML's configuration.
|
|
311
|
-
|
|
312
|
-
For example:
|
|
313
|
-
|
|
314
|
-
```js
|
|
315
|
-
posthtml([
|
|
316
|
-
htmlnano({
|
|
317
|
-
removeAttributeQuotes: true
|
|
318
|
-
})
|
|
319
|
-
]).process(html, {
|
|
320
|
-
quoteAllAttributes: true
|
|
321
|
-
})
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
`removeAttributeQuotes` will not work because PostHTML's `quoteAllAttributes` takes the priority.
|
|
325
|
-
|
|
326
|
-
### removeUnusedCss
|
|
327
|
-
|
|
328
|
-
Removes unused CSS inside `<style>` tags with either [uncss](https://github.com/uncss/uncss)
|
|
329
|
-
or [PurgeCSS](https://github.com/FullHuman/purgecss).
|
|
330
|
-
|
|
331
|
-
#### With uncss
|
|
332
|
-
|
|
333
|
-
##### Options
|
|
334
|
-
See [the documentation of uncss](https://github.com/uncss/uncss) for all supported options.
|
|
335
|
-
|
|
336
|
-
uncss options can be passed directly to the `removeUnusedCss` module:
|
|
337
|
-
```js
|
|
338
|
-
htmlnano.process(html, {
|
|
339
|
-
removeUnusedCss: {
|
|
340
|
-
ignore: ['.do-not-remove']
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
```
|
|
344
|
-
|
|
345
|
-
The following uncss options are ignored if passed to the module:
|
|
346
|
-
|
|
347
|
-
- `stylesheets`
|
|
348
|
-
- `ignoreSheets`
|
|
349
|
-
- `raw`
|
|
350
|
-
|
|
351
|
-
#### With PurgeCSS
|
|
352
|
-
|
|
353
|
-
Use PurgeCSS instead of uncss by adding `tool: 'purgeCSS'` to the options.
|
|
354
|
-
|
|
355
|
-
##### Options
|
|
356
|
-
|
|
357
|
-
See [the documentation of PurgeCSS](https://www.purgecss.com) for all supported options.
|
|
358
|
-
|
|
359
|
-
PurgeCSS options can be passed directly to the `removeUnusedCss` module:
|
|
360
|
-
```js
|
|
361
|
-
htmlnano.process(html, {
|
|
362
|
-
removeUnusedCss: {
|
|
363
|
-
tool: 'purgeCSS',
|
|
364
|
-
safelist: ['.do-not-remove']
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
The following PurgeCSS options are ignored if passed to the module:
|
|
370
|
-
|
|
371
|
-
- `content`
|
|
372
|
-
- `css`
|
|
373
|
-
- `extractors`
|
|
374
|
-
|
|
375
|
-
##### Example
|
|
376
|
-
Source:
|
|
377
|
-
```html
|
|
378
|
-
<div class="b">
|
|
379
|
-
<style>
|
|
380
|
-
.a {
|
|
381
|
-
margin: 10px 10px 10px 10px;
|
|
382
|
-
}
|
|
383
|
-
.b {
|
|
384
|
-
color: #ff0000;
|
|
385
|
-
}
|
|
386
|
-
</style>
|
|
387
|
-
</div>
|
|
388
|
-
```
|
|
389
|
-
|
|
390
|
-
Optimized:
|
|
391
|
-
```html
|
|
392
|
-
<div class="b">
|
|
393
|
-
<style>
|
|
394
|
-
.b {
|
|
395
|
-
color: #ff0000;
|
|
396
|
-
}
|
|
397
|
-
</style>
|
|
398
|
-
</div>
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
### minifyCss
|
|
403
|
-
Minifies CSS with [cssnano](http://cssnano.co/) inside `<style>` tags and `style` attributes.
|
|
404
|
-
|
|
405
|
-
##### Options
|
|
406
|
-
See [the documentation of cssnano](http://cssnano.co/optimisations/) for all supported optimizations.
|
|
407
|
-
By default CSS is minified with preset `default`, which shouldn't have any side-effects.
|
|
408
|
-
|
|
409
|
-
To use another preset or disabled some optimizations pass options to `minifyCss` module:
|
|
410
|
-
```js
|
|
411
|
-
htmlnano.process(html, {
|
|
412
|
-
minifyCss: {
|
|
413
|
-
preset: ['default', {
|
|
414
|
-
discardComments: {
|
|
415
|
-
removeAll: true,
|
|
416
|
-
},
|
|
417
|
-
}]
|
|
418
|
-
}
|
|
419
|
-
});
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
##### Example
|
|
423
|
-
Source:
|
|
424
|
-
```html
|
|
425
|
-
<div>
|
|
426
|
-
<style>
|
|
427
|
-
h1 {
|
|
428
|
-
margin: 10px 10px 10px 10px;
|
|
429
|
-
color: #ff0000;
|
|
430
|
-
}
|
|
431
|
-
</style>
|
|
432
|
-
</div>
|
|
433
|
-
```
|
|
434
|
-
|
|
435
|
-
Minified:
|
|
436
|
-
```html
|
|
437
|
-
<div>
|
|
438
|
-
<style>h1{margin:10px;color:red}</style>
|
|
439
|
-
</div>
|
|
440
|
-
```
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
### minifyJs
|
|
444
|
-
Minifies JS using [Terser](https://github.com/fabiosantoscode/terser) inside `<script>` tags.
|
|
445
|
-
|
|
446
|
-
##### Options
|
|
447
|
-
See [the documentation of Terser](https://github.com/fabiosantoscode/terser#api-reference) for all supported options.
|
|
448
|
-
Terser options can be passed directly to the `minifyJs` module:
|
|
449
|
-
```js
|
|
450
|
-
htmlnano.process(html, {
|
|
451
|
-
minifyJs: {
|
|
452
|
-
output: { quote_style: 1 },
|
|
453
|
-
},
|
|
454
|
-
});
|
|
455
|
-
```
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
##### Example
|
|
460
|
-
Source:
|
|
461
|
-
```html
|
|
462
|
-
<div>
|
|
463
|
-
<script>
|
|
464
|
-
/* comment */
|
|
465
|
-
const foo = function () {
|
|
466
|
-
|
|
467
|
-
};
|
|
468
|
-
</script>
|
|
469
|
-
</div>
|
|
470
|
-
```
|
|
471
|
-
|
|
472
|
-
Minified:
|
|
473
|
-
```html
|
|
474
|
-
<div>
|
|
475
|
-
<script>const foo=function(){};</script>
|
|
476
|
-
</div>
|
|
477
|
-
```
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
### minifyJson
|
|
481
|
-
Minifies JSON inside `<script type="application/json"></script>`.
|
|
482
|
-
|
|
483
|
-
##### Example
|
|
484
|
-
Source:
|
|
485
|
-
```html
|
|
486
|
-
<script type="application/json">
|
|
487
|
-
{
|
|
488
|
-
"user": "me"
|
|
489
|
-
}
|
|
490
|
-
</script>
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
Minified:
|
|
494
|
-
```html
|
|
495
|
-
<script type="application/json">{"user":"me"}</script>
|
|
496
|
-
```
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
### minifySvg
|
|
500
|
-
Minifies SVG inside `<svg>` tags using [SVGO](https://github.com/svg/svgo/).
|
|
501
|
-
|
|
502
|
-
##### Options
|
|
503
|
-
See [the documentation of SVGO](https://github.com/svg/svgo/blob/master/README.md) for all supported options.
|
|
504
|
-
SVGO options can be passed directly to the `minifySvg` module:
|
|
505
|
-
```js
|
|
506
|
-
htmlnano.process(html, {
|
|
507
|
-
minifySvg: {
|
|
508
|
-
plugins: extendDefaultPlugins([
|
|
509
|
-
{
|
|
510
|
-
name: 'builtinPluginName',
|
|
511
|
-
params: {
|
|
512
|
-
optionName: 'optionValue'
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
])
|
|
516
|
-
}
|
|
517
|
-
});
|
|
518
|
-
```
|
|
519
|
-
|
|
520
|
-
##### Example
|
|
521
|
-
Source:
|
|
522
|
-
```html
|
|
523
|
-
<svg version="1.1" baseProfile="full" width="300" height="200" xmlns="http://www.w3.org/2000/svg">
|
|
524
|
-
<rect width="100%" height="100%" fill="red" />
|
|
525
|
-
|
|
526
|
-
<circle cx="150" cy="100" r="80" fill="green" />
|
|
527
|
-
|
|
528
|
-
<text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>
|
|
529
|
-
</svg>`
|
|
530
|
-
```
|
|
531
|
-
|
|
532
|
-
Minified:
|
|
533
|
-
```html
|
|
534
|
-
<svg baseProfile="full" width="300" height="200" xmlns="http://www.w3.org/2000/svg"><rect width="100%" height="100%" fill="red"/><circle cx="150" cy="100" r="80" fill="green"/><text x="150" y="125" font-size="60" text-anchor="middle" fill="#fff">SVG</text></svg>
|
|
535
|
-
```
|
|
536
|
-
|
|
537
|
-
### minifyConditionalComments
|
|
538
|
-
|
|
539
|
-
Minify content inside conditional comments.
|
|
540
|
-
|
|
541
|
-
##### Example
|
|
542
|
-
|
|
543
|
-
Source:
|
|
544
|
-
|
|
545
|
-
```html
|
|
546
|
-
<!--[if lte IE 7]>
|
|
547
|
-
<style type="text/css">
|
|
548
|
-
.title {
|
|
549
|
-
color: red;
|
|
550
|
-
}
|
|
551
|
-
</style>
|
|
552
|
-
<![endif]-->
|
|
553
|
-
```
|
|
554
|
-
|
|
555
|
-
Minified:
|
|
556
|
-
|
|
557
|
-
```html
|
|
558
|
-
<!--[if lte IE 7]><style>.title{color:red}</style><![endif]-->
|
|
559
|
-
```
|
|
560
|
-
|
|
561
|
-
### removeRedundantAttributes
|
|
562
|
-
Removes redundant attributes from tags if they contain default values:
|
|
563
|
-
- `method="get"` from `<form>`
|
|
564
|
-
- `type="text"` from `<input>`
|
|
565
|
-
- `type="submit"` from `<button>`
|
|
566
|
-
- `language="javascript"` and `type="text/javascript"` from `<script>`
|
|
567
|
-
- `charset` from `<script>` if it's an external script
|
|
568
|
-
- `media="all"` from `<style>` and `<link>`
|
|
569
|
-
- `type="text/css"` from `<link rel="stylesheet">`
|
|
570
|
-
|
|
571
|
-
##### Options
|
|
572
|
-
This module is disabled by default, change option to true to enable this module.
|
|
573
|
-
|
|
574
|
-
##### Side effects
|
|
575
|
-
This module could break your styles or JS if you use selectors with attributes:
|
|
576
|
-
```CSS
|
|
577
|
-
form[method="get"] {
|
|
578
|
-
color: red;
|
|
579
|
-
}
|
|
580
|
-
```
|
|
581
|
-
|
|
582
|
-
##### Example
|
|
583
|
-
Source:
|
|
584
|
-
```html
|
|
585
|
-
<form method="get">
|
|
586
|
-
<input type="text">
|
|
587
|
-
</form>
|
|
588
|
-
```
|
|
589
|
-
|
|
590
|
-
Minified:
|
|
591
|
-
```html
|
|
592
|
-
<form>
|
|
593
|
-
<input>
|
|
594
|
-
</form>
|
|
595
|
-
```
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
### collapseBooleanAttributes
|
|
599
|
-
Collapses boolean attributes (like `disabled`) to the minimized form.
|
|
600
|
-
|
|
601
|
-
##### Options
|
|
602
|
-
If your document uses [AMP](https://www.ampproject.org/), set the `amphtml` flag
|
|
603
|
-
to collapse additonal, AMP-specific boolean attributes:
|
|
604
|
-
```Json
|
|
605
|
-
"collapseBooleanAttributes": {
|
|
606
|
-
"amphtml": true
|
|
607
|
-
}
|
|
608
|
-
```
|
|
609
|
-
|
|
610
|
-
##### Side effects
|
|
611
|
-
This module could break your styles or JS if you use selectors with attributes:
|
|
612
|
-
```CSS
|
|
613
|
-
button[disabled="disabled"] {
|
|
614
|
-
color: red;
|
|
615
|
-
}
|
|
616
|
-
```
|
|
617
|
-
|
|
618
|
-
##### Example
|
|
619
|
-
Source:
|
|
620
|
-
```html
|
|
621
|
-
<button disabled="disabled">click</button>
|
|
622
|
-
<script defer=""></script>
|
|
623
|
-
```
|
|
624
|
-
|
|
625
|
-
Minified:
|
|
626
|
-
```html
|
|
627
|
-
<button disabled>click</button>
|
|
628
|
-
<script defer></script>
|
|
629
|
-
```
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
### mergeStyles
|
|
633
|
-
Merges multiple `<style>` with the same `media` and `type` into one tag.
|
|
634
|
-
`<style scoped>...</style>` are skipped.
|
|
635
|
-
|
|
636
|
-
##### Example
|
|
637
|
-
Source:
|
|
638
|
-
```html
|
|
639
|
-
<style>h1 { color: red }</style>
|
|
640
|
-
<style media="print">div { color: blue }</style>
|
|
641
|
-
|
|
642
|
-
<style type="text/css" media="print">a {}</style>
|
|
643
|
-
<style>div { font-size: 20px }</style>
|
|
644
|
-
```
|
|
645
|
-
|
|
646
|
-
Minified:
|
|
647
|
-
```html
|
|
648
|
-
<style>h1 { color: red } div { font-size: 20px }</style>
|
|
649
|
-
<style media="print">div { color: blue } a {}</style>
|
|
650
|
-
```
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
### mergeScripts
|
|
654
|
-
Merge multiple `<script>` with the same attributes (`id, class, type, async, defer`) into one (last) tag.
|
|
655
|
-
|
|
656
|
-
##### Side effects
|
|
657
|
-
It could break your code if the tags with different attributes share the same variable scope.
|
|
658
|
-
See the example below.
|
|
659
|
-
|
|
660
|
-
##### Example
|
|
661
|
-
Source:
|
|
662
|
-
```html
|
|
663
|
-
<script>const foo = 'A:1';</script>
|
|
664
|
-
<script class="test">foo = 'B:1';</script>
|
|
665
|
-
<script type="text/javascript">foo = 'A:2';</script>
|
|
666
|
-
<script defer>foo = 'C:1';</script>
|
|
667
|
-
<script>foo = 'A:3';</script>
|
|
668
|
-
<script defer="defer">foo = 'C:2';</script>
|
|
669
|
-
<script class="test" type="text/javascript">foo = 'B:2';</script>
|
|
670
|
-
```
|
|
671
|
-
|
|
672
|
-
Minified:
|
|
673
|
-
```html
|
|
674
|
-
<script>const foo = 'A:1'; foo = 'A:2'; foo = 'A:3';</script>
|
|
675
|
-
<script defer="defer">foo = 'C:1'; foo = 'C:2';</script>
|
|
676
|
-
<script class="test" type="text/javascript">foo = 'B:1'; foo = 'B:2';</script>
|
|
677
|
-
```
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
### custom
|
|
681
|
-
It's also possible to pass custom modules in the minifier.
|
|
682
|
-
As a function:
|
|
683
|
-
```js
|
|
684
|
-
const options = {
|
|
685
|
-
custom: function (tree, options) {
|
|
686
|
-
// Some minification
|
|
687
|
-
return tree;
|
|
688
|
-
}
|
|
689
|
-
};
|
|
690
|
-
```
|
|
691
|
-
|
|
692
|
-
Or as a list of functions:
|
|
693
|
-
```js
|
|
694
|
-
const options = {
|
|
695
|
-
custom: [
|
|
696
|
-
function (tree, options) {
|
|
697
|
-
// Some minification
|
|
698
|
-
return tree;
|
|
699
|
-
},
|
|
700
|
-
|
|
701
|
-
function (tree, options) {
|
|
702
|
-
// Some other minification
|
|
703
|
-
return tree;
|
|
704
|
-
}
|
|
705
|
-
]
|
|
706
|
-
};
|
|
707
|
-
```
|
|
708
|
-
|
|
709
|
-
`options` is an object with all options that were passed to the plugin.
|
|
710
|
-
|
|
711
|
-
### sortAttributesWithLists
|
|
712
|
-
Sort values in list-like attributes (`class`, `rel`, `ping`).
|
|
713
|
-
|
|
714
|
-
The module won't impact the plain-text size of the output. However it will improve the compression ratio of gzip/brotli used in HTTP compression.
|
|
715
|
-
|
|
716
|
-
##### Options
|
|
717
|
-
|
|
718
|
-
- `alphabetical`: Default option. Sort attribute values in alphabetical order.
|
|
719
|
-
- `frequency`: Sort attribute values by frequency.
|
|
720
|
-
|
|
721
|
-
##### Example
|
|
722
|
-
|
|
723
|
-
**alphabetical**
|
|
724
|
-
|
|
725
|
-
Source:
|
|
726
|
-
```html
|
|
727
|
-
<div class="foo baz bar">click</div>
|
|
728
|
-
```
|
|
729
|
-
|
|
730
|
-
Processed:
|
|
731
|
-
```html
|
|
732
|
-
<div class="bar baz foo">click</div>
|
|
733
|
-
```
|
|
734
|
-
|
|
735
|
-
**frequency**
|
|
736
|
-
|
|
737
|
-
Source:
|
|
738
|
-
```html
|
|
739
|
-
<div class="foo baz bar"></div><div class="bar foo"></div>
|
|
740
|
-
```
|
|
741
|
-
|
|
742
|
-
Processed:
|
|
743
|
-
```html
|
|
744
|
-
<div class="foo bar baz"></div><div class="foo bar"></div>
|
|
745
|
-
```
|
|
746
|
-
|
|
747
|
-
### sortAttributes
|
|
748
|
-
Sort attributes inside elements.
|
|
749
|
-
|
|
750
|
-
The module won't impact the plain-text size of the output. However it will improve the compression ratio of gzip/brotli used in HTTP compression.
|
|
751
|
-
|
|
752
|
-
##### Options
|
|
753
|
-
|
|
754
|
-
- `alphabetical`: Default option. Sort attributes in alphabetical order.
|
|
755
|
-
- `frequency`: Sort attributes by frequency.
|
|
756
|
-
|
|
757
|
-
##### Example
|
|
758
|
-
|
|
759
|
-
**alphabetical**
|
|
760
|
-
|
|
761
|
-
Source:
|
|
762
|
-
```html
|
|
763
|
-
<input type="text" class="form-control" name="testInput" autofocus="" autocomplete="off" id="testId">
|
|
764
|
-
```
|
|
765
|
-
|
|
766
|
-
Processed:
|
|
767
|
-
```html
|
|
768
|
-
<input autocomplete="off" autofocus="" class="form-control" id="testId" name="testInput" type="text">
|
|
769
|
-
```
|
|
770
|
-
|
|
771
|
-
**frequency**
|
|
772
|
-
|
|
773
|
-
Source:
|
|
774
|
-
```html
|
|
775
|
-
<input type="text" class="form-control" name="testInput" id="testId">
|
|
776
|
-
<a id="testId" href="#" class="testClass"></a>
|
|
777
|
-
<img width="20" src="../images/image.png" height="40" alt="image" class="cls" id="id2">
|
|
778
|
-
```
|
|
779
|
-
|
|
780
|
-
Processed:
|
|
781
|
-
```html
|
|
782
|
-
<input class="form-control" id="testId" type="text" name="testInput">
|
|
783
|
-
<a class="testClass" id="testId" href="#"></a>
|
|
784
|
-
<img class="cls" id="id2" width="20" src="../images/image.png" height="40" alt="image">
|
|
785
|
-
```
|
|
786
|
-
|
|
787
|
-
### minifyUrls
|
|
788
|
-
Convert absolute URL to relative URL using [relateurl](https://www.npmjs.com/package/relateurl).
|
|
789
|
-
|
|
790
|
-
##### Options
|
|
791
|
-
|
|
792
|
-
The base URL to resolve against. Support `String` & `URL`.
|
|
793
|
-
|
|
794
|
-
```js
|
|
795
|
-
htmlnano.process(html, {
|
|
796
|
-
minifyUrls: 'https://example.com' // Valid configuration
|
|
797
|
-
});
|
|
798
|
-
```
|
|
799
|
-
|
|
800
|
-
```js
|
|
801
|
-
htmlnano.process(html, {
|
|
802
|
-
minifyUrls: new URL('https://example.com') // Valid configuration
|
|
803
|
-
});
|
|
804
|
-
```
|
|
805
|
-
|
|
806
|
-
```js
|
|
807
|
-
htmlnano.process(html, {
|
|
808
|
-
minifyUrls: false // The module will be disabled
|
|
809
|
-
});
|
|
810
|
-
```
|
|
811
|
-
|
|
812
|
-
```js
|
|
813
|
-
htmlnano.process(html, {
|
|
814
|
-
minifyUrls: true // Invalid configuration, the module will be disabled
|
|
815
|
-
});
|
|
816
|
-
```
|
|
817
|
-
|
|
818
|
-
##### Example
|
|
819
|
-
|
|
820
|
-
**Basic Usage**
|
|
821
|
-
|
|
822
|
-
Configuration:
|
|
823
|
-
|
|
824
|
-
```js
|
|
825
|
-
htmlnano.process(html, {
|
|
826
|
-
minifyUrls: 'https://example.com'
|
|
827
|
-
});
|
|
828
|
-
```
|
|
829
|
-
|
|
830
|
-
Source:
|
|
831
|
-
|
|
832
|
-
```html
|
|
833
|
-
<a href="https://example.com/foo/bar/baz">bar</a>
|
|
834
|
-
```
|
|
835
|
-
|
|
836
|
-
Minified:
|
|
837
|
-
|
|
838
|
-
```html
|
|
839
|
-
<a href="foo/bar/baz">bar</a>
|
|
840
|
-
```
|
|
841
|
-
|
|
842
|
-
**With sub-directory**
|
|
843
|
-
|
|
844
|
-
Configuration:
|
|
845
|
-
|
|
846
|
-
```js
|
|
847
|
-
htmlnano.process(html, {
|
|
848
|
-
minifyUrls: 'https://example.com/foo/baz/'
|
|
849
|
-
});
|
|
850
|
-
```
|
|
851
|
-
|
|
852
|
-
Source:
|
|
853
|
-
|
|
854
|
-
```html
|
|
855
|
-
<a href="https://example.com/foo/bar">bar</a>
|
|
856
|
-
```
|
|
857
|
-
|
|
858
|
-
Minified:
|
|
859
|
-
|
|
860
|
-
```html
|
|
861
|
-
<a href="../bar">bar</a>
|
|
862
|
-
```
|
|
863
|
-
|
|
864
|
-
## removeOptionalTags
|
|
865
|
-
Remove certain tags that can be omitted, see [HTML Standard - 13.1.2.4 Optional tags](https://html.spec.whatwg.org/multipage/syntax.html#optional-tags).
|
|
866
|
-
|
|
867
|
-
##### Example
|
|
868
|
-
|
|
869
|
-
Source:
|
|
870
|
-
|
|
871
|
-
```html
|
|
872
|
-
<html><head><title>Title</title></head><body><p>Hi</p></body></html>
|
|
873
|
-
```
|
|
874
|
-
|
|
875
|
-
Minified:
|
|
876
|
-
|
|
877
|
-
```html
|
|
878
|
-
<title>Title</title><p>Hi</p>
|
|
879
|
-
```
|
|
880
|
-
##### Notice
|
|
881
|
-
Due to [the limitation of PostHTML](https://github.com/posthtml/htmlnano/issues/99), htmlnano can't remove only the start tag or the end tag of an element. Currently, htmlnano only supports removing the following optional tags, as htmlnano can remove their start tag and end tag at the same time:
|
|
882
|
-
|
|
883
|
-
- `html`
|
|
884
|
-
- `head`
|
|
885
|
-
- `body`
|
|
886
|
-
- `colgroup`
|
|
887
|
-
- `tbody`
|
|
888
|
-
|
|
889
|
-
## Contribute
|
|
890
|
-
Since the minifier is modular, it's very easy to add new modules:
|
|
891
|
-
|
|
892
|
-
1. Create a ES6-file inside `lib/modules/` with a function that does some minification. For example you can check [`lib/modules/example.es6`](https://github.com/posthtml/htmlnano/blob/master/lib/modules/example.es6).
|
|
893
|
-
|
|
894
|
-
2. Add the module's name into one of those [presets](https://github.com/posthtml/htmlnano/tree/master/lib/presets). You can choose either `ampSafe`, `max`, or `safe`.
|
|
895
|
-
|
|
896
|
-
3. Create a JS-file inside `test/modules/` with some unit-tests.
|
|
897
|
-
|
|
898
|
-
4. Describe your module in the section "[Modules](https://github.com/posthtml/htmlnano/blob/master/README.md#modules)".
|
|
13
|
+
| [stackoverflow.blog](https://stackoverflow.blog/) | 90 | 82 | 76 |
|
|
14
|
+
| [github.com](https://github.com/) | 232 | 203 | 173 |
|
|
15
|
+
| [en.wikipedia.org](https://en.wikipedia.org/wiki/Main_Page) | 81 | 76 | 75 |
|
|
16
|
+
| [npmjs.com](https://www.npmjs.com/features) | 43 | 40 | 38 |
|
|
17
|
+
| [tc39.es](https://tc39.es/ecma262/) | 6001 | 5465 | 5459 |
|
|
18
|
+
| **Avg. minify rate** | 0% | **9%** | **13%** |
|
|
899
19
|
|
|
900
|
-
5. Send me a pull request.
|
|
901
20
|
|
|
902
|
-
|
|
903
|
-
|
|
21
|
+
## Documentation
|
|
22
|
+
https://htmlnano.netlify.app
|