question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SVG imports are undefined

See original GitHub issue

Describe the bug When I import SVGs into storybook, the imports are undefined.

To Reproduce

import React from 'react'
import { storiesOf } from '@storybook/react'
import { ReactComponent as Example } from './example.svg'

console.log(Example) // undefined

Expected behavior The SVG should be imported successfully.

Code snippets If applicable, add code samples to help explain your problem.

System:

  • OS: OSX
  • Device: Macbook Pro 2018
  • Browser: N/A
  • Framework: N/A
  • Addons: Not relevant
  • Version: "@storybook/react": "^4.1.11"

Additional context Judging by this issue, this should be supported out of the box without a custom Webpack or Babel config.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
pedrombafonsocommented, Apr 30, 2019

For those interested, I was able to get .svg imports working using the following custom webpack config

module.exports = ({ config, mode }) => {

  config.module.rules = config.module.rules.map(rule => {
    if (!rule.test.test(".svg")) {
      return rule;
    }

    const newRule = rule;
    // Changes existing default rule to not handle SVG files
    newRule.test = /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2)(\?.*)?$/;
    return newRule;
  });

  // Adds new SVG loader
  config.module.rules.push({
    test: /\.svg$/,
    use: ["@svgr/webpack", "url-loader"]
  });

  return config;
};

for reference: https://storybook.js.org/docs/configurations/custom-webpack-config/

2reactions
DimaRGBcommented, Nov 27, 2019

hey @pedrombafonso, thank you for the solution.

but let’s try to make it more adaptable:

module.exports = ({ config, mode }) => {
  const assetRule = config.module.rules.find(({ test }) => test.test('.svg'));
  const assetLoader = {
    loader: assetRule.loader,
    options: assetRule.options || assetRule.query,
  };
  config.module.rules.unshift({
    test: /\.svg$/,
    use: ['@svgr/webpack', assetLoader],
  });
  config.module.rules = [{ oneOf: config.module.rules }];

  return config;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting undefined when importing svg as ReactComponent in ...
To import an SVG: import MySvg from "MySvg.svg" which will save the path as a string inside MySvg . To render it, use...
Read more >
Importing SVG into Meteor/React - help
Hey guys, since it is “trendy” to import SVGs directly into your code, I'm wondering if anyone from you has some instructions for...
Read more >
Options - SVGR
"classic": adds import * as React from 'react' on the top of file ... SVG nodes with uppercase and use a specific template...
Read more >
Why do I keep getting this error: "Element type is invalid" when ...
import { ReactComponent as MyIcon } from "../../assets/icons/12x12/arrow-down-12.svg" and when I render MyIcon , I keep getting this error: ...
Read more >
SVG Error? - Pixelmator Community
Andrew Hart. on undefined undefined, Sun. Hi. I'm trying to edit an SVG file, but when I import it to Pixelmator Pro one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found