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 5 years ago
- Reactions: 12
- 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
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!
You can try storybook-preset-inline-svg. It uses
svg-inline-loaderand you can specify inlining all SVG or only some.For example
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.@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.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?
Added the above to webpack.config.js and it worked for me. Importing the SVG as below:
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
@storybook@6.2.9webpack@5.39.1I was able to get svgs working by removing Storybook’s svg loader and using the one suggested in Webpack5 docs.
{ test: /\.(svg)$/, type: "asset/inline", }That’s precisely what happens with Storybook
^6.0.28and@svgr/webpack. Your solution fixed my build, thanks a lot!@tbelch-at-eHealth-Tec Storybook supports changing its default webpack config: https://storybook.js.org/docs/configurations/custom-webpack-config/
@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
@OmarZeidan that PR was closed almost a year ago, not merged. I left it around for documentation’s sake.
Thanks for sharing this! ☺️
However, the new version of
Storybookhas addedpdfto thetest, See here, just incase anyone is wondering why yours is not working as expected 😃I am also expecting a fix for this coming soon, currently is in the
nextversion of Storybook. Check out this PR 💃🏻💃🏻💃🏻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. 🤞
@hnryjms 's method works fine for me. I used the SVG Inline Loader to load every kind of svg (svg files, svg in html, inline svg via URI).
Thank you! That fixed it.
Were you able to get this working @elie222 ?