Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/svg/svgo
⚙️ Node.js tool for optimizing SVG files
https://github.com/svg/svgo
cli javascript minification nodejs optimization svg svgo
Last synced: 6 days ago
JSON representation
⚙️ Node.js tool for optimizing SVG files
- Host: GitHub
- URL: https://github.com/svg/svgo
- Owner: svg
- License: mit
- Created: 2012-08-22T10:41:43.000Z (over 12 years ago)
- Default Branch: main
- Last Pushed: 2024-12-22T16:01:23.000Z (21 days ago)
- Last Synced: 2024-12-30T14:07:17.613Z (13 days ago)
- Topics: cli, javascript, minification, nodejs, optimization, svg, svgo
- Language: JavaScript
- Homepage: https://svgo.dev/
- Size: 5.9 MB
- Stars: 21,164
- Watchers: 242
- Forks: 1,391
- Open Issues: 242
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome - svg/svgo - ⚙️ Node.js tool for optimizing SVG files (JavaScript)
- Front-End-Checklist - SVGO - based tool for optimizing SVG vector graphics files. (Images / Best practices)
- Front-End-Checklist - SVGO - based tool for optimizing SVG vector graphics files. (Images / Best practices)
- awesome-nodejs - svgo - ⚙️ Node.js tool for optimizing SVG files. ![](https://img.shields.io/github/stars/lovell/sharp.svg?style=social&label=Star) (Repository / Image)
- awesome-ccamel - svg/svgo - ⚙️ Node.js tool for optimizing SVG files (JavaScript)
- awesome-github-repos - svg/svgo - ⚙️ Node.js tool for optimizing SVG files (JavaScript)
- awesome-svg - cli - written in JavaScript and plugs into a whole bunch of different build tools. (Compression)
- awesome-list - svgo
- awesome-performance - svgo - **SVG O**ptimizer is a Nodejs-based tool for optimizing SVG vector graphics files ([Web GUI](https://jakearchibald.github.io/svgomg/)). (Converter)
- StarryDivineSky - svg/svgo
- jimsghstars - svg/svgo - ⚙️ Node.js tool for optimizing SVG files (JavaScript)
- stars - svgo
- stars - svgo
README
# 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/)
SVGO, short for **SVG O**ptimizer, is a Node.js library and command-line application for optimizing SVG files.
## Why?
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.
## Installation
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.
```sh
# npm
npm install -g svgo# yarn
yarn global add svgo# pnpm
pnpm add -g svgo
```## Command-line usage
Process single files:
```sh
svgo one.svg two.svg -o one.min.svg two.min.svg
```Process a directory of files recursively with `-r`/`--recursive` and `-f`/`--folder`:
```sh
svgo -rf path/to/directory_with_svgs -o path/to/output_directory
```Help for advanced usage:
```sh
svgo --help
```## Configuration
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/).
SVGO reads the configuration from `svgo.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`**
```js
export default {
multipass: false, // boolean
datauri: 'base64', // 'base64'|'enc'|'unenc'
js2svg: {
indent: 4, // number
pretty: 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',
},
},
],
};
```### Default preset
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`**
```js
export default {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
// disable a default plugin
cleanupIds: false,// customize the params of a default plugin
inlineStyles: {
onlyMatchedOnce: false,
},
},
},
},
],
};
```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).
### Custom plugins
You can also specify custom plugins:
**`svgo.config.mjs`**
```js
import importedPlugin from './imported-plugin';export default {
plugins: [
// plugin imported from another JavaScript file
importedPlugin,// plugin defined inline
{
name: 'customPlugin',
params: {
paramName: 'paramValue',
},
fn: (ast, params, info) => {},
},
],
};
```## API usage
SVGO provides a few low level utilities.
### optimize
The core of SVGO is `optimize` function.
```js
import { optimize } from 'svgo';const result = optimize(svgString, {
path: 'path-to.svg', // recommended
multipass: true, // all other config fields are available here
});const optimizedSvgString = result.data;
```### loadConfig
If you write a tool on top of SVGO you may want to resolve the `svgo.config.mjs` file.
```js
import { loadConfig } from 'svgo';const config = await loadConfig();
```You can also specify a path and customize the current working directory.
```js
const config = await loadConfig(configFile, cwd);
```## Donors
| [](https://sheetjs.com/) | [](https://fontello.com/) |
| :------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------: |
| [SheetJS LLC](https://sheetjs.com/) | [Fontello](https://fontello.com/) |## License and Copyright
This software is released under the terms of the [MIT license](https://github.com/svg/svgo/blob/main/LICENSE).
Logo by [André Castillo](https://github.com/DerianAndre).