Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to pass custom config/plugins to svgo? #52

Closed
marcofugaro opened this issue Feb 20, 2018 · 24 comments
Closed

Is there a way to pass custom config/plugins to svgo? #52

marcofugaro opened this issue Feb 20, 2018 · 24 comments

Comments

@marcofugaro
Copy link

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.

@gregberge
Copy link
Owner

@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

@marcofugaro
Copy link
Author

how do you use the rawConvert function?

@gregberge
Copy link
Owner

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

@marcofugaro
Copy link
Author

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,
  },
},

@marcofugaro
Copy link
Author

You should be more explicit in the readme

@gregberge
Copy link
Owner

@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 👍.

@marcofugaro
Copy link
Author

thank you!

@wtgtybhertgeghgtwtg
Copy link

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.

@gregberge
Copy link
Owner

@wtgtybhertgeghgtwtg yes it is an idea!

@coreyward
Copy link

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

@gregberge
Copy link
Owner

You can already disable svgo using svgo option.

@coreyward
Copy link

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.

@ryall
Copy link

ryall commented Apr 4, 2018

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.

@ryall
Copy link

ryall commented Apr 4, 2018

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.

@lifeiscontent
Copy link
Contributor

@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.

@gregberge
Copy link
Owner

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

@gregberge
Copy link
Owner

Follow up in #94 and #85.

@bobysf12
Copy link

When will this changes released? @neoziro

@gregberge
Copy link
Owner

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

@gregberge
Copy link
Owner

v2 is published 🎉

@marcofugaro
Copy link
Author

🎉

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.*)' } },
      ],
    },
  },
},

@gregberge
Copy link
Owner

Replace svgo by svgoConfig and it should work!

@marcofugaro
Copy link
Author

Thank you! You're the man!

@marcofugaro
Copy link
Author

marcofugaro commented Jun 18, 2018

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 } },
      ],
    },
  },
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants