HTMLMinifier
HTMLMinifier is a highly configurable, well-tested, JavaScript-based HTML minifier.
Installation
From NPM for use as a command line app:
npm install html-minifier-terser -gFrom NPM for programmatic use:
npm install html-minifier-terserUsage
Note that almost all options are disabled by default. Experiment and find what works best for you and your project.
For command line usage please see html-minifier-terser --help for a list of available options.
Sample command line:
html-minifier-terser --collapse-whitespace --remove-comments --minify-js trueNode.js
const { minify } = require('html-minifier-terser');
const result = await minify('<p title="blah" id="moo">foo</p>', {
removeAttributeQuotes: true,
});
result; // '<p title=blah id=moo>foo</p>'See corresponding blog post for all the gory details of how it works, description of each option, testing results and conclusions.
Also see corresponding Ruby wrapper, and for Node.js, Grunt plugin, Gulp plugin, Koa middleware wrapper and Express middleware wrapper.
For lint-like capabilities take a look at HTMLLint.