Svgo: How to include custom, local, plugin

Created on 6 Jul 2016  路  5Comments  路  Source: svg/svgo

Is there a way to add custom plugins to SVGO? I have a small plugin I'd like to include as a build step in a project.

For example, I'd like to run svgo with a command line option pointing to a directory that contains custom plugins. Or point to said directory in my .svgo.yml configuration file. Is this possible?

Most helpful comment

So looking at the source I think something like this is already supported. When I pass in a configuration looking like this:

{ 
  plugins: [
    { 
     custom: { // this could be any name except the already registered ones
        type: 'full', // full, perItem or perItemReverse
        description: 'My custom plugin',
        params: {}, // some arbitrary data
        fn: function(data, params) {
          // custom plugin code goes here
          return data;
        }
     }
  ]
}

I get to execute my code in a custom manner.

It seems that the only limitation is:

  • Only supports inline code, so you have to invoke SVGO via the API rather than using command line or YAML.

All 5 comments

No, it's not possible for now.

@GreLI would you be happy for a contribution adding this ability?

Yes

So looking at the source I think something like this is already supported. When I pass in a configuration looking like this:

{ 
  plugins: [
    { 
     custom: { // this could be any name except the already registered ones
        type: 'full', // full, perItem or perItemReverse
        description: 'My custom plugin',
        params: {}, // some arbitrary data
        fn: function(data, params) {
          // custom plugin code goes here
          return data;
        }
     }
  ]
}

I get to execute my code in a custom manner.

It seems that the only limitation is:

  • Only supports inline code, so you have to invoke SVGO via the API rather than using command line or YAML.

Oh sweet! Thanks for looking into this @gampleman!

In my opinion it would be ideal to have a config (or command line) option that points to a local directory of custom plugins that following the conventions of the plugins bundled inside SVGO. This directory would essentially be merged with bundled plugins.

I peeked at the source when I created this issue but didn't have time to investigate further.

Was this page helpful?
0 / 5 - 0 ratings