rollup-plugin-imagemin
is a Rollup plugin that uses imagemin
to optimize images in your Rollup build. If you've used imagemin
on any other platform before, this will feel familiar to you.
npm i rollup-plugin-imagemin --save-dev
// rollup.config.js
import { imagemin } from "rollup-plugin-imagemin";
export default {
plugins: [
imagemin()
],
input: "src/index.js"
output: {
format: "esm",
file: "./dist/index.js"
}
};
// src/index.js
import someImage from "./some-image.png"; // <-- With the above config, this should output an optimized PNG to the dist folder.
rollup-plugin-imagemin
has number of useful options to help you tune your builds to your liking:
disable
(default:false
): Disable all optimizations and output unoptimized images. Useful for speedier development builds.verbose
(default:false
): Enables verbose logging, such as optimization gains.emitFiles
(default:true
): Whether to emit files. Could be useful for server side builds. Be aware that unlessdisable
is set totrue
, images will still be optimized in memory, but will not be written to disk.hashLength
(default:16
): The length of hashes used in asset filenames.include
(default:"**/*.{svg,png,jpg,jpeg,gif}"
): File glob pattern of assets to be processed byrollup-plugin-imagemin
.exclude
(default:""
): File glob pattern of assets to not be processed byrollup-plugin-imagemin
. The pattern defined byexclude
is applied after the value of theinclude
option pattern.fileName
(default:"[name]-[hash][extname]"
): The output filename pattern of images optimized byrollup-plugin-imagemin
. The pattern includes the following tokens:[name]
: The basename of the input file.[hash]
: The has of the input file.[extname]
: The extension of the input file.
publicPath
(default:""
): A folder for where to put optimized assets. Use this to separate your images into a separate folder.preserveTree
(default:false
): Iftrue
, preserve directory structure relative toprocess.cwd()
. Can also be a path specifying root from where directory structure should be preserved.gifsicle
: (default:{ optimizationLevel: 3 }
): Settings to merge with default, to pass toimagemin-gifsicle
.jpegtran
(default:{ progressive: true }
): Settings to merge with default, to pass toimagemin-jpegtran
.pngquant
: (default:{ speed: 1, strip: true }
): Settings to merge with default, to pass toimagemin-pngquant
.svgo
: (default:{ precision: 1, multipass: true }
): Settings to merge with default, to pass toimagemin-svgo
.plugins
: object with plugin names as keys and plugins as value to pass toimagemin
. By default,{gifsicle: 'imagemin-gifsicle', jpegtran: 'imagemin-jpegtran', pngquant: 'imagemin-pngquant', svgo: 'imagemin-svgo'}
are used. Each plugin function must be a factory, taking the plugin's config (the object atoptions[pluginName]
, merged with defaults), and returning an imagemin buffer transformer.
You can use custom plugins the following way:
// rollup.config.js
import imagemin from "rollup-plugin-imagemin";
import myCustomPlugin from "imagemin-my-custom-plugin";
export default {
plugins: [
imagemin({
myCustomPlugin: {
// Config to pass to `myCustomPlugin`'s factory
},
plugins: {
myCustomPlugin,
}
})
],
input: "src/index.js"
output: {
format: "esm",
file: "./dist/index.js"
}
};
Please read the contributing guidelines in CONTRIBUTING.md
.
This is my first Rollup plugin. As such, I drew extensive help from the Rollup documentation, but also from the rollup-plugin-url
and rollup-plugin-image
source code. If anything in the plugin looks familiar to either of those two, it's no coincidence, and I owe a lot to the authors of those plugins for inspiration and guidance.