I must be looking in all the wrong places; sorry if so.
How can I disable stripping the viewBox attribute from SVGs?
Relatedly, is there a way to add a viewBox if it doesn't already exist? (i.e. turn width="w" height="h" into width="w" height="h" viewBox="0 0 w h")
// in .svgrrc.js
module.exports = {
svgoConfig: {
plugins: {
removeViewBox: false
}
}
};
Thanks, that got me on the right lines. That didn't work for me (I may have made a typo) but I saw in the options I can do a YAML file instead. I ended up writing a file .svgo.yml with the following contents:
plugins:
- removeViewBox: false
is there a way to specify this option with @svgr/webpack loader?
okay, if anyone runs into the same thing. i've managed to make it work with webpack config like so:
js
{
test: /(icons|images)\/.*?.svg$/,
use: [{
loader: '@svgr/webpack',
options: {
svgoConfig: {
plugins: {
removeViewBox: false
}
}
}
}, 'file-loader']
}
Any idea if there's a way to do that in a require statement, i.e. with no
special Webpack configuration?
If you're configuring/disabling several svgo plugins, remember that they each have to be in a separate object:
svgoConfig: {
multipass: true,
plugins: [
{
cleanupIDs: {
prefix: `svg-${componentName.toLowerCase()}-`,
},
},
{
removeViewBox: false,
}
],
},
Any idea if there's a way to do that in a
requirestatement, i.e. with no special Webpack configuration?
Keeping the viewBox attribute really should be the default..
@vlinder you should submit a PR to SVGO.
The SVGO option in .svgrrc didn't work for me.
However, SVGR CLI has an option to preserve the viewBox attribute by passing in --icon flag.
Example
npx @svgr/cli --icon -d src/icons svg-icons --ext tsx
Thanks, that got me on the right lines. That didn't work for me (I may have made a typo) but I saw in the options I can do a YAML file instead. I ended up writing a file
.svgo.ymlwith the following contents:plugins: - removeViewBox: false
Saved my day. .svgrrc didn't work for me since in my case the issue was in the rollup bundle plugin. Adding a .svgorc config fixed it.
if use rollup.config, you can create .svgarc file
inside indicate:
plugins: {
removeViewBox: false
}
Most helpful comment
okay, if anyone runs into the same thing. i've managed to make it work with webpack config like so:
js { test: /(icons|images)\/.*?.svg$/, use: [{ loader: '@svgr/webpack', options: { svgoConfig: { plugins: { removeViewBox: false } } } }, 'file-loader'] }