UNPKG

14.5 kBMarkdownView Raw
1<img src="https://svg.github.io/svgo-logo.svg" width="200" height="200" alt="logo"/>
2
3## SVGO [![NPM version](https://badge.fury.io/js/svgo.svg)](https://npmjs.org/package/svgo)
4
5**SVG O**ptimizer is a Nodejs-based tool for optimizing SVG vector graphics files.
6![](https://mc.yandex.ru/watch/18431326)
7
8## Why?
9
10SVG files, especially those exported from various editors, usually contain a lot of redundant and useless information. This can include editor metadata, comments, hidden elements, default or non-optimal values and other stuff that can be safely removed or converted without affecting the SVG rendering result.
11
12## Installation
13
14```sh
15npm -g svgo
16```
17or
18```sh
19yarn global add svgo
20```
21
22## CLI usage
23
24```sh
25svgo one.svg two.svg -p one.min.svg two.min.svg
26```
27
28Or use --folder, -f flag to optimize whole folder of svg icons
29```sh
30svgo -f ./path/to/folder/with/svg/files -o ./path/to/folder/with/svg/output
31```
32
33See help for advanced usage
34```sh
35svgo --help
36```
37
38## Configuration
39
40Some options can be configured with CLI though it may be easier to have configuration in separate file.
41SVGO automatically loads configuration from `svgo.config.js` or module specified with `--config` flag.
42
43```js
44module.exports = {
45 multipass: true, // boolean. false by default
46 datauri: 'enc', // 'base64', 'enc' or 'unenc'. 'base64' by default
47 js2svg: {
48 indent: 2, // string with spaces or number of spaces. 4 by default
49 pretty: true, // boolean, false by default
50 }
51}
52```
53
54SVGO has a plugin-based architecture, so almost every optimization is a separate plugin.
55There is a set of [builtin plugins](#builtin-plugins). See how to configure them:
56
57```js
58module.exports = {
59 plugins: [
60 // enable builtin plugin by name
61 'builtinPluginName',
62 // or by expanded version
63 {
64 name: 'builtinPluginName'
65 },
66 // some plugins allow/require to pass options
67 {
68 name: 'builtinPluginName',
69 params: {
70 optionName: 'optionValue'
71 }
72 }
73 ]
74}
75```
76
77If `plugins` field is specified default list is fully overrided. To extend default
78list use `extendDefaultPlugins` utility:
79
80```js
81const { extendDefaultPlugins } = require('svgo');
82module.exports = {
83 plugins: extendDefaultPlugins([
84 {
85 name: 'builtinPluginName',
86 params: {
87 optionName: 'optionValue'
88 }
89 }
90 ])
91}
92```
93
94To disable one of default plugins use `active` field:
95
96```js
97const { extendDefaultPlugins } = require('svgo');
98module.exports = {
99 plugins: extendDefaultPlugins([
100 {
101 name: 'builtinPluginName',
102 active: false
103 }
104 ])
105}
106```
107
108See the list of default plugins:
109
110```js
111module.exports = {
112 plugins: [
113 'removeDoctype',
114 'removeXMLProcInst',
115 'removeComments',
116 'removeMetadata',
117 'removeEditorsNSData',
118 'cleanupAttrs',
119 'inlineStyles',
120 'minifyStyles',
121 'convertStyleToAttrs',
122 'cleanupIDs',
123 'removeUselessDefs',
124 'cleanupNumericValues',
125 'convertColors',
126 'removeUnknownsAndDefaults',
127 'removeNonInheritableGroupAttrs',
128 'removeUselessStrokeAndFill',
129 'removeViewBox',
130 'cleanupEnableBackground',
131 'removeHiddenElems',
132 'removeEmptyText',
133 'convertShapeToPath',
134 'convertEllipseToCircle',
135 'moveElemsAttrsToGroup',
136 'moveGroupAttrsToElems',
137 'collapseGroups',
138 'convertPathData',
139 'convertTransform',
140 'removeEmptyAttrs',
141 'removeEmptyContainers',
142 'mergePaths',
143 'removeUnusedNS',
144 'sortDefsChildren',
145 'removeTitle',
146 'removeDesc'
147 ]
148}
149```
150
151It's also possible to specify custom plugin:
152
153```js
154const anotherCustomPlugin = require('./another-custom-plugin.js')
155module.exports = {
156 plugins: [
157 {
158 name: 'customPluginName',
159 type: 'perItem', // 'perItem', 'perItemReverse' or 'full'
160 params: {
161 optionName: 'optionValue',
162 },
163 fn: (ast, params, info) => {}
164 },
165 anotherCustomPlugin
166 ]
167}
168```
169
170## API usage
171
172SVGO provides a few low level utilities. `extendDefaultPlugins` is described above.
173
174### optimize
175
176The core of SVGO is `optimize` function.
177
178```js
179const { optimize } = require('svgo');
180const result = optimize(svgString, {
181 // optional but recommended field
182 path: 'path-to.svg',
183 // all config fields are also available here
184 multipass: true
185})
186const optimizedSvgString = result.data
187```
188
189### loadConfig
190
191If you write a tool on top of svgo you might need a way to load svgo config.
192
193```js
194const { loadConfig } = require('svgo');
195const config = await loadConfig()
196
197// you can also specify relative or absolute path and customize current working directory
198const config = await loadConfig(configFile, cwd)
199```
200
201## Builtin plugins
202
203| Plugin | Description | Default |
204| ------ | ----------- | ------- |
205| [cleanupAttrs](https://github.com/svg/svgo/blob/master/plugins/cleanupAttrs.js) | cleanup attributes from newlines, trailing, and repeating spaces | `enabled` |
206| [inlineStyles](https://github.com/svg/svgo/blob/master/plugins/inlineStyles.js) | move and merge styles from `<style>` elements to element `style` attributes | `enabled` |
207| [removeDoctype](https://github.com/svg/svgo/blob/master/plugins/removeDoctype.js) | remove doctype declaration | `enabled` |
208| [removeXMLProcInst](https://github.com/svg/svgo/blob/master/plugins/removeXMLProcInst.js) | remove XML processing instructions | `enabled` |
209| [removeComments](https://github.com/svg/svgo/blob/master/plugins/removeComments.js) | remove comments | `enabled` |
210| [removeMetadata](https://github.com/svg/svgo/blob/master/plugins/removeMetadata.js) | remove `<metadata>` | `enabled` |
211| [removeTitle](https://github.com/svg/svgo/blob/master/plugins/removeTitle.js) | remove `<title>` | `enabled` |
212| [removeDesc](https://github.com/svg/svgo/blob/master/plugins/removeDesc.js) | remove `<desc>` | `enabled` |
213| [removeUselessDefs](https://github.com/svg/svgo/blob/master/plugins/removeUselessDefs.js) | remove elements of `<defs>` without `id` | `enabled` |
214| [removeXMLNS](https://github.com/svg/svgo/blob/master/plugins/removeXMLNS.js) | removes `xmlns` attribute (for inline svg) | `disabled` |
215| [removeEditorsNSData](https://github.com/svg/svgo/blob/master/plugins/removeEditorsNSData.js) | remove editors namespaces, elements, and attributes | `enabled` |
216| [removeEmptyAttrs](https://github.com/svg/svgo/blob/master/plugins/removeEmptyAttrs.js) | remove empty attributes | `enabled` |
217| [removeHiddenElems](https://github.com/svg/svgo/blob/master/plugins/removeHiddenElems.js) | remove hidden elements | `enabled` |
218| [removeEmptyText](https://github.com/svg/svgo/blob/master/plugins/removeEmptyText.js) | remove empty Text elements | `enabled` |
219| [removeEmptyContainers](https://github.com/svg/svgo/blob/master/plugins/removeEmptyContainers.js) | remove empty Container elements | `enabled` |
220| [removeViewBox](https://github.com/svg/svgo/blob/master/plugins/removeViewBox.js) | remove `viewBox` attribute when possible | `enabled` |
221| [cleanupEnableBackground](https://github.com/svg/svgo/blob/master/plugins/cleanupEnableBackground.js) | remove or cleanup `enable-background` attribute when possible | `enabled` |
222| [minifyStyles](https://github.com/svg/svgo/blob/master/plugins/minifyStyles.js) | minify `<style>` elements content with [CSSO](https://github.com/css/csso) | `enabled` |
223| [convertStyleToAttrs](https://github.com/svg/svgo/blob/master/plugins/convertStyleToAttrs.js) | convert styles into attributes | `enabled` |
224| [convertColors](https://github.com/svg/svgo/blob/master/plugins/convertColors.js) | convert colors (from `rgb()` to `#rrggbb`, from `#rrggbb` to `#rgb`) | `enabled` |
225| [convertPathData](https://github.com/svg/svgo/blob/master/plugins/convertPathData.js) | convert Path data to relative or absolute (whichever is shorter), convert one segment to another, trim useless delimiters, smart rounding, and much more | `enabled` |
226| [convertTransform](https://github.com/svg/svgo/blob/master/plugins/convertTransform.js) | collapse multiple transforms into one, convert matrices to the short aliases, and much more | `enabled` |
227| [removeUnknownsAndDefaults](https://github.com/svg/svgo/blob/master/plugins/removeUnknownsAndDefaults.js) | remove unknown elements content and attributes, remove attrs with default values | `enabled` |
228| [removeNonInheritableGroupAttrs](https://github.com/svg/svgo/blob/master/plugins/removeNonInheritableGroupAttrs.js) | remove non-inheritable group's "presentation" attributes | `enabled` |
229| [removeUselessStrokeAndFill](https://github.com/svg/svgo/blob/master/plugins/removeUselessStrokeAndFill.js) | remove useless `stroke` and `fill` attrs | `enabled` |
230| [removeUnusedNS](https://github.com/svg/svgo/blob/master/plugins/removeUnusedNS.js) | remove unused namespaces declaration | `enabled` |
231| [prefixIds](https://github.com/svg/svgo/blob/master/plugins/prefixIds.js) | prefix IDs and classes with the SVG filename or an arbitrary string | `disabled` |
232| [cleanupIDs](https://github.com/svg/svgo/blob/master/plugins/cleanupIDs.js) | remove unused and minify used IDs | `enabled` |
233| [cleanupNumericValues](https://github.com/svg/svgo/blob/master/plugins/cleanupNumericValues.js) | round numeric values to the fixed precision, remove default `px` units | `enabled` |
234| [cleanupListOfValues](https://github.com/svg/svgo/blob/master/plugins/cleanupListOfValues.js) | round numeric values in attributes that take a list of numbers (like `viewBox` or `enable-background`) | `disabled` |
235| [moveElemsAttrsToGroup](https://github.com/svg/svgo/blob/master/plugins/moveElemsAttrsToGroup.js) | move elements' attributes to their enclosing group | `enabled` |
236| [moveGroupAttrsToElems](https://github.com/svg/svgo/blob/master/plugins/moveGroupAttrsToElems.js) | move some group attributes to the contained elements | `enabled` |
237| [collapseGroups](https://github.com/svg/svgo/blob/master/plugins/collapseGroups.js) | collapse useless groups | `enabled` |
238| [removeRasterImages](https://github.com/svg/svgo/blob/master/plugins/removeRasterImages.js) | remove raster images | `disabled` |
239| [mergePaths](https://github.com/svg/svgo/blob/master/plugins/mergePaths.js) | merge multiple Paths into one | `enabled` |
240| [convertShapeToPath](https://github.com/svg/svgo/blob/master/plugins/convertShapeToPath.js) | convert some basic shapes to `<path>` | `enabled` |
241| [convertEllipseToCircle](https://github.com/svg/svgo/blob/master/plugins/convertEllipseToCircle.js) | convert non-eccentric `<ellipse>` to `<circle>` | `enabled` |
242| [sortAttrs](https://github.com/svg/svgo/blob/master/plugins/sortAttrs.js) | sort element attributes for epic readability | `disabled` |
243| [sortDefsChildren](https://github.com/svg/svgo/blob/master/plugins/sortDefsChildren.js) | sort children of `<defs>` in order to improve compression | `enabled` |
244| [removeDimensions](https://github.com/svg/svgo/blob/master/plugins/removeDimensions.js) | remove `width`/`height` and add `viewBox` if it's missing (opposite to removeViewBox, disable it first) | `disabled` |
245| [removeAttrs](https://github.com/svg/svgo/blob/master/plugins/removeAttrs.js) | remove attributes by pattern | `disabled` |
246| [removeAttributesBySelector](https://github.com/svg/svgo/blob/master/plugins/removeAttributesBySelector.js) | removes attributes of elements that match a css selector | `disabled` |
247| [removeElementsByAttr](https://github.com/svg/svgo/blob/master/plugins/removeElementsByAttr.js) | remove arbitrary elements by ID or className | `disabled` |
248| [addClassesToSVGElement](https://github.com/svg/svgo/blob/master/plugins/addClassesToSVGElement.js) | add classnames to an outer `<svg>` element | `disabled` |
249| [addAttributesToSVGElement](https://github.com/svg/svgo/blob/master/plugins/addAttributesToSVGElement.js) | adds attributes to an outer `<svg>` element | `disabled` |
250| [removeOffCanvasPaths](https://github.com/svg/svgo/blob/master/plugins/removeOffCanvasPaths.js) | removes elements that are drawn outside of the viewbox | `disabled` |
251| [removeStyleElement](https://github.com/svg/svgo/blob/master/plugins/removeStyleElement.js) | remove `<style>` elements | `disabled` |
252| [removeScriptElement](https://github.com/svg/svgo/blob/master/plugins/removeScriptElement.js) | remove `<script>` elements | `disabled` |
253| [reusePaths](https://github.com/svg/svgo/blob/master/plugins/reusePaths.js) | Find duplicated <path> elements and replace them with <use> links | `disabled` |
254
255## Other Ways to Use SVGO
256
257* as a web app – [SVGOMG](https://jakearchibald.github.io/svgomg/)
258* as a GitHub Action – [SVGO Action](https://github.com/marketplace/actions/svgo-action)
259* as a Nodejs module – [examples](https://github.com/svg/svgo/tree/master/examples)
260* as a Grunt task – [grunt-svgmin](https://github.com/sindresorhus/grunt-svgmin)
261* as a Gulp task – [gulp-svgmin](https://github.com/ben-eb/gulp-svgmin)
262* as a Mimosa module – [mimosa-minify-svg](https://github.com/dbashford/mimosa-minify-svg)
263* as an OSX Folder Action – [svgo-osx-folder-action](https://github.com/svg/svgo-osx-folder-action)
264* as a webpack loader – [image-webpack-loader](https://github.com/tcoopman/image-webpack-loader)
265* as a Telegram Bot – [svgo_bot](https://github.com/maksugr/svgo_bot)
266* as a PostCSS plugin – [postcss-svgo](https://github.com/ben-eb/postcss-svgo)
267* as an Inkscape plugin – [inkscape-svgo](https://github.com/konsumer/inkscape-svgo)
268* as a Sketch plugin - [svgo-compressor](https://github.com/BohemianCoding/svgo-compressor)
269* as macOS app - [Image Shrinker](https://image-shrinker.com)
270* as a Rollup plugin - [rollup-plugin-svgo](https://github.com/porsager/rollup-plugin-svgo)
271* as a VS Code plugin - [vscode-svgo](https://github.com/1000ch/vscode-svgo)
272* as a Atom plugin - [atom-svgo](https://github.com/1000ch/atom-svgo)
273* as a Sublime plugin - [Sublime-svgo](https://github.com/1000ch/Sublime-svgo)
274* as a Figma plugin - [Advanced SVG Export](https://www.figma.com/c/plugin/782713260363070260/Advanced-SVG-Export)
275
276## Backers
277
278| [<img src="https://sheetjs.com/sketch128.png" width="80">](https://sheetjs.com/) | [<img src="https://rawgithub.com/fontello/fontello/master/fontello-image.svg" width="80">](http://fontello.com/) |
279|:-:|:-:|
280| [SheetJS LLC](https://sheetjs.com/) | [Fontello](http://fontello.com/) |
281
282## Donations
283
284- PayPal: https://www.paypal.me/deepsweet
285
286## License and Copyright
287
288This software is released under the terms of the [MIT license](https://github.com/svg/svgo/blob/master/LICENSE).
289
290Logo by [Yegor Bolshakov](http://xizzzy.ru/).