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

SVGO Override Behavior #38

Closed
jhoffmcd opened this issue May 16, 2019 · 8 comments
Closed

SVGO Override Behavior #38

jhoffmcd opened this issue May 16, 2019 · 8 comments

Comments

@jhoffmcd
Copy link

jhoffmcd commented May 16, 2019

I was unable to get the config to keep the viewBox over the width / height attributes given an SVG that has both. See this CodePen:

https://codesandbox.io/s/gatsbysvgrsvgotest-e4i19

I would expect that this config would yield a check.svg SVG that only has viewBox:

svgoConfig: {
  removeDimensions: true,
  removeViewBox: false,
},

But you can see, it's the reverse and we only see the width / height retained on the svg element. Does this mean SVGO rule overrides may not be applied?

@coreyward
Copy link
Collaborator

I suspect this is upstream in SVGR (or SVGO, though that's somewhat less likely). This plugin really doesn't do much other than swap the webpack loader that Gatsby configures for SVGs. We pass all of the other options along to @svgr/webpack. If you find that there's something we can do to make this work, though, please let us know.

@tremby
Copy link
Contributor

tremby commented May 21, 2019

I had the same problem. I worked around it by using an SVGO configuration file rather than trying to set the configuration in the Gatsby configuration file.

The SVGR docs are pretty terrible but if you look under "SVGO" on this page https://www.smooth-code.com/open-source/svgr/docs/options/#svgo you'll see a link to "see SVGO options", which points here https://gist.github.com/pladaria/69321af86ce165c2c1fc1c718b098dd0

There's no actual documentation there, but I made a guess at its point... I'm not sure if this is an SVGR thing or SVGO thing, but making a file with that same name .svgo.yml in your project root does the trick. You can then remove the options for gatsby-plugin-svgr from your Gatsby config file.

My .svgo.yml is this:

multipass: true
plugins:
  - removeDimensions: true
  - removeViewBox: false

I have no idea whether this indicates a bug in SVGR or SVGO. Maybe one of them simply changed how configuration should be given.

@coreyward
Copy link
Collaborator

coreyward commented May 22, 2019 via email

@jhoffmcd
Copy link
Author

@tremby you are totally correct!

I've updated our project with the yml file and its working like a charm.

@zabute
Copy link
Owner

zabute commented May 29, 2019

Thanks, @tremby for this much needed workaround.

@coreyward I certainly agree with encouraging using external configuration for now but I'm hesitant to officially break api parity with svgr without figuring out why this is happening and whether or not it can be fixed. Wouldn't you agree?

@zabute zabute pinned this issue May 29, 2019
@coreyward
Copy link
Collaborator

@zabute Yeah, ideally we'd support configuration of SVGR as though Gatsby wasn't in the equation, but for the sake of letting folks get on with their work I think we ought to stop providing an example configuration if it isn't working.

I think if we want to keep the SVGR options long term we need someone that wants to use them to pitch in and figure out what's going on. I use the out-of-the-box defaults in projects I work on and don't have any spare hours to dive into what's happening here, unfortunately.

glennreyes added a commit to glennreyes/glennreyes.com that referenced this issue Jul 19, 2019
@zabute
Copy link
Owner

zabute commented Jul 22, 2019

It appears svgo config keys have to be under svgoConfig.plugins. See #40

@ElijahWoelbing
Copy link

This worked for me

    {
      resolve: 'gatsby-plugin-svgr',
      options: {
        include: /images\/icons\/.*\.svg/,
        svgo: true,
        svgoConfig: {
          plugins: [
            {
              name: 'preset-default',
              params: {
                overrides: {
                  removeViewBox: false,
                },
              },
            },
          ],
        },
      }
    }

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

No branches or pull requests

5 participants