1 | <div align="center">
|
2 | <img src="./logo/logo-web.svg" width="348.61" height="100" alt=""/>
|
3 | </div>
|
4 |
|
5 | # SVGO [![npm](https://img.shields.io/npm/v/svgo)](https://npmjs.org/package/svgo) [![chat](https://img.shields.io/discord/815166721315831868)](https://discord.gg/z8jX8NYxrE) [![docs](https://img.shields.io/badge/docs-svgo.dev-blue)](https://svgo.dev/)
|
6 |
|
7 | SVGO, short for **SVG O**ptimizer, is a Node.js library and command-line application for optimizing SVG files.
|
8 |
|
9 | ## Why?
|
10 |
|
11 | 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.
|
12 |
|
13 | ## Installation
|
14 |
|
15 | 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.
|
16 |
|
17 | ```sh
|
18 | # npm
|
19 | npm install -g svgo
|
20 |
|
21 | # yarn
|
22 | yarn global add svgo
|
23 |
|
24 | # pnpm
|
25 | pnpm add -g svgo
|
26 | ```
|
27 |
|
28 | ## Command-line usage
|
29 |
|
30 | Process single files:
|
31 |
|
32 | ```sh
|
33 | svgo one.svg two.svg -o one.min.svg two.min.svg
|
34 | ```
|
35 |
|
36 | Process a directory of files recursively with `-f`/`--folder`:
|
37 |
|
38 | ```sh
|
39 | svgo -f path/to/directory_with_svgs -o path/to/output_directory
|
40 | ```
|
41 |
|
42 | Help for advanced usage:
|
43 |
|
44 | ```sh
|
45 | svgo --help
|
46 | ```
|
47 |
|
48 | ## Configuration
|
49 |
|
50 | SVGO has a plugin architecture. You can read more about all plugins in [Plugins | SVGO Documentation](https://svgo.dev/docs/plugins/), and the default plugins in [Preset Default | SVGO Documentation](https://svgo.dev/docs/preset-default/).
|
51 |
|
52 | 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.
|
53 |
|
54 | **`svgo.config.js`**
|
55 |
|
56 | ```js
|
57 | module.exports = {
|
58 | multipass: false, // boolean
|
59 | datauri: 'base64', // 'base64'|'enc'|'unenc'
|
60 | js2svg: {
|
61 | indent: 4, // number
|
62 | pretty: false, // boolean
|
63 | },
|
64 | plugins: [
|
65 | 'preset-default', // built-in plugins enabled by default
|
66 | 'prefixIds', // enable built-in plugins by name
|
67 |
|
68 | // enable built-in plugins with an object to configure plugins
|
69 | {
|
70 | name: 'prefixIds',
|
71 | params: {
|
72 | prefix: 'uwu',
|
73 | },
|
74 | },
|
75 | ],
|
76 | };
|
77 | ```
|
78 |
|
79 | ### Default preset
|
80 |
|
81 | Instead of configuring SVGO from scratch, you can tweak the default preset to suit your needs by configuring or disabling the respective plugin.
|
82 |
|
83 | **`svgo.config.js`**
|
84 |
|
85 | ```js
|
86 | module.exports = {
|
87 | plugins: [
|
88 | {
|
89 | name: 'preset-default',
|
90 | params: {
|
91 | overrides: {
|
92 | // disable a default plugin
|
93 | removeViewBox: false,
|
94 |
|
95 | // customize the params of a default plugin
|
96 | inlineStyles: {
|
97 | onlyMatchedOnce: false,
|
98 | },
|
99 | },
|
100 | },
|
101 | },
|
102 | ],
|
103 | };
|
104 | ```
|
105 |
|
106 | You can find a list of the default plugins in the order they run in [Preset Default | SVGO Documentation](https://svgo.dev/docs/preset-default/#plugins-list).
|
107 |
|
108 | ### Custom plugins
|
109 |
|
110 | You can also specify custom plugins:
|
111 |
|
112 | **`svgo.config.js`**
|
113 |
|
114 | ```js
|
115 | const importedPlugin = require('./imported-plugin');
|
116 |
|
117 | module.exports = {
|
118 | plugins: [
|
119 | // plugin imported from another JavaScript file
|
120 | importedPlugin,
|
121 |
|
122 | // plugin defined inline
|
123 | {
|
124 | name: 'customPlugin',
|
125 | params: {
|
126 | paramName: 'paramValue',
|
127 | },
|
128 | fn: (ast, params, info) => {},
|
129 | },
|
130 | ],
|
131 | };
|
132 | ```
|
133 |
|
134 | ## API usage
|
135 |
|
136 | SVGO provides a few low level utilities.
|
137 |
|
138 | ### optimize
|
139 |
|
140 | The core of SVGO is `optimize` function.
|
141 |
|
142 | ```js
|
143 | const { optimize } = require('svgo');
|
144 |
|
145 | const result = optimize(svgString, {
|
146 | path: 'path-to.svg', // recommended
|
147 | multipass: true, // all other config fields are available here
|
148 | });
|
149 |
|
150 | const optimizedSvgString = result.data;
|
151 | ```
|
152 |
|
153 | ### loadConfig
|
154 |
|
155 | If you write a tool on top of SVGO you may want to resolve the `svgo.config.js` file.
|
156 |
|
157 | ```js
|
158 | const { loadConfig } = require('svgo');
|
159 |
|
160 | const config = await loadConfig();
|
161 | ```
|
162 |
|
163 | You can also specify a path and customize the current working directory.
|
164 |
|
165 | ```js
|
166 | const config = await loadConfig(configFile, cwd);
|
167 | ```
|
168 |
|
169 | ## Other ways to use SVGO
|
170 |
|
171 | | Method | Reference |
|
172 | | ------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
|
173 | | Web app | [SVGOMG](https://jakearchibald.github.io/svgomg/) |
|
174 | | Grunt task | [grunt-svgmin](https://github.com/sindresorhus/grunt-svgmin) |
|
175 | | Gulp task | [gulp-svgmin](https://github.com/ben-eb/gulp-svgmin) |
|
176 | | Webpack loader | [image-minimizer-webpack-plugin](https://github.com/webpack-contrib/image-minimizer-webpack-plugin/#optimize-with-svgo) |
|
177 | | PostCSS plugin | [postcss-svgo](https://github.com/cssnano/cssnano/tree/master/packages/postcss-svgo) |
|
178 | | Inkscape plugin | [inkscape-svgo](https://github.com/konsumer/inkscape-svgo) |
|
179 | | Sketch plugin | [svgo-compressor](https://github.com/BohemianCoding/svgo-compressor) |
|
180 | | Rollup plugin | [rollup-plugin-svgo](https://github.com/porsager/rollup-plugin-svgo) |
|
181 | | Visual Studio Code plugin | [vscode-svgo](https://github.com/1000ch/vscode-svgo) |
|
182 | | Atom plugin | [atom-svgo](https://github.com/1000ch/atom-svgo) |
|
183 | | Sublime plugin | [Sublime-svgo](https://github.com/1000ch/Sublime-svgo) |
|
184 | | Figma plugin | [Advanced SVG Export](https://www.figma.com/c/plugin/782713260363070260/Advanced-SVG-Export) |
|
185 | | Linux app | [Oh My SVG](https://github.com/sonnyp/OhMySVG) |
|
186 | | Browser extension | [SVG Gobbler](https://github.com/rossmoody/svg-gobbler) |
|
187 | | API | [Vector Express](https://github.com/smidyo/vectorexpress-api#convertor-svgo) |
|
188 |
|
189 | ## Donors
|
190 |
|
191 | | [<img src="https://sheetjs.com/sketch128.png" width="80">](https://sheetjs.com/) | [<img src="https://raw.githubusercontent.com/fontello/fontello/8.0.0/fontello-image.svg" width="80">](https://fontello.com/) |
|
192 | | :------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------: |
|
193 | | [SheetJS LLC](https://sheetjs.com/) | [Fontello](https://fontello.com/) |
|
194 |
|
195 | ## License and Copyright
|
196 |
|
197 | This software is released under the terms of the [MIT license](https://github.com/svg/svgo/blob/main/LICENSE).
|
198 |
|
199 | Logo by [André Castillo](https://github.com/DerianAndre).
|