# 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/). > The author of htmlnano is available for hire as a full stack web developer: https://kirillmaltsev.net/services ## [Benchmark](https://github.com/maltsev/html-minifiers-benchmark/blob/master/README.md) [html-minifier@3.5.20]: https://www.npmjs.com/package/html-minifier [htmlnano@0.2.0]: https://www.npmjs.com/package/htmlnano | Website | Source (KB) | [html-minifier@3.5.20] | [htmlnano@0.2.0] | |---------|------------:|----------------:|-----------:| | [stackoverflow.com](http://stackoverflow.com/) | 258 | 205 | 218 | | [github.com](http://github.com/) | 63 | 51 | 56 | | [en.wikipedia.org](https://en.wikipedia.org/wiki/Main_Page) | 77 | 69 | 74 | | [npmjs.com](https://www.npmjs.com/features) | 32 | 29 | 30 | | **Avg. minify rate** | 0% | **15%** | **9%** | ## Usage ### Gulp ```bash npm install --save-dev gulp-htmlnano ``` ```js const gulp = require('gulp'); const htmlnano = require('gulp-htmlnano'); const options = { removeComments: false }; gulp.task('default', function() { return gulp .src('./index.html') .pipe(htmlnano(options)) .pipe(gulp.dest('./build')); }); ``` ### Javascript ```js const htmlnano = require('htmlnano'); const options = { removeEmptyAttributes: false, // Disable the module "removeEmptyAttributes" collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace" }; // posthtml, posthtml-render, and posthtml-parse options const postHtmlOptions = { sync: true, // https://github.com/posthtml/posthtml#usage lowerCaseTags: true, // https://github.com/posthtml/posthtml-parser#options quoteAllAttributes: false, // https://github.com/posthtml/posthtml-render#options }; htmlnano // "preset" arg might be skipped (see "Presets" section below for more info) // "postHtmlOptions" arg might be skipped .process(html, options, preset, postHtmlOptions) .then(function (result) { // result.html is minified }) .catch(function (err) { console.error(err); }); ``` ### PostHTML Just add `htmlnano` as a final plugin: ```js const posthtml = require('posthtml'); const options = { removeComments: false, // Disable the module "removeComments" collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace" }; const posthtmlPlugins = [ /* other PostHTML plugins */ require('htmlnano')(options) ]; const postHtmlOptions = { // See PostHTML docs }; posthtml(posthtmlPlugins) .process(html, posthtmlOptions) .then(function (result) { // result.html is minified }) .catch(function (err) { console.error(err); }); ``` ## Presets A preset is just an object with modules config. Currently the following presets are available: - [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) - [ampSafe](https://github.com/posthtml/htmlnano/blob/master/lib/presets/ampSafe.es6) - same as `safe` preset but for [AMP pages](https://www.ampproject.org/) - [max](https://github.com/posthtml/htmlnano/blob/master/lib/presets/max.es6) - maximal minification (might break some pages) You can use them the following way: ```js const htmlnano = require('htmlnano'); const ampSafePreset = require('htmlnano').presets.ampSafe; const options = { // Your options }; htmlnano .process(html, options, ampSafePreset) .then(function (result) { // result.html is minified }) .catch(function (err) { console.error(err); }); ``` If you skip `preset` argument [`safe`](https://github.com/posthtml/htmlnano/blob/master/lib/presets/safe.es6) preset would be used by default. If you'd like to define your very own config without any presets pass an empty object as a preset: ```js const htmlnano = require('htmlnano'); const options = { // Your options }; htmlnano .process(html, options, {}) .then(function (result) { // result.html is minified }) .catch(function (err) { console.error(err); }); ``` You might create also your own presets: ```js const htmlnano = require('htmlnano'); // Preset for minifying email templates const emailPreset = { mergeStyles: true, minifyCss: { safe: true }, }; const options = { // Some specific options }; htmlnano .process(html, options, emailPreset) .then(function (result) { // result.html is minified }) .catch(function (err) { console.error(err); }); ``` 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. ## 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`. ### collapseAttributeWhitespace Collapse redundant white spaces in list-like attributes (`class`, `rel`, `ping`). ##### Example Source: ```html
``` Minified: ```html ``` ### collapseWhitespace Collapses redundant white spaces (including new lines). It doesn’t affect white spaces in the elements ` ``` Minified (with `all`): ```html
```
Minified:
```html
```
### removeUnusedCss
Removes unused CSS inside `
```
Optimized:
```html