Svgo: Support external plugins

Created on 13 May 2017  路  6Comments  路  Source: svg/svgo

I have used svgo on a nodejs project and I couldn't find a way to define a plugin from outside of svgo module so I had to fork the library to add my own plugins. Am I missing something or is svgo plugins designed this way on purpose?

Most helpful comment

Speaking of this, if I reading lib/svgo/config.js correctly, you can pass plugins as functions with config like this. (Haven't tried it really.)

{
    plugins: [
        {
            myPlugin: {
                name: 'myPlugin',
                params: {},
                fn: function(data) { /*...*/ }
            }
        }
    ]
}

My question was related to passing plugins as files.

All 6 comments

You're right, currently, there is no design to connect external plugin. How do you think such an API would look like?

Hum, maybe the less disruptive approach would be to have something like this:

const svgo = new SVGO({
    definePlugins: [
        renameAttrsToCamelCase: {
          type: "perItem",
          active: true,
          description: 'Convert attributes names to camelCase',
          params: {},
          fn: (item, params) => {
            // Blabla my logic here 
          }
        }
    ],
    usePlugins: [
        'removeComments',
        'removeMetadata'
    ]
});

I can work on a PR if you are happy with that.

Speaking of this, if I reading lib/svgo/config.js correctly, you can pass plugins as functions with config like this. (Haven't tried it really.)

{
    plugins: [
        {
            myPlugin: {
                name: 'myPlugin',
                params: {},
                fn: function(data) { /*...*/ }
            }
        }
    ]
}

My question was related to passing plugins as files.

It may be a nice solution to use liftoff as base for plugin loading.

@GreLI I was able to get an external plugin to work using your example as a start, it's just missing the type property. I needed a custom plugin to remove the ID attr from the svg element. Here's my working config with custom and built-in plugins mixed together:

{
  plugins : [
    {
      removeSvgId : {
        type        : 'perItem',
        name        : 'removeSvgId',
        description : 'Removes the ID attr from the <svg> element',
        fn          : function(item) {
          if (item.isElem('svg')) {
            item.removeAttr('id');
          }
        }
      }
    },
    {sortAttrs   : true},
    {removeTitle : false},
  ],
}

I don't think any other plugin loading system is needed, this way works perfectly, it just needs to be documented.

@Enchiridion Thanks for pointing that out. However, if I copy/paste your code (or create a even more simpler/empty plugin) in a config.js file, I am getting:

YAMLException: missed comma between flow collection entries at line 8, column 38: ... fn : function(item) {

Any idea?
btw. I am on a Mac (10.14.4)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guylando picture guylando  路  3Comments

gregberge picture gregberge  路  3Comments

Roman52 picture Roman52  路  5Comments

bmcminn picture bmcminn  路  4Comments

wuz picture wuz  路  3Comments