Skip to content

posthtml/htmlnano

Repository files navigation

htmlnano

npm version CI

Modular HTML minifier, built on top of the PostHTML. Inspired by cssnano.

Benchmarks

Website Source (KB) html-minifier-terser html-minifier-next htmlnano
stackoverflow.blog 342 1.7% 1.7% 4.1%
github.com 541 3.7% 3.7% 18.1%
en.wikipedia.org 218 4.6% 4.6% 5.0%
npmjs.com 459 0.5% 0.5% 0.9%
tc39.es 7198 8.6% 8.6% 8.7%
reddit.com 539 6.1% 6.1% 6.9%
apple.com 190 7.6% 7.6% 12.3%
w3.org 48 19.0% 19.0% 23.2%
weather.com 1823 0.2% 0.2% 13.1%
Avg. minify rate 0% 5.8% 5.8% 10.3%

Latest benchmarks: https://github.com/maltsev/html-minifiers-benchmark (updated daily).

Documentation

https://htmlnano.netlify.app

Usage

npm install htmlnano
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);
    });

More usage examples (PostHTML, Gulp, Webpack): https://htmlnano.netlify.app/next/usage