- Notifications
You must be signed in to change notification settings - Fork1.4k
⚙️ Node.js tool for optimizing SVG files
License
svg/svgo
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
SVGO, short forSVG Optimizer, is a Node.js library and command-line application for optimizing SVG files.
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.
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.
# npmnpm install -g svgo# yarnyarn global add svgo# pnpmpnpm add -g svgo
Process single files:
svgo one.svg two.svg -o one.min.svg two.min.svg
Process a directory of files recursively with-r
/--recursive
and-f
/--folder
:
svgo -rf path/to/directory_with_svgs -o path/to/output_directory
Help for advanced usage:
svgo --help
SVGO has a plugin architecture. You can read more about all plugins inPlugins | SVGO Documentation, and the default plugins inPreset Default | SVGO Documentation.
SVGO reads the configuration fromsvgo.config.mjs
or the--config path/to/config.mjs
command-line option. Some other parameters can be configured though command-line options too.
svgo.config.mjs
exportdefault{multipass:false,// booleandatauri:'base64',// 'base64'|'enc'|'unenc'js2svg:{indent:4,// numberpretty:false,// boolean},plugins:['preset-default',// built-in plugins enabled by default'prefixIds',// enable built-in plugins by name// enable built-in plugins with an object to configure plugins{name:'prefixIds',params:{prefix:'uwu',},},],};
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.mjs
exportdefault{plugins:[{name:'preset-default',params:{overrides:{// disable a default plugincleanupIds:false,// customize the params of a default plugininlineStyles:{onlyMatchedOnce:false,},},},},],};
You can find a list of the default plugins in the order they run inPreset Default | SVGO Documentation.
You can also specify custom plugins:
svgo.config.mjs
importimportedPluginfrom'./imported-plugin';exportdefault{plugins:[// plugin imported from another JavaScript fileimportedPlugin,// plugin defined inline{name:'customPlugin',params:{paramName:'paramValue',},fn:(ast,params,info)=>{},},],};
SVGO provides a few low level utilities.
The core of SVGO isoptimize
function.
import{optimize}from'svgo';constresult=optimize(svgString,{path:'path-to.svg',// recommendedmultipass:true,// all other config fields are available here});constoptimizedSvgString=result.data;
If you write a tool on top of SVGO you may want to resolve thesvgo.config.mjs
file.
import{loadConfig}from'svgo';constconfig=awaitloadConfig();
You can also specify a path and customize the current working directory.
constconfig=awaitloadConfig(configFile,cwd);
SheetJS LLC | Fontello |
This software is released under the terms of theMIT license.
Logo byAndré Castillo.
About
⚙️ Node.js tool for optimizing SVG files