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 imagemin-svgo
Usage
import imagemin from 'imagemin';
import imageminSvgo from 'imagemin-svgo';
await imagemin(['images/*.svg'], {
destination: 'build/images',
plugins: [
imageminSvgo({
plugins: [{
name: 'removeViewBox',
active: false
}]
})
]
});
console.log('Images optimized');
API
imageminSvgo(options?)(buffer)
Returns a Promise<Buffer>
.
options
Type: object
Pass options to SVGO.
buffer
Type: Buffer
The buffer to optimize.