metalsmith-minify-jpeg v1.0.0
metalsmith-minify-jpeg
A metalsmith plugin for minifying JPEG images.
This plugin minifies all of the JPEG images found in Metalsmith files by using the Jpegoptim program. If Jpegoptim is not installed, ImageMagick will be used as a fallback, and if ImageMagick is not installed then minification of JPEG images will be skipped.
Installation
npm install metalsmith-minify-jpegUsage
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 minifyJpeg = require('metalsmith-minify-jpeg');
Metalsmith(__dirname)
.use(minifyJpeg())
.build((err, files) => {
if (err) { throw err; }
});JSON
{
"plugins": {
"metalsmith-minify-jpeg": {}
}
}Options
You can pass options to metalsmith-minify-jpeg with the Javascript API or CLI. The options are:
- pattern: optional. Only JPEG files that match this pattern will be processed. Accepts a string or an array of strings. The default is
**/*.jpeg,**/*.jpg,**/*.jpe. - quality: optional. The level of quality of the JPEG image in the
--maxargument for Jpegoptim and the-qualityargument for ImageMagick. Accepts a string or a number. The default isnull(no quality level will be set).
pattern
Only files that match this pattern will be processed for JPEG minification. So this Metalsmith JavaScript configuration or metalsmith.json:
JavaScript
const Metalsmith = require('metalsmith');
const minifyJpeg = require('metalsmith-minify-jpeg');
Metalsmith(__dirname)
.use(minifyJpeg({
pattern: 'blog/**/*.jpeg',
}))
.build((err, files) => {
if (err) { throw err; }
});JSON
{
"source": "src",
"destination": "build",
"plugins": {
"metalsmith-minify-jpeg": {
"pattern": "blog/**/*.jpeg"
}
}
}Would only process JPEG files within the ./src/blog folder, because the pattern
is relative to your source folder. See multimatch
for further details.
quality
The quality parameter sets the quality level when running Jpegoptim or ImageMagick programs. So this Metalsmith JavaScript configuration or metalsmith.json:
JavaScript
const Metalsmith = require('metalsmith');
const minifyJpeg = require('metalsmith-minify-jpeg');
Metalsmith(__dirname)
.use(minifyJpeg({
quality: 20,
}))
.build((err, files) => {
if (err) { throw err; }
});JSON
{
"source": "src",
"destination": "build",
"plugins": {
"metalsmith-minify-jpeg": {
"quality": 20
}
}
}Would set the quality level to 20 when running Jpegoptim or ImageMagick.
License
4 years ago