1.0.1 • Published 3 years ago

metalsmith-minify-html v1.0.1

Weekly downloads
-
License
AGPLv3
Repository
github
Last release
3 years ago

metalsmith-minify-html

A metalsmith plugin for minifying HTML markup.

This plugin minifies all of the HTML markup in the HTML files. Under the hood, this plugin uses html-minifier, and uses a default set of options focusing on high minification levels.

Installation

npm install metalsmith-minify-html

Usage

To use this plugin, simply add it to the existing plugins in your Metalsmith source file or include it in the Metalsmith JSON file:

JavaScript

const Metalsmith = require('metalsmith');
const minifyHtml = require('metalsmith-minify-html');

Metalsmith(__dirname)
  .use(minifyHtml())
  .build((err, files) => {
    if (err) { throw err; }
  });

JSON

{
  "plugins": {
    "metalsmith-minify-html": {}
  }
}

Options

You can pass options to metalsmith-minify-html with the Javascript API or CLI. The options are:

  • pattern: optional. Only GIF files that match this pattern will be processed. Accepts a string or an array of strings. The default is **/*.gif.
  • htmlMinifierOptions: optional. The options used by the html-minifier. Accepts an object. The default is the following object:
{
  caseSensitive: true,
  collapseBooleanAttributes: true,
  collapseInlineTagWhitespace: true,
  collapseWhitespace: true,
  conservativeCollapse: false,
  continueOnParseError: true,
  html5: true,
  keepClosingSlash: false,
  minifyURLs: false,
  preserveLineBreaks: false,
  removeAttributeQuotes: true,
  removeComments: true,
  removeEmptyAttributes: true,
  removeScriptTypeAttributes: true,
  removeStyleLinkTypeAttributes: true,
  trimCustomFragments: true,
  useShortDoctype: true,
  collapseEmptyAttributes: true,
}

pattern

Only HTML files that match this pattern will be minified. So this Metalsmith JavaScript configuration or metalsmith.json:

JavaScript

const Metalsmith = require('metalsmith');
const minifyHtml = require('metalsmith-minify-html');

Metalsmith(__dirname)
  .use(minifyHtml({
    pattern: 'blog/**/*.html',
  }))
  .build((err, files) => {
    if (err) { throw err; }
  });

JSON

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-minify-html": {
      "pattern": "blog/**/*.html"
    }
  }
}

Would only process HTML files within the ./src/blog folder, because the pattern is relative to your source folder. See multimatch for further details.

htmlMinifierOptions

The htmlMinifierOptions parameter sets parameters used by the html-minifier library. So this Metalsmith JavaScript configuration or metalsmith.json:

JavaScript

const Metalsmith = require('metalsmith');
const minifyHtml = require('metalsmith-minify-html');

Metalsmith(__dirname)
  .use(minifyGifs({
    htmlMinifierOptions: {
      caseSensitive: true,
      collapseBooleanAttributes: true,
      collapseInlineTagWhitespace: true,
      collapseWhitespace: true,
      conservativeCollapse: false,
      continueOnParseError: true,
      html5: true,
      keepClosingSlash: false,
      minifyURLs: false,
      preserveLineBreaks: false,
      removeAttributeQuotes: true,
      removeComments: true,
      removeEmptyAttributes: true,
      removeScriptTypeAttributes: true,
      removeStyleLinkTypeAttributes: true,
      trimCustomFragments: true,
      useShortDoctype: true,
      collapseEmptyAttributes: true,
    },
  }))
  .build((err, files) => {
    if (err) { throw err; }
  });

JSON

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-minify-html": {
      "htmlMinifierOptions": {
        "caseSensitive": true,
        "collapseBooleanAttributes": true,
        "collapseInlineTagWhitespace": true,
        "collapseWhitespace": true,
        "conservativeCollapse": false,
        "continueOnParseError": true,
        "html5": true,
        "keepClosingSlash": false,
        "minifyURLs": false,
        "preserveLineBreaks": false,
        "removeAttributeQuotes": true,
        "removeComments": true,
        "removeEmptyAttributes": true,
        "removeScriptTypeAttributes": true,
        "removeStyleLinkTypeAttributes": true,
        "trimCustomFragments": true,
        "useShortDoctype": true,
        "collapseEmptyAttributes": true
      }
    }
  }
}

Would modify the options used by the html-minifier library.

License

AGPLv3

1.0.1

3 years ago

1.0.0

3 years ago