# htmlnano [![npm version](https://badge.fury.io/js/htmlnano.svg)](http://badge.fury.io/js/htmlnano) [![Build Status](https://travis-ci.org/posthtml/htmlnano.svg?branch=master)](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
hello world!
``` Minified (with `conservative`): ```html
hello world!
``` ### removeComments ##### Options - `safe` – removes all HTML comments except the conditional comments and [``](https://yandex.com/support/webmaster/controlling-robot/html.xml) (default) - `all` — removes all HTML comments ##### Example Source: ```html
``` Minified: ```html
``` ### removeEmptyAttributes Removes empty [safe-to-remove](https://github.com/posthtml/htmlnano/blob/master/lib/modules/removeEmptyAttributes.es6) attributes. ##### Side effects This module could break your styles or JS if you use selectors with attributes: ```CSS img[style=""] { margin: 10px; } ``` ##### Example Source: ```html ``` Minified: ```html ``` ### minifyCss Minifies CSS with [cssnano](http://cssnano.co/) inside ` ``` Minified: ```html
``` ### minifyJs Minifies JS with [Terser](https://github.com/fabiosantoscode/terser) inside ` ``` Minified: ```html
``` ### minifyJson Minifies JSON inside ``. ##### Example Source: ```html ``` Minified: ```html ``` ### minifySvg Minifies SVG inside `` tags with [SVGO](https://github.com/svg/svgo/). ##### Options See [the documentation of SVGO](https://github.com/svg/svgo/blob/master/README.md) ##### Example Source: ```html SVG ` ``` Minified: ```html SVG ``` ### removeRedundantAttributes Removes redundant attributes from tags if they contain default values: - `method="get"` from `
` - `type="text"` from `