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.
o-o o o o--o o-o
\ \ / | | | |
o-o o o--o o-o
|
o--o
SVGO
SVG Optimizer is a Nodejs-based tool for optimizing SVG vector graphics files.
Why?
SVG files, especially exported from various editors, usually contains a lot of redundant and useless information such as editor metadata, comments, hidden elements and other stuff that can be safely removed without affecting SVG rendering result.
What it can do
SVGO has a plugin-based architecture, so almost every optimization is a separate plugin.
Today we have:
- [ > ] cleanup attributes from newlines, trailing and repeating spaces
- [ > ] remove doctype declaration
- [ > ] remove XML processing instructions
- [ > ] remove comments
- [ > ] remove metadata
- [ > ] remove editors namespaces, elements and attributes
- [ > ] remove empty attributes
- [ > ] remove default "px" unit
- [ > ] remove a lot of hidden elements
- [ > ] remove empty Text elements
- [ > ] remove empty Container elements
- [ > ] convert styles into attributes
- [ > ] convert colors
- [ > ] move elements attributes to the existing group wrapper
- [ > ] collapse groups
But it's not only about rude removing, SVG has a strict specification with a lot of opportunities for optimizations, default values, geometry hacking and more.
How-to instructions and plugins API docs will coming ASAP.
How to use
npm install -g svgo
Usage:
svgo [OPTIONS] [ARGS]
Options:
-h, --help : Help
-v, --version : Version
-c CONFIG, --config=CONFIG : Local config
-d DISABLE, --disable=DISABLE : Disable plugin
-e ENABLE, --enable=ENABLE : Enable plugin
-i INPUT, --input=INPUT : Input file (default: stdin)
-o OUTPUT, --output=OUTPUT : Output file (default: stdout)
svgo -i test.svg -o test.min.svg
cat test.svg | svgo -d removeDoctype -d removeComment > test.min.svg
TODO
It's only the very first public alpha :)
- documentation!
- phantomjs-based server-side SVG rendering "before vs after" tests
- more unit tests
- more plugins
- …