1 | <div align="center">
|
---|
2 | <img src="./logo/logo-web.svg" width="348.61" height="100" alt="SVGO logo"/>
|
---|
3 | </div>
|
---|
4 |
|
---|
5 | ## SVGO [![npm version](https://img.shields.io/npm/v/svgo)](https://npmjs.org/package/svgo) [![Discord](https://img.shields.io/discord/815166721315831868)](https://discord.gg/z8jX8NYxrE)
|
---|
6 |
|
---|
7 | **SVG O**ptimizer is a Node.js-based tool for optimizing SVG vector graphics files.
|
---|
8 |
|
---|
9 | ## Why?
|
---|
10 |
|
---|
11 | SVG 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.
|
---|
12 |
|
---|
13 | ## Installation
|
---|
14 |
|
---|
15 | ```sh
|
---|
16 | npm -g install svgo
|
---|
17 | ```
|
---|
18 |
|
---|
19 | or
|
---|
20 |
|
---|
21 | ```sh
|
---|
22 | yarn global add svgo
|
---|
23 | ```
|
---|
24 |
|
---|
25 | ## CLI usage
|
---|
26 |
|
---|
27 | ```sh
|
---|
28 | svgo one.svg two.svg -o one.min.svg two.min.svg
|
---|
29 | ```
|
---|
30 |
|
---|
31 | Or use the `--folder`/`-f` flag to optimize a whole folder of SVG icons
|
---|
32 |
|
---|
33 | ```sh
|
---|
34 | svgo -f ./path/to/folder/with/svg/files -o ./path/to/folder/with/svg/output
|
---|
35 | ```
|
---|
36 |
|
---|
37 | See help for advanced usage
|
---|
38 |
|
---|
39 | ```sh
|
---|
40 | svgo --help
|
---|
41 | ```
|
---|
42 |
|
---|
43 | ## Configuration
|
---|
44 |
|
---|
45 | Some options can be configured with CLI though it may be easier to have the configuration in a separate file.
|
---|
46 | SVGO automatically loads configuration from `svgo.config.js` or module specified with `--config` flag.
|
---|
47 |
|
---|
48 | ```js
|
---|
49 | module.exports = {
|
---|
50 | multipass: true, // boolean. false by default
|
---|
51 | datauri: 'enc', // 'base64', 'enc' or 'unenc'. 'base64' by default
|
---|
52 | js2svg: {
|
---|
53 | indent: 2, // string with spaces or number of spaces. 4 by default
|
---|
54 | pretty: true, // boolean, false by default
|
---|
55 | },
|
---|
56 | };
|
---|
57 | ```
|
---|
58 |
|
---|
59 | SVGO has a plugin-based architecture, so almost every optimization is a separate plugin.
|
---|
60 | There is a set of [built-in plugins](#built-in-plugins). See how to configure them:
|
---|
61 |
|
---|
62 | ```js
|
---|
63 | module.exports = {
|
---|
64 | plugins: [
|
---|
65 | // enable a built-in plugin by name
|
---|
66 | 'builtinPluginName',
|
---|
67 | // or by expanded version
|
---|
68 | {
|
---|
69 | name: 'builtinPluginName',
|
---|
70 | },
|
---|
71 | // some plugins allow/require to pass options
|
---|
72 | {
|
---|
73 | name: 'builtinPluginName',
|
---|
74 | params: {
|
---|
75 | optionName: 'optionValue',
|
---|
76 | },
|
---|
77 | },
|
---|
78 | ],
|
---|
79 | };
|
---|
80 | ```
|
---|
81 |
|
---|
82 | The default preset of plugins is fully overridden if the `plugins` field is specified.
|
---|
83 | Use `preset-default` plugin to customize plugins options.
|
---|
84 |
|
---|
85 | ```js
|
---|
86 | module.exports = {
|
---|
87 | plugins: [
|
---|
88 | {
|
---|
89 | name: 'preset-default',
|
---|
90 | params: {
|
---|
91 | overrides: {
|
---|
92 | // customize options for plugins included in preset
|
---|
93 | builtinPluginName: {
|
---|
94 | optionName: 'optionValue',
|
---|
95 | },
|
---|
96 | // or disable plugins
|
---|
97 | anotherBuiltinPlugin: false,
|
---|
98 | },
|
---|
99 | },
|
---|
100 | },
|
---|
101 | // Enable builtin plugin not included in preset
|
---|
102 | 'moreBuiltinPlugin',
|
---|
103 | // Enable and configure builtin plugin not included in preset
|
---|
104 | {
|
---|
105 | name: 'manyBuiltInPlugin',
|
---|
106 | params: {
|
---|
107 | optionName: 'value',
|
---|
108 | },
|
---|
109 | },
|
---|
110 | ],
|
---|
111 | };
|
---|
112 | ```
|
---|
113 |
|
---|
114 | Default preset includes the following list of plugins:
|
---|
115 |
|
---|
116 | - removeDoctype
|
---|
117 | - removeXMLProcInst
|
---|
118 | - removeComments
|
---|
119 | - removeMetadata
|
---|
120 | - removeEditorsNSData
|
---|
121 | - cleanupAttrs
|
---|
122 | - mergeStyles
|
---|
123 | - inlineStyles
|
---|
124 | - minifyStyles
|
---|
125 | - cleanupIDs
|
---|
126 | - removeUselessDefs
|
---|
127 | - cleanupNumericValues
|
---|
128 | - convertColors
|
---|
129 | - removeUnknownsAndDefaults
|
---|
130 | - removeNonInheritableGroupAttrs
|
---|
131 | - removeUselessStrokeAndFill
|
---|
132 | - removeViewBox
|
---|
133 | - cleanupEnableBackground
|
---|
134 | - removeHiddenElems
|
---|
135 | - removeEmptyText
|
---|
136 | - convertShapeToPath
|
---|
137 | - convertEllipseToCircle
|
---|
138 | - moveElemsAttrsToGroup
|
---|
139 | - moveGroupAttrsToElems
|
---|
140 | - collapseGroups
|
---|
141 | - convertPathData
|
---|
142 | - convertTransform
|
---|
143 | - removeEmptyAttrs
|
---|
144 | - removeEmptyContainers
|
---|
145 | - mergePaths
|
---|
146 | - removeUnusedNS
|
---|
147 | - sortDefsChildren
|
---|
148 | - removeTitle
|
---|
149 | - removeDesc
|
---|
150 |
|
---|
151 | It's also possible to specify a custom plugin:
|
---|
152 |
|
---|
153 | ```js
|
---|
154 | const anotherCustomPlugin = require('./another-custom-plugin.js');
|
---|
155 | module.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 |
|
---|
172 | SVGO provides a few low level utilities.
|
---|
173 |
|
---|
174 | ### optimize
|
---|
175 |
|
---|
176 | The core of SVGO is `optimize` function.
|
---|
177 |
|
---|
178 | ```js
|
---|
179 | const { optimize } = require('svgo');
|
---|
180 | const 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 | });
|
---|
186 | const optimizedSvgString = result.data;
|
---|
187 | ```
|
---|
188 |
|
---|
189 | ### loadConfig
|
---|
190 |
|
---|
191 | If you write a tool on top of SVGO you might need a way to load SVGO config.
|
---|
192 |
|
---|
193 | ```js
|
---|
194 | const { loadConfig } = require('svgo');
|
---|
195 | const config = await loadConfig();
|
---|
196 |
|
---|
197 | // you can also specify a relative or absolute path and customize the current working directory
|
---|
198 | const config = await loadConfig(configFile, cwd);
|
---|
199 | ```
|
---|
200 |
|
---|
201 | ## Built-in 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 | | [mergeStyles](https://github.com/svg/svgo/blob/master/plugins/mergeStyles.js) | merge multiple style elements into one | `enabled` |
|
---|
207 | | [inlineStyles](https://github.com/svg/svgo/blob/master/plugins/inlineStyles.js) | move and merge styles from `<style>` elements to element `style` attributes | `enabled` |
|
---|
208 | | [removeDoctype](https://github.com/svg/svgo/blob/master/plugins/removeDoctype.js) | remove `doctype` declaration | `enabled` |
|
---|
209 | | [removeXMLProcInst](https://github.com/svg/svgo/blob/master/plugins/removeXMLProcInst.js) | remove XML processing instructions | `enabled` |
|
---|
210 | | [removeComments](https://github.com/svg/svgo/blob/master/plugins/removeComments.js) | remove comments | `enabled` |
|
---|
211 | | [removeMetadata](https://github.com/svg/svgo/blob/master/plugins/removeMetadata.js) | remove `<metadata>` | `enabled` |
|
---|
212 | | [removeTitle](https://github.com/svg/svgo/blob/master/plugins/removeTitle.js) | remove `<title>` | `enabled` |
|
---|
213 | | [removeDesc](https://github.com/svg/svgo/blob/master/plugins/removeDesc.js) | remove `<desc>` | `enabled` |
|
---|
214 | | [removeUselessDefs](https://github.com/svg/svgo/blob/master/plugins/removeUselessDefs.js) | remove elements of `<defs>` without `id` | `enabled` |
|
---|
215 | | [removeXMLNS](https://github.com/svg/svgo/blob/master/plugins/removeXMLNS.js) | removes the `xmlns` attribute (for inline SVG) | `disabled` |
|
---|
216 | | [removeEditorsNSData](https://github.com/svg/svgo/blob/master/plugins/removeEditorsNSData.js) | remove editors namespaces, elements, and attributes | `enabled` |
|
---|
217 | | [removeEmptyAttrs](https://github.com/svg/svgo/blob/master/plugins/removeEmptyAttrs.js) | remove empty attributes | `enabled` |
|
---|
218 | | [removeHiddenElems](https://github.com/svg/svgo/blob/master/plugins/removeHiddenElems.js) | remove hidden elements | `enabled` |
|
---|
219 | | [removeEmptyText](https://github.com/svg/svgo/blob/master/plugins/removeEmptyText.js) | remove empty Text elements | `enabled` |
|
---|
220 | | [removeEmptyContainers](https://github.com/svg/svgo/blob/master/plugins/removeEmptyContainers.js) | remove empty Container elements | `enabled` |
|
---|
221 | | [removeViewBox](https://github.com/svg/svgo/blob/master/plugins/removeViewBox.js) | remove `viewBox` attribute when possible | `enabled` |
|
---|
222 | | [cleanupEnableBackground](https://github.com/svg/svgo/blob/master/plugins/cleanupEnableBackground.js) | remove or cleanup `enable-background` attribute when possible | `enabled` |
|
---|
223 | | [minifyStyles](https://github.com/svg/svgo/blob/master/plugins/minifyStyles.js) | minify `<style>` elements content with [CSSO](https://github.com/css/csso) | `enabled` |
|
---|
224 | | [convertStyleToAttrs](https://github.com/svg/svgo/blob/master/plugins/convertStyleToAttrs.js) | convert styles into attributes | `disabled` |
|
---|
225 | | [convertColors](https://github.com/svg/svgo/blob/master/plugins/convertColors.js) | convert colors (from `rgb()` to `#rrggbb`, from `#rrggbb` to `#rgb`) | `enabled` |
|
---|
226 | | [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` |
|
---|
227 | | [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` |
|
---|
228 | | [removeUnknownsAndDefaults](https://github.com/svg/svgo/blob/master/plugins/removeUnknownsAndDefaults.js) | remove unknown elements content and attributes, remove attributes with default values | `enabled` |
|
---|
229 | | [removeNonInheritableGroupAttrs](https://github.com/svg/svgo/blob/master/plugins/removeNonInheritableGroupAttrs.js) | remove non-inheritable group's "presentation" attributes | `enabled` |
|
---|
230 | | [removeUselessStrokeAndFill](https://github.com/svg/svgo/blob/master/plugins/removeUselessStrokeAndFill.js) | remove useless `stroke` and `fill` attributes | `enabled` |
|
---|
231 | | [removeUnusedNS](https://github.com/svg/svgo/blob/master/plugins/removeUnusedNS.js) | remove unused namespaces declaration | `enabled` |
|
---|
232 | | [prefixIds](https://github.com/svg/svgo/blob/master/plugins/prefixIds.js) | prefix IDs and classes with the SVG filename or an arbitrary string | `disabled` |
|
---|
233 | | [cleanupIDs](https://github.com/svg/svgo/blob/master/plugins/cleanupIDs.js) | remove unused and minify used IDs | `enabled` |
|
---|
234 | | [cleanupNumericValues](https://github.com/svg/svgo/blob/master/plugins/cleanupNumericValues.js) | round numeric values to the fixed precision, remove default `px` units | `enabled` |
|
---|
235 | | [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` |
|
---|
236 | | [moveElemsAttrsToGroup](https://github.com/svg/svgo/blob/master/plugins/moveElemsAttrsToGroup.js) | move elements' attributes to their enclosing group | `enabled` |
|
---|
237 | | [moveGroupAttrsToElems](https://github.com/svg/svgo/blob/master/plugins/moveGroupAttrsToElems.js) | move some group attributes to the contained elements | `enabled` |
|
---|
238 | | [collapseGroups](https://github.com/svg/svgo/blob/master/plugins/collapseGroups.js) | collapse useless groups | `enabled` |
|
---|
239 | | [removeRasterImages](https://github.com/svg/svgo/blob/master/plugins/removeRasterImages.js) | remove raster images | `disabled` |
|
---|
240 | | [mergePaths](https://github.com/svg/svgo/blob/master/plugins/mergePaths.js) | merge multiple Paths into one | `enabled` |
|
---|
241 | | [convertShapeToPath](https://github.com/svg/svgo/blob/master/plugins/convertShapeToPath.js) | convert some basic shapes to `<path>` | `enabled` |
|
---|
242 | | [convertEllipseToCircle](https://github.com/svg/svgo/blob/master/plugins/convertEllipseToCircle.js) | convert non-eccentric `<ellipse>` to `<circle>` | `enabled` |
|
---|
243 | | [sortAttrs](https://github.com/svg/svgo/blob/master/plugins/sortAttrs.js) | sort element attributes for epic readability | `disabled` |
|
---|
244 | | [sortDefsChildren](https://github.com/svg/svgo/blob/master/plugins/sortDefsChildren.js) | sort children of `<defs>` in order to improve compression | `enabled` |
|
---|
245 | | [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` |
|
---|
246 | | [removeAttrs](https://github.com/svg/svgo/blob/master/plugins/removeAttrs.js) | remove attributes by pattern | `disabled` |
|
---|
247 | | [removeAttributesBySelector](https://github.com/svg/svgo/blob/master/plugins/removeAttributesBySelector.js) | removes attributes of elements that match a CSS selector | `disabled` |
|
---|
248 | | [removeElementsByAttr](https://github.com/svg/svgo/blob/master/plugins/removeElementsByAttr.js) | remove arbitrary elements by `ID` or `className` | `disabled` |
|
---|
249 | | [addClassesToSVGElement](https://github.com/svg/svgo/blob/master/plugins/addClassesToSVGElement.js) | add classnames to an outer `<svg>` element | `disabled` |
|
---|
250 | | [addAttributesToSVGElement](https://github.com/svg/svgo/blob/master/plugins/addAttributesToSVGElement.js) | adds attributes to an outer `<svg>` element | `disabled` |
|
---|
251 | | [removeOffCanvasPaths](https://github.com/svg/svgo/blob/master/plugins/removeOffCanvasPaths.js) | removes elements that are drawn outside of the viewbox | `disabled` |
|
---|
252 | | [removeStyleElement](https://github.com/svg/svgo/blob/master/plugins/removeStyleElement.js) | remove `<style>` elements | `disabled` |
|
---|
253 | | [removeScriptElement](https://github.com/svg/svgo/blob/master/plugins/removeScriptElement.js) | remove `<script>` elements | `disabled` |
|
---|
254 | | [reusePaths](https://github.com/svg/svgo/blob/master/plugins/reusePaths.js) | Find duplicated <path> elements and replace them with <use> links | `disabled` |
|
---|
255 |
|
---|
256 | ## Other Ways to Use SVGO
|
---|
257 |
|
---|
258 | - as a web app – [SVGOMG](https://jakearchibald.github.io/svgomg/)
|
---|
259 | - as a GitHub Action – [SVGO Action](https://github.com/marketplace/actions/svgo-action)
|
---|
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 a 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 | - as a Linux app - [Oh My SVG](https://github.com/sonnyp/OhMySVG)
|
---|
276 | - as a Browser extension - [SVG Gobbler](https://github.com/rossmoody/svg-gobbler)
|
---|
277 | - as an API - [Vector Express](https://github.com/smidyo/vectorexpress-api#convertor-svgo)
|
---|
278 |
|
---|
279 | ## Donators
|
---|
280 |
|
---|
281 | | [<img src="https://sheetjs.com/sketch128.png" width="80">](https://sheetjs.com/) | [<img src="https://raw.githubusercontent.com/fontello/fontello/master/fontello-image.svg" width="80">](https://fontello.com/) |
|
---|
282 | | :------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------: |
|
---|
283 | | [SheetJS LLC](https://sheetjs.com/) | [Fontello](https://fontello.com/) |
|
---|
284 |
|
---|
285 | ## License and Copyright
|
---|
286 |
|
---|
287 | This software is released under the terms of the [MIT license](https://github.com/svg/svgo/blob/master/LICENSE).
|
---|
288 |
|
---|
289 | Logo by [André Castillo](https://github.com/DerianAndre).
|
---|