1.0.0 • Published 3 years ago

metalsmith-minify-svg v1.0.0

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

metalsmith-minify-svg

A metalsmith plugin for minifying SVG images.

This plugin minifies all of the SVG images found in Metalsmith files and inline SVG images by using the svgo library.

Installation

npm install metalsmith-minify-svg

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 minifySvg = require('metalsmith-minify-svg');

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

JSON

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

Options

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

  • htmlPattern: optional. Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is **/*.html.
  • svgPattern: optional. Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is **/*.svg.
  • svgoOptions: optional. Options used in the svgo library to modify the SVG minification process. Accepts and object. The default is {}.

htmlPattern

Only files that match this pattern will be processed. So this metalsmith.json:

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-minify-svg": {
      "htmlPattern": "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.

svgPattern

Only files that match this pattern will be processed. So this metalsmith.json:

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

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

svgoOptions

Options used by the svgo library to minify SVG. So this Metalsmith JavaScript configuration or metalsmith.json:

JavaScript

const Metalsmith = require('metalsmith');
const minifySvg = require('metalsmith-minify-svg');

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

JSON

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-minify-svg": {
      "svgoOptions": {},
    }
  }
}

Would specify different options to be passes to the svgo library when running the SVG minification.

License

AGPLv3

1.0.0

3 years ago