What is imagemin-svgo?
The imagemin-svgo npm package is a plugin for Imagemin that uses SVGO (SVG Optimizer) to compress and optimize SVG files. It helps reduce the file size of SVG images by removing unnecessary data without affecting the visual quality.
What are imagemin-svgo's main functionalities?
Basic SVG Optimization
This code demonstrates how to use imagemin-svgo to optimize SVG files in the 'images' directory and save the optimized files to the 'output/images' directory.
const imagemin = require('imagemin');
const imageminSvgo = require('imagemin-svgo');
(async () => {
await imagemin(['images/*.svg'], {
destination: 'output/images',
plugins: [
imageminSvgo()
]
});
console.log('SVG images optimized');
})();
Custom SVGO Options
This code shows how to use imagemin-svgo with custom SVGO options. In this example, the 'removeViewBox' and 'cleanupIDs' plugins are configured to preserve the viewBox attribute and IDs in the SVG files.
const imagemin = require('imagemin');
const imageminSvgo = require('imagemin-svgo');
(async () => {
await imagemin(['images/*.svg'], {
destination: 'output/images',
plugins: [
imageminSvgo({
plugins: [
{ removeViewBox: false },
{ cleanupIDs: false }
]
})
]
});
console.log('SVG images optimized with custom options');
})();
Other packages similar to imagemin-svgo
svgo
SVGO (SVG Optimizer) is a standalone tool for optimizing SVG files. It provides a command-line interface and can be used as a library in Node.js applications. While imagemin-svgo is a plugin for Imagemin, svgo can be used independently for more direct control over SVG optimization.
svgmin
svgmin is another tool for optimizing SVG files. It is similar to svgo but offers different optimization strategies and configurations. svgmin can be used as a command-line tool or integrated into build processes. It provides an alternative to imagemin-svgo with its own set of features and options.
imagemin-svgo
svgo imagemin plugin
Install
$ npm install --save imagemin-svgo
Usage
var Imagemin = require('imagemin');
var svgo = require('imagemin-svgo');
var imagemin = new Imagemin()
.src('images/*.svg')
.dest('build/images')
.use(svgo());
imagemin.run(function (err, files) {
if (err) {
throw err;
}
console.log('Files optimized successfully!');
});
You can also use this plugin with gulp:
var gulp = require('gulp');
var svgo = require('imagemin-svgo');
gulp.task('default', function () {
return gulp.src('images/*.svg')
.pipe(svgo()())
.pipe(gulp.dest('build/images'));
});
Options
multipass
Type: Boolean
Default: false
Optimize image multiple times until it's fully optimized.
plugins
Type: Array
Default: []
Customize which SVGO plugins to use.
var imagemin = new Imagemin()
.use(svgo({ plugins: [{ removeViewBox: false }, { removeEmptyAttrs: false }] }));
License
MIT © imagemin