Svgr: Is there a way to pass custom config/plugins to svgo?

Created on 20 Feb 2018  路  24Comments  路  Source: gregberge/svgr

Judging from the readme, you can only enable/disable svgo and some of its plugin, but I wanted to use some other svgo plugins.

Does the svgo parameter in node work also as a config object? It's not clear in the readme.

The svgo plugins I wanted to use are removeStyleElement, convertColors: { currentColor: true }, or the inlineStyles plugin.

Most helpful comment

Replace svgo by svgoConfig and it should work!

All 24 comments

@marcofugaro it is only possible using rawConvert function, this code should help you: https://github.com/smooth-code/svgr/blob/master/src/index.js#L35

how do you use the rawConvert function?

You can deduce how to do it in the same script : https://github.com/smooth-code/svgr/blob/master/src/index.js#L52

so a thing like this will work?

{
  loader: 'svgr/webpack',
  options: {
    svgo: {
      pretty: true,
      multipass: true,
      plugins: [
        { sortAttrs: true },
        { removeDimensions: true },
        { removeStyleElement: true },
        { convertColors: { currentColor: true } },
        { removeAttrs: { attrs: '(xmlns.*)' } },
      ],
    },
    semi: false,
    singleQuote: true,
    trailingComma: 'es5',
    icon: true,
  },
},

You should be more explicit in the readme

@marcofugaro If you are using svgr/webpack you cannot use rawConvert. So for now, no solution for your problem. But configuring SVGO sound interesting. It is actually a feature to develop 馃憤.

thank you!

Why not move to a plugin architecture? There could be a package for whatever transformation the user wants (babel, prettier, svgo) and they can configure them there.

@wtgtybhertgeghgtwtg yes it is an idea!

What about a way to straight up disable SVGO? There's a bug causing viewBox to be removed in cases where it's unsafe.

You can already disable svgo using svgo option.

I tried that but it didn't have an effect, but I'm also using gatsby-plugin-svgr because I'm using Gatsby and it doesn't really give you another way to use webpack. So effectively I passed svgo: false in the options to gatsby-plugin-svgr and assume it came through.

This is a pretty critical feature IMO. I also tried chaining the SVGO loader in Webpack and it didn't work.

Is there a reason this is difficult to implement? Surely it should just be passing a configurable object to rawConvert merged with the generated options object.

I have added a pull request with the config option name svgoConfig to allow specifying custom SVGO options. If equivalent SVGR config options are specified, they will always take priority over the option defined in svgoConfig.

@neoziro what do you think about pulling out all configs and just using the ones in the projects root? E.g. if there's a prettier config, svgo config, etc, use it.

@lifeiscontent yes I think it is a good move, but have to support both

Follow up in #94 and #85.

When will this changes released? @neoziro

@bobysf12 I don't have a release date, sorry.

v2 is published 馃帀

馃帀

So does this work with v2 @neoziro?

{
  loader: 'svgr/webpack',
  options: {
    svgo: {
      pretty: true,
      multipass: true,
      plugins: [
        { sortAttrs: true },
        { removeDimensions: true },
        { removeStyleElement: true },
        { convertColors: { currentColor: true } },
        { removeAttrs: { attrs: '(xmlns.*)' } },
      ],
    },
  },
},

Replace svgo by svgoConfig and it should work!

Thank you! You're the man!

Good job with v2, the new api and options look dope as hell!

I used the svgoConfig option to create a shared config between every project to import svg icons, 脿 la create-react-app. Here are my options, might be useful to someone:

{
  loader: '@svgr/webpack',
  options: {
    svgAttributes: {
      fill: 'currentColor',
    },
    svgoConfig: {
      multipass: true,
      pretty: process.env.NODE_ENV === 'development',
      indent: 2,
      plugins: [
        { sortAttrs: true },
        { removeViewBox: false },
        { removeDimensions: true },
        { convertColors: { currentColor: true } },
      ],
    },
  },
},
Was this page helpful?
0 / 5 - 0 ratings

Related issues

7ynk3r picture 7ynk3r  路  4Comments

lm93547 picture lm93547  路  3Comments

kg-currenxie picture kg-currenxie  路  4Comments

smashercosmo picture smashercosmo  路  3Comments

1saf picture 1saf  路  3Comments