Socket
Socket
Sign inDemoInstall

svgo

Package Overview
Dependencies
3
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    svgo

Nodejs-based tool for optimizing SVG vector graphics files


Version published
Maintainers
1
Install size
756 kB
Created

Package description

What is svgo?

The svgo npm package is a Node.js-based tool for optimizing SVG vector graphics files. SVGO stands for Scalable Vector Graphics Optimizer. It works by applying a series of transformations and optimizations to SVG files to reduce their size without affecting their visual quality. This is particularly useful for web development, where smaller file sizes can lead to faster load times and better performance.

What are svgo's main functionalities?

Minify SVG files

This feature allows you to minify SVG files by removing unnecessary data without affecting the rendering of the SVG. The code sample demonstrates how to use the optimize function to minify an SVG string.

const { optimize } = require('svgo');
const svgString = '<svg ...> ... </svg>';
const result = optimize(svgString, { path: 'path/to/svg/file.svg' });
console.log(result.data);

Remove specified attributes

This feature allows you to remove specified attributes from SVG elements. The code sample shows how to use the removeAttributesBySelector plugin to remove the 'fill' attribute from all elements that have it.

const { optimize } = require('svgo');
const svgString = '<svg ...> ... </svg>';
const result = optimize(svgString, {
  plugins: [
    {
      name: 'removeAttributesBySelector',
      params: {
        selector: '[fill]',
        attributes: 'fill'
      }
    }
  ]
});
console.log(result.data);

Prettify SVG files

This feature allows you to prettify SVG files by reformatting them with consistent indentation and spacing. The code sample demonstrates how to use the js2svg option with the pretty parameter set to true.

const { optimize } = require('svgo');
const svgString = '<svg ...> ... </svg>';
const result = optimize(svgString, {
  plugins: [
    'preset-default',
    'sortAttrs',
    {
      name: 'removeAttrs',
      params: { attrs: '(stroke|fill)' }
    }
  ],
  js2svg: { pretty: true }
});
console.log(result.data);

Other packages similar to svgo

Readme

Source
o-o o   o o--o o-o
 \   \ /  |  | | |
o-o   o   o--o o-o
             | 
          o--o

SVGO

SVG Optimizer is a Nodejs-based tool for optimizing SVG vector graphics files.

Why?

SVG files, especially exported from various editors, usually contains a lot of redundant and useless information such as editor metadata, comments, hidden elements and other stuff that can be safely removed without affecting SVG rendering result.

What it can do

SVGO has a plugin-based architecture, so almost every optimization is a separate plugin.

Today we have:

  • [ > ] cleanup attributes from newlines, trailing and repeating spaces
  • [ > ] remove doctype declaration
  • [ > ] remove XML processing instructions
  • [ > ] remove comments
  • [ > ] remove metadata
  • [ > ] remove editors namespaces, elements and attributes
  • [ > ] remove empty attributes
  • [ > ] remove default "px" unit
  • [ > ] remove a lot of hidden elements
  • [ > ] remove empty Text elements
  • [ > ] remove empty Container elements
  • [ > ] convert styles into attributes
  • [ > ] convert colors
  • [ > ] move elements attributes to the existing group wrapper
  • [ > ] collapse groups

But it's not only about rude removing, SVG has a strict specification with a lot of opportunities for optimizations, default values, geometry hacking and more.

How-to instructions and plugins API docs will coming ASAP.

How to use

npm install -g svgo
Usage:
  svgo [OPTIONS] [ARGS]


Options:
  -h, --help : Help
  -v, --version : Version
  -c CONFIG, --config=CONFIG : Local config
  -d DISABLE, --disable=DISABLE : Disable plugin
  -e ENABLE, --enable=ENABLE : Enable plugin
  -i INPUT, --input=INPUT : Input file (default: stdin)
  -o OUTPUT, --output=OUTPUT : Output file (default: stdout)
svgo -i test.svg -o test.min.svg
cat test.svg | svgo -d removeDoctype -d removeComment > test.min.svg

TODO

It's only the very first public alpha :)

  1. documentation!
  2. phantomjs-based server-side SVG rendering "before vs after" tests
  3. more unit tests
  4. more plugins

Keywords

FAQs

Last updated on 29 Sep 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc