# htmlnano [](http://badge.fury.io/js/htmlnano) [](https://travis-ci.org/posthtml/htmlnano) Modular HTML minifier, built on top of the [PostHTML](https://github.com/posthtml/posthtml). Inspired by [cssnano](http://cssnano.co/). ## [Benchmark](https://github.com/maltsev/html-minifiers-benchmark/blob/master/README.md) [html-minifier]: https://www.npmjs.com/package/html-minifier [htmlnano]: https://www.npmjs.com/package/htmlnano | Website | Source (KB) | [html-minifier] | [htmlnano] | |---------|------------:|----------------:|-----------:| | [stackoverflow.com](http://stackoverflow.com/) | 250 | 199 | 208 | | [github.com](http://github.com/) | 51 | 43 | 45 | | [en.wikipedia.org](https://en.wikipedia.org/wiki/Main_Page) | 71 | 64 | 68 | | [npmjs.com](https://www.npmjs.com/features) | 26 | 20 | 21 | | **Avg. minify rate** | 0% | **18%** | **14%** | ## Usage ### Gulp ```bash npm install --save-dev gulp-htmlnano ``` ```js var gulp = require('gulp'); var htmlnano = require('gulp-htmlnano'); var options = { removeComments: false }; gulp.task('default', function() { return gulp .src('./index.html') .pipe(htmlnano(options)) .pipe(gulp.dest('./build')); }); ``` ### Javascript ```js var htmlnano = require('htmlnano'); var options = { removeEmptyAttributes: false, // Disable the module "removeEmptyAttributes" collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace" }; htmlnano .process(html, options) .then(function (result) { // result.html is minified }) .catch(function (err) { console.error(err); }); ``` ### PostHTML Just add `htmlnano` as the last plugin: ```js var posthtml = require('posthtml'); var options = { removeComments: false, // Disable the module "removeComments" collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace" }; var posthtmlPlugins = [ /* other PostHTML plugins */ require('htmlnano')(options) ]; posthtml(posthtmlPlugins) .process(html) .then(function (result) { // result.html is minified }) .catch(function (err) { console.error(err); }); // You can also use htmlnano modules separately: posthtml([ require('htmlnano/lib/modules/mergeStyles').default ]).process(html); ``` ## Modules By default the modules should only perform safe transforms, see the module documentation below for details. You can disable modules by passing `false` as option, and enable them by passing `true`. ### collapseWhitespace Collapses redundant white spaces (including new lines). It doesn’t affect white spaces in the elements ` ``` Minified (with `all`): ```html
```
Minified:
```html
```
### minifyCss
Minifies CSS with [cssnano](http://cssnano.co/) inside `
```
Minified:
```html