Skip to content

sketch-hq/svgo-compressor

Repository files navigation

SVGO Compressor

A Plugin that compresses SVG assets using SVGO, right when you export them.

Install

  • Download SVGO Compressor & unzip it.
  • Double click SVGO Compressor.sketchplugin to install the Plugin.

Usage

Selecting menu items or hitting keys is out of fashion. SVGO Compressor will compress your SVG assets whenever you export them, without you having to do anything.

You’ll get a message on your document window to let you know the compression worked as expected.

If you need uncompressed SVG assets, you can temporarily disable the Plugin by opening Sketch’s Preferences › Plugins and unchecking 'SVGO Compressor'. Or you can right-click any layer and select Copy SVG Code, and that will give you the original, uncompressed code.

Custom SVGO configuration

SVGO Compressor uses a default configuration that does a reasonable job of compressing SVG code, while maintaining compatibility and avoiding rendering issues. If you need to change the defaults, you can do so by creating an svgo.config.js file in Sketch's Plugins directory (located by default in ~/Library/Application\ Support/com.bohemiancoding.sketch3/Plugins/).

For a complete reference of what your SVGO config should look like, see SVGO’s configuration documentation.

Any option that is not set on your custom configuration will use the defaults set by SVGO Compressor. For example, here's how a sample configuration to output unminified code could look like:

module.exports = {
  js2svg: {
    indent: 2,
    pretty: true,
  }
}

Keep in mind that our defaults do not match the ones in SVGO 100%. If you use the preset-default option in SVGO your results may vary from the ones this plugin exports. For the record, here's the default configuration we use:

{
  path: currentFile, // This is the path to the currently exported SVG asset
  multipass: true,
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          inlineStyles: false,
          convertStyleToAttrs: true,
          cleanupListOfValues: true,
          removeViewBox: false,
          cleanupEnableBackground: false,
          removeHiddenElems: false,
          convertShapeToPath: false,
          moveElemsAttrsToGroup: false,
          moveGroupAttrsToElems: false,
          convertPathData: false,
          sortAttrs: true,
        }
      }
    }
  ],
}

Again, for more information about custom configurations please refer to SVGO's own documentation. Please note that the custom plugins feature is untested in SVGO Compressor, so it may or may not work.

Acknowledgements

We would like to thank:

Development

This plugin is built using skpm. To build it, just run

npm i
npm run build

To edit the Plugin's code, edit the code in src and run npm run build. You can also run npm run watch to automatically rebuild the Plugin every time you make changes in the code.