storybook: Unable to run Storybook when importing svgs - Storybook v5
Describe the bug Unable to run Storybook when importing svgs into a component. (TS/Gatsby project).
To Reproduce
git clone git@github.com:elie222/elie-tech.git
cd elie-tech
git checkout storybook
yarn storybook
Expected behavior Storybook should run.
Screenshots
Code snippets
System:
- OS: MacOS
- Device: Macbook Pro 2015
- Browser: chrome
- Framework: react
- Addons: -
- Version: 5.x
Additional context
I’m using Gatsby and TypeScript. The Gatsby site imports the svgs and runs fine. It uses the gatsby-plugin-svgr Gatsby plugin.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 13
- Comments: 37 (4 by maintainers)
With storybook 6
main.js
@elie222 looks like your svg rule is wrong for this use case, have a look at https://github.com/smooth-code/svgr/tree/master/packages/webpack#using-with-url-loader-or-file-loader.
My
webpack.config.jscontains this which works like a charm:Thank you @snc , it is a nice solution but somehow it didn’t work for me. It was really helpful to solve mine! I am using this example in version 5.2.8
In storybook^6.0.0 beta one of the rules had an array as the test field for me like this:
[ /\.stories\.(jsx?$|tsx?$)/ ], so the code above fails. This is my.storybook/webpack.config.jswhich disregards arrays:So ^ the above solution works … however if Storybook uses SVGs in their internal rendering some day, it would break. A slightly safer solution would be something like :
If you so choose, you could have a more elaborate rule for evaluating which rule is the
svgRulehere.Hmm… Well the linked project runs, but stories for components that make use of svgs no longer work:
An example of a problematic line:
Continuing to play with this to find a solution.
it works for me
@LuisOsta by default it imports an SVG as a file path, just like any other static asset. turning it into a react component is a choice for your specific project. we should make it easier to override this with a storybook addon that does this configuration on your behalf.
The
babel-plugin-inline-react-svgsolution ~worked, in that I could render the SVGs, but I couldn’t manage to customize the optimization options, which were removing viewBox and breaking responsivity.Luckily the solution @csaden provided did the trick (thanks!).
Here’s my implementation of it in case it’s helpful
You can try storybook-preset-inline-svg. It uses
svg-inline-loaderand you can specify inlining all SVG or only some.For example
Hi everyone! Seems like there hasn’t been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don’t have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!
@elie222 I ran into problems here too. We have custom webpack config that includes svgr-loader, and are merging our config with Storybook’s webpack config.
I believe the issue is the Preview config of file-loader here: https://github.com/storybooks/storybook/blob/4da246bbe9413510b48d1ab8b9faf4fae4656d92/lib/core/src/server/preview/base-webpack.config.js#L36-L40
In our case, we needed to have our svgr-loader rule occur before that file-loader rule. You might be able to
unshiftyour loader rule here so that it takes precedence, rather thanpushing it.Thanks, @snc for a solution that pointed me in the right direction. I only had to teak it a little bit by adding
pdfto the test string.@storybook 6.3.7For me using
babel-plugin-inline-react-svgin my.storybook/.babelrcdoes the trick.if you have customer svg loaders and aliases.
`
`
svg-url-loader.jsmodule.exports = function () { const path = JSON.stringify(this.resourcePath); return "export default new URL(${path}, import.meta.url).toString();" };Yes. I was. If anyone is running into these issues check the linked repo for a solution. Thanks for help!
What worked out for me is from this post, basically:
I’m using 6.2.9 none of them didn’t work for me. Is there any other solution for this?
Thanks @bkiac, that worked almost perfectly for my usecase. I’m importing .svg files into my components as such:
import ClockIcon from '@icons/clock.svg';sourl-loaderwould transform it into base64, which would eventually fail to render. Keeping just@svgr/webpackin theusearray fixed it.@hnryjms Where would I put this configuration? I haven’t touched the default config of storybook yet, so I’m not sure where this would go.
i was running into this problem in a monorepo setup and @snc’s solution mostly fixed it, but i also had to set
removeViewBox: falseto prevent the viewBox attribute from getting removed.posting in case anyone has the same issue and happens upon this thread
Added the above to webpack.config.js and it worked for me. Importing the SVG as below:
@jackmccloy I had similar problem in a monorepo, but solved by using
removeViewBox: falseoption. The other possible cause is that I was using a material design icon. Anyway, that code helped a lot.my version
This is how i resolved it. `
@hnryjms FYI the “manager” webpack config (that renders Storybook’s UI) and “preview” config (that renders user’s components) are completely separate, so you should be able to do whatever you want that’s suitable to your codebase without having to worry about Storybook. 🤞