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
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "htmlnano-docs",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"docusaurus": "docusaurus",
|
|
6
|
+
"start": "docusaurus start",
|
|
7
|
+
"build": "docusaurus build",
|
|
8
|
+
"swizzle": "docusaurus swizzle",
|
|
9
|
+
"deploy": "docusaurus deploy",
|
|
10
|
+
"clear": "docusaurus clear",
|
|
11
|
+
"serve": "docusaurus serve",
|
|
12
|
+
"write-translations": "docusaurus write-translations",
|
|
13
|
+
"write-heading-ids": "docusaurus write-heading-ids"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@docusaurus/core": "2.0.0-beta.4",
|
|
17
|
+
"@docusaurus/preset-classic": "2.0.0-beta.4",
|
|
18
|
+
"@mdx-js/react": "^1.6.21",
|
|
19
|
+
"@svgr/webpack": "^5.5.0",
|
|
20
|
+
"clsx": "^1.1.1",
|
|
21
|
+
"file-loader": "^6.2.0",
|
|
22
|
+
"prism-react-renderer": "^1.2.1",
|
|
23
|
+
"react": "^17.0.1",
|
|
24
|
+
"react-dom": "^17.0.1",
|
|
25
|
+
"url-loader": "^4.1.1"
|
|
26
|
+
},
|
|
27
|
+
"browserslist": {
|
|
28
|
+
"production": [
|
|
29
|
+
">0.5%",
|
|
30
|
+
"not dead",
|
|
31
|
+
"not op_mini all"
|
|
32
|
+
],
|
|
33
|
+
"development": [
|
|
34
|
+
"last 1 chrome version",
|
|
35
|
+
"last 1 firefox version",
|
|
36
|
+
"last 1 safari version"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
}
|
package/docs/sidebars.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creating a sidebar enables you to:
|
|
3
|
+
- create an ordered group of docs
|
|
4
|
+
- render a sidebar for each doc of that group
|
|
5
|
+
- provide next/previous navigation
|
|
6
|
+
|
|
7
|
+
The sidebars can be generated from the filesystem, or explicitly defined here.
|
|
8
|
+
|
|
9
|
+
Create as many sidebars as you want.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
module.exports = {
|
|
13
|
+
// By default, Docusaurus generates a sidebar from the docs folder structure
|
|
14
|
+
tutorialSidebar: [{type: 'autogenerated', dirName: '.'}],
|
|
15
|
+
|
|
16
|
+
// But you can create a sidebar manually
|
|
17
|
+
/*
|
|
18
|
+
tutorialSidebar: [
|
|
19
|
+
{
|
|
20
|
+
type: 'category',
|
|
21
|
+
label: 'Tutorial',
|
|
22
|
+
items: ['hello'],
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
*/
|
|
26
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_position: 1
|
|
3
|
+
slug: /
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Introduction
|
|
7
|
+
|
|
8
|
+
Modular HTML minifier, built on top of the [PostHTML](https://github.com/posthtml/posthtml).
|
|
9
|
+
Inspired by [cssnano](http://cssnano.co/).
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## [Benchmark](https://github.com/maltsev/html-minifiers-benchmark/blob/master/README.md)
|
|
13
|
+
[html-minifier-terser@5.1.1]: https://www.npmjs.com/package/html-minifier-terser
|
|
14
|
+
[htmlnano@1.0.0]: https://www.npmjs.com/package/htmlnano
|
|
15
|
+
|
|
16
|
+
| Website | Source (KB) | [html-minifier-terser@5.1.1] | [htmlnano@1.0.0] |
|
|
17
|
+
|---------|------------:|----------------:|-----------:|
|
|
18
|
+
| [stackoverflow.blog](https://stackoverflow.blog/) | 95 | 87 | 82 |
|
|
19
|
+
| [github.com](https://github.com/) | 210 | 183 | 171 |
|
|
20
|
+
| [en.wikipedia.org](https://en.wikipedia.org/wiki/Main_Page) | 78 | 72 | 72 |
|
|
21
|
+
| [npmjs.com](https://www.npmjs.com/features) | 41 | 38 | 36 |
|
|
22
|
+
| **Avg. minify rate** | 0% | **9%** | **13%** |
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Usage
|
|
2
|
+
|
|
3
|
+
## Gulp
|
|
4
|
+
```bash
|
|
5
|
+
npm install --save-dev gulp-htmlnano
|
|
6
|
+
```
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
const gulp = require('gulp');
|
|
10
|
+
const htmlnano = require('gulp-htmlnano');
|
|
11
|
+
const options = {
|
|
12
|
+
removeComments: false
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
gulp.task('default', function() {
|
|
16
|
+
return gulp
|
|
17
|
+
.src('./index.html')
|
|
18
|
+
.pipe(htmlnano(options))
|
|
19
|
+
.pipe(gulp.dest('./build'));
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## Javascript
|
|
25
|
+
```js
|
|
26
|
+
const htmlnano = require('htmlnano');
|
|
27
|
+
const options = {
|
|
28
|
+
removeEmptyAttributes: false, // Disable the module "removeEmptyAttributes"
|
|
29
|
+
collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace"
|
|
30
|
+
};
|
|
31
|
+
// posthtml, posthtml-render, and posthtml-parse options
|
|
32
|
+
const postHtmlOptions = {
|
|
33
|
+
sync: true, // https://github.com/posthtml/posthtml#usage
|
|
34
|
+
lowerCaseTags: true, // https://github.com/posthtml/posthtml-parser#options
|
|
35
|
+
quoteAllAttributes: false, // https://github.com/posthtml/posthtml-render#options
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
htmlnano
|
|
39
|
+
// "preset" arg might be skipped (see "Presets" section below for more info)
|
|
40
|
+
// "postHtmlOptions" arg might be skipped
|
|
41
|
+
.process(html, options, preset, postHtmlOptions)
|
|
42
|
+
.then(function (result) {
|
|
43
|
+
// result.html is minified
|
|
44
|
+
})
|
|
45
|
+
.catch(function (err) {
|
|
46
|
+
console.error(err);
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
## PostHTML
|
|
52
|
+
Just add `htmlnano` as a final plugin:
|
|
53
|
+
```js
|
|
54
|
+
const posthtml = require('posthtml');
|
|
55
|
+
const options = {
|
|
56
|
+
removeComments: false, // Disable the module "removeComments"
|
|
57
|
+
collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace"
|
|
58
|
+
};
|
|
59
|
+
const posthtmlPlugins = [
|
|
60
|
+
/* other PostHTML plugins */
|
|
61
|
+
|
|
62
|
+
require('htmlnano')(options)
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
const posthtmlOptions = {
|
|
66
|
+
// See PostHTML docs
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
posthtml(posthtmlPlugins)
|
|
70
|
+
.process(html, posthtmlOptions)
|
|
71
|
+
.then(function (result) {
|
|
72
|
+
// result.html is minified
|
|
73
|
+
})
|
|
74
|
+
.catch(function (err) {
|
|
75
|
+
console.error(err);
|
|
76
|
+
});
|
|
77
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Config
|
|
2
|
+
|
|
3
|
+
There are two main ways to configure htmlnano:
|
|
4
|
+
|
|
5
|
+
## Passing options to `htmlnano` directly
|
|
6
|
+
This is the way described above in the examples.
|
|
7
|
+
|
|
8
|
+
## Using configuration file
|
|
9
|
+
Alternatively, you might create a configuration file (e.g., `htmlanorc.json` or `htmlnanorc.js`) or save options to `package.json` with `htmlnano` key.
|
|
10
|
+
`htmlnano` uses `cosmiconfig`, so refer to [its documentation](https://github.com/davidtheclark/cosmiconfig/blob/main/README.md) for a more detailed description.
|
|
11
|
+
|
|
12
|
+
If you want to specify a preset that way, use `preset` key:
|
|
13
|
+
|
|
14
|
+
```json
|
|
15
|
+
{
|
|
16
|
+
"preset": "max",
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Configuration files have lower precedence than passing options to `htmlnano` directly.
|
|
21
|
+
So if you use both ways, then the configuration file would be ignored.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Presets
|
|
2
|
+
|
|
3
|
+
A preset is just an object with modules config.
|
|
4
|
+
|
|
5
|
+
Currently the following presets are available:
|
|
6
|
+
- [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)
|
|
7
|
+
- [ampSafe](https://github.com/posthtml/htmlnano/blob/master/lib/presets/ampSafe.es6) - same as `safe` preset but for [AMP pages](https://www.ampproject.org/)
|
|
8
|
+
- [max](https://github.com/posthtml/htmlnano/blob/master/lib/presets/max.es6) - maximal minification (might break some pages)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
You can use them the following way:
|
|
12
|
+
```js
|
|
13
|
+
const htmlnano = require('htmlnano');
|
|
14
|
+
const ampSafePreset = require('htmlnano').presets.ampSafe;
|
|
15
|
+
const options = {
|
|
16
|
+
// Your options
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
htmlnano
|
|
20
|
+
.process(html, options, ampSafePreset)
|
|
21
|
+
.then(function (result) {
|
|
22
|
+
// result.html is minified
|
|
23
|
+
})
|
|
24
|
+
.catch(function (err) {
|
|
25
|
+
console.error(err);
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
If you skip `preset` argument [`safe`](https://github.com/posthtml/htmlnano/blob/master/lib/presets/safe.es6) preset would be used by default.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
If you'd like to define your very own config without any presets pass an empty object as a preset:
|
|
33
|
+
```js
|
|
34
|
+
const htmlnano = require('htmlnano');
|
|
35
|
+
const options = {
|
|
36
|
+
// Your options
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
htmlnano
|
|
40
|
+
.process(html, options, {})
|
|
41
|
+
.then(function (result) {
|
|
42
|
+
// result.html is minified
|
|
43
|
+
})
|
|
44
|
+
.catch(function (err) {
|
|
45
|
+
console.error(err);
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
You might create also your own presets:
|
|
51
|
+
```js
|
|
52
|
+
const htmlnano = require('htmlnano');
|
|
53
|
+
// Preset for minifying email templates
|
|
54
|
+
const emailPreset = {
|
|
55
|
+
mergeStyles: true,
|
|
56
|
+
minifyCss: {
|
|
57
|
+
safe: true
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const options = {
|
|
62
|
+
// Some specific options
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
htmlnano
|
|
66
|
+
.process(html, options, emailPreset)
|
|
67
|
+
.then(function (result) {
|
|
68
|
+
// result.html is minified
|
|
69
|
+
})
|
|
70
|
+
.catch(function (err) {
|
|
71
|
+
console.error(err);
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
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.
|