What is svgo?
The svgo npm package is a Node.js-based tool for optimizing SVG vector graphics files. SVGO stands for Scalable Vector Graphics Optimizer. It works by applying a series of transformations and optimizations to SVG files to reduce their size without affecting their visual quality. This is particularly useful for web development, where smaller file sizes can lead to faster load times and better performance.
What are svgo's main functionalities?
Minify SVG files
This feature allows you to minify SVG files by removing unnecessary data without affecting the rendering of the SVG. The code sample demonstrates how to use the optimize function to minify an SVG string.
const { optimize } = require('svgo');
const svgString = '<svg ...> ... </svg>';
const result = optimize(svgString, { path: 'path/to/svg/file.svg' });
console.log(result.data);
Remove specified attributes
This feature allows you to remove specified attributes from SVG elements. The code sample shows how to use the removeAttributesBySelector plugin to remove the 'fill' attribute from all elements that have it.
const { optimize } = require('svgo');
const svgString = '<svg ...> ... </svg>';
const result = optimize(svgString, {
plugins: [
{
name: 'removeAttributesBySelector',
params: {
selector: '[fill]',
attributes: 'fill'
}
}
]
});
console.log(result.data);
Prettify SVG files
This feature allows you to prettify SVG files by reformatting them with consistent indentation and spacing. The code sample demonstrates how to use the js2svg option with the pretty parameter set to true.
const { optimize } = require('svgo');
const svgString = '<svg ...> ... </svg>';
const result = optimize(svgString, {
plugins: [
'preset-default',
'sortAttrs',
{
name: 'removeAttrs',
params: { attrs: '(stroke|fill)' }
}
],
js2svg: { pretty: true }
});
console.log(result.data);
Other packages similar to svgo
imagemin-svgo
imagemin-svgo is a plugin for Imagemin, which is a general image optimization framework. While svgo focuses solely on SVG files, Imagemin can handle various image formats when combined with the appropriate plugins. Imagemin-svgo brings the capabilities of svgo to the Imagemin ecosystem.
svg-sprite
svg-sprite is a package that takes a set of SVG files and combines them into a single sprite sheet. While svgo optimizes individual SVG files, svg-sprite focuses on creating an efficient way to bundle multiple SVGs for use on the web.
svg-crowbar
svg-crowbar is a tool designed to extract SVG elements from an HTML document and download them as standalone SVG files. It is different from svgo, which optimizes existing SVG files rather than extracting them from HTML.
SVGO
SVGO, short for SVG Optimizer, is a Node.js library and command-line application for optimizing SVG files.
Why?
SVG files, especially those exported from vector editors, usually contain a lot of redundant information. This includes editor metadata, comments, hidden elements, default or suboptimal values, and other stuff that can be safely removed or converted without impacting rendering.
Installation
You can install SVGO globally through npm, yarn, or pnpm. Alternatively, drop the global flag (global
/-g
) to use it in your Node.js project.
npm install -g svgo
yarn global add svgo
pnpm add -g svgo
Command-line usage
Process single files:
svgo one.svg two.svg -o one.min.svg two.min.svg
Process a directory of files recursively with -f
/--folder
:
svgo -f path/to/directory_with_svgs -o path/to/output_directory
Help for advanced usage:
svgo --help
Configuration
SVGO has a plugin architecture. You can read more about all plugins in Plugins | SVGO Documentation, and the default plugins in Preset Default | SVGO Documentation.
SVGO reads the configuration from svgo.config.js
or the --config path/to/config.js
command-line option. Some other parameters can be configured though command-line options too.
svgo.config.js
module.exports = {
multipass: false,
datauri: 'base64',
js2svg: {
indent: 4,
pretty: false,
},
plugins: [
'preset-default',
'prefixIds',
{
name: 'prefixIds',
params: {
prefix: 'uwu',
},
},
],
};
Default preset
Instead of configuring SVGO from scratch, you can tweak the default preset to suit your needs by configuring or disabling the respective plugin.
svgo.config.js
module.exports = {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
inlineStyles: {
onlyMatchedOnce: false,
},
},
},
},
],
};
You can find a list of the default plugins in the order they run in Preset Default | SVGO Documentation.
Custom plugins
You can also specify custom plugins:
svgo.config.js
const importedPlugin = require('./imported-plugin');
module.exports = {
plugins: [
importedPlugin,
{
name: 'customPlugin',
params: {
paramName: 'paramValue',
},
fn: (ast, params, info) => {},
},
],
};
API usage
SVGO provides a few low level utilities.
optimize
The core of SVGO is optimize
function.
const { optimize } = require('svgo');
const result = optimize(svgString, {
path: 'path-to.svg',
multipass: true,
});
const optimizedSvgString = result.data;
loadConfig
If you write a tool on top of SVGO you may want to resolve the svgo.config.js
file.
const { loadConfig } = require('svgo');
const config = await loadConfig();
You can also specify a path and customize the current working directory.
const config = await loadConfig(configFile, cwd);
Other ways to use SVGO
Donors
License and Copyright
This software is released under the terms of the MIT license.
Logo by André Castillo.