A Metalsmith plug-in to minify HTML files.
JavaScript
Clone or download
whymarrh Merge pull request #37 from whymarrh/build-updates
Update CI node versions and lockfile
Latest commit 1001274 Jun 12, 2018
Permalink
Failed to load latest commit information.
__mocks__
__tests__
lib Merge pattern and minify opts into a single object arg Dec 19, 2017
.gitattributes Add Git attrs file Oct 3, 2017
.gitignore
.jscsrc Add jscs rc Feb 9, 2016
.npmignore Add npm ignore file Aug 12, 2014
.travis.yml
CHANGELOG.md
LICENSE.md Add MIT license Aug 10, 2014
README.md Update README to show a single options argument Dec 19, 2017
package-lock.json
package.json 3.0.0 Jan 1, 2018

README.md

Metalsmith HTML Minifier

npm

A Metalsmith plug-in to minify HTML files using kangax/html-minifier.

Be aware that certain optimizations are enabled by default in this plug-in, some of which may not be what you want. Please read the documentation to ensure you understand what is happening to your markup and disable options as required.

Installation

The usual:

$ npm install --save metalsmith-html-minifier

How do I use this thing?

A quick example:

var Metalsmith   = require("metalsmith");
var htmlMinifier = require("metalsmith-html-minifier");
Metalsmith(__dirname)
    .use(htmlMinifier()) // Use the default options
    .build();

The above will minify all the HTML files (files ending in .html) it processes. To provide a custom glob for files to minify:

var Metalsmith   = require("metalsmith");
var htmlMinifier = require("metalsmith-html-minifier");
Metalsmith(__dirname)
    .use(htmlMinifier({
        pattern: "**/*.html",
    }))
    .build();

You can also pass an array of globs to match filenames:

var Metalsmith   = require("metalsmith");
var htmlMinifier = require("metalsmith-html-minifier");
Metalsmith(__dirname)
    .use(htmlMinifier({
        pattern: ["**/*.html", "**/*.xhtml"],
    }))
    .build();

To pass options to the minifier (to enable or disable optimizations):

var Metalsmith   = require("metalsmith");
var htmlMinifier = require("metalsmith-html-minifier");
Metalsmith(__dirname)
    .use(htmlMinifier({
        minifierOptions: {
            removeComments: false,
            // etc.
        },
    }))
    .build();

Options

See Options Quick Reference for a full reference. The options passed to the plug-in are merged with the defaults below and passed to the minifier directly.

The following options are enabled by default. To disable any of these you will need to explicitly set them to false.

Option Description
collapseBooleanAttributes Omit attribute values from boolean attributes
collapseWhitespace Collapse white space that contributes to text nodes in a document tree
removeAttributeQuotes Remove quotes around attributes when possible
removeComments Strip HTML comments
removeEmptyAttributes Remove all attributes with whitespace-only values
removeRedundantAttributes Remove attributes when value matches default

Tests

To run the tests:

$ npm test

License

This software is released under the MIT License. See LICENSE.md for more information.