Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/svgoPublic

⚙️ Node.js tool for optimizing SVG files

License

NotificationsYou must be signed in to change notification settings

svg/svgo

Repository files navigation

SVGOnpmchatdocs

SVGO, short forSVG Optimizer, 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.

# npmnpm install -g svgo# yarnyarn global add svgo# pnpmpnpm add -g svgo

Command-line usage

Process single files:

svgo one.svg two.svg -o one.min.svg two.min.svg

Process a directory of files recursively with-r/--recursive and-f/--folder:

svgo -rf path/to/directory_with_svgs -o path/to/output_directory

Help for advanced usage:

svgo --help

Configuration

SVGO has a plugin architecture. You can read more about all plugins inPlugins | SVGO Documentation, and the default plugins inPreset Default | SVGO Documentation.

SVGO reads the configuration fromsvgo.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

exportdefault{multipass:false,// booleandatauri:'base64',// 'base64'|'enc'|'unenc'js2svg:{indent:4,// numberpretty: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

exportdefault{plugins:[{name:'preset-default',params:{overrides:{// disable a default plugincleanupIds:false,// customize the params of a default plugininlineStyles:{onlyMatchedOnce:false,},},},},],};

You can find a list of the default plugins in the order they run inPreset Default | SVGO Documentation.

Custom plugins

You can also specify custom plugins:

svgo.config.mjs

importimportedPluginfrom'./imported-plugin';exportdefault{plugins:[// plugin imported from another JavaScript fileimportedPlugin,// 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 isoptimize function.

import{optimize}from'svgo';constresult=optimize(svgString,{path:'path-to.svg',// recommendedmultipass:true,// all other config fields are available here});constoptimizedSvgString=result.data;

loadConfig

If you write a tool on top of SVGO you may want to resolve thesvgo.config.mjs file.

import{loadConfig}from'svgo';constconfig=awaitloadConfig();

You can also specify a path and customize the current working directory.

constconfig=awaitloadConfig(configFile,cwd);

Donors

SheetJS LLCFontello

License and Copyright

This software is released under the terms of theMIT license.

Logo byAndré Castillo.


[8]ページ先頭

©2009-2025 Movatter.jp