metalsmith-minify-png v1.0.1
metalsmith-minify-png
A metalsmith plugin for minifying PNG images.
This plugin minifies all of the PNG images found in Metalsmith files by using the Pngquant and Zopflipng programs. If Pngquant and Zopflipng are not installed, then minification of Pngquant and Zopflipng images will be skipped.
Installation
npm install metalsmith-minify-png
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 minifyPng = require('metalsmith-minify-png');
Metalsmith(__dirname)
.use(minifyPng())
.build((err, files) => {
if (err) { throw err; }
});
JSON
{
"plugins": {
"metalsmith-minify-png": {}
}
}
Options
You can pass options to metalsmith-minify-png
with the Javascript API or CLI. The options are:
- pattern: optional. Only PNG files that match this pattern will be processed. Accepts a string or an array of strings. The default is
**/*.png
. - usePngquant: optional. If true, will use the
pngquant
program to minify PNGs (however, if Pngquant is not installed, will skip this option). Accepts a boolean value. The default istrue
. - useZopflipng: optional. If true, will use the
zopflipng
program to minify PNGs (however, if Zopflipng is not installed, will skip this option). Accepts a boolean value. The default istrue
. - speed: optional. The level of speed specified in the
--speed
argument for Pngquant that sets the level of optimization. Accepts a string or a number. The default isnull
(no speed level will be set). - strip: optional. If
true
, will strip the optional PNG metadata. Accepts a boolean value. The default isfalse
. - iterations: optional. Set the number of iterations to use with Zopflipng. Accepts a number. The default is
15
.
pattern
Only files that match this pattern will be processed for PNG minification. So this Metalsmith JavaScript configuration or metalsmith.json
:
JavaScript
const Metalsmith = require('metalsmith');
const minifyPng = require('metalsmith-minify-png');
Metalsmith(__dirname)
.use(minifyPng({
pattern: 'blog/**/*.png',
}))
.build((err, files) => {
if (err) { throw err; }
});
JSON
{
"source": "src",
"destination": "build",
"plugins": {
"metalsmith-minify-png": {
"pattern": "blog/**/*.png"
}
}
}
Would only process PNG files within the ./src/blog
folder, because the pattern
is relative to your source folder. See multimatch
for further details.
usePngquant
If set to true
, will use the pngquant
program to minify PNG files. So this Metalsmith JavaScript configuration or metalsmith.json
:
JavaScript
const Metalsmith = require('metalsmith');
const minifyPng = require('metalsmith-minify-png');
Metalsmith(__dirname)
.use(minifyPng({
usePngquant: true,
}))
.build((err, files) => {
if (err) { throw err; }
});
JSON
{
"source": "src",
"destination": "build",
"plugins": {
"metalsmith-minify-png": {
"usePngquant": true
}
}
}
Would process PNG files using Pngquant.
useZopflipng
If set to true
, will use the zopflipng
program to minify PNG files. So this Metalsmith JavaScript configuration or metalsmith.json
:
JavaScript
const Metalsmith = require('metalsmith');
const minifyPng = require('metalsmith-minify-png');
Metalsmith(__dirname)
.use(minifyPng({
useZopflipng: true,
}))
.build((err, files) => {
if (err) { throw err; }
});
JSON
{
"source": "src",
"destination": "build",
"plugins": {
"metalsmith-minify-png": {
"useZopflipng": true
}
}
}
Would process PNG files using Zopflipng.
speed
The speed
parameter sets the level of optimization when running Pngquant program. So this Metalsmith JavaScript configuration or metalsmith.json
:
JavaScript
const Metalsmith = require('metalsmith');
const minifyPng = require('metalsmith-minify-png');
Metalsmith(__dirname)
.use(minifyPng({
speed: null,
}))
.build((err, files) => {
if (err) { throw err; }
});
JSON
{
"source": "src",
"destination": "build",
"plugins": {
"metalsmith-minify-png": {
"speed": null
}
}
}
Would disable the --speed
parameter for Pngquant (the default value 4
will be used).
strip
The strip
parameter determines if optional metadata will be removed when running Pngquant program. So this Metalsmith JavaScript configuration or metalsmith.json
:
JavaScript
const Metalsmith = require('metalsmith');
const minifyPng = require('metalsmith-minify-png');
Metalsmith(__dirname)
.use(minifyPng({
strip: false,
}))
.build((err, files) => {
if (err) { throw err; }
});
JSON
{
"source": "src",
"destination": "build",
"plugins": {
"metalsmith-minify-png": {
"strip": false
}
}
}
Would disable the --strip
parameter for Pngquant (no optional metadata will be removed).
iterations
The iterations
parameter determines how many iterations will be used with the Zopflipng program. So this Metalsmith JavaScript configuration or metalsmith.json
:
JavaScript
const Metalsmith = require('metalsmith');
const minifyPng = require('metalsmith-minify-png');
Metalsmith(__dirname)
.use(minifyPng({
iterations: 15,
}))
.build((err, files) => {
if (err) { throw err; }
});
JSON
{
"source": "src",
"destination": "build",
"plugins": {
"metalsmith-minify-png": {
"iterations": 15
}
}
}
Would use 15 iterations with the Zopflipng program.