You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
22 KiB

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