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 loader returns a string instead of a symbol object when loading from storybook

See original GitHub issue

versions:

    "svg-sprite-loader": "^3.7.3",
    "svgo": "^1.0.5",
    "svgo-loader": "^2.1.0",
    "ts-loader": "^4.1.0",
    "typescript": "^2.8.1",
    "webpack": "^4.3.0",
    "webpack-cli": "^2.0.13"

webpack configuration

{
    test: /\.svg$/,
    use: [
      {
        loader: 'svg-sprite-loader',
        options: {
          // extract: true,
          spriteFilename: "icon.svg"
        }
      }, {
        loader: 'svgo-loader',
        options: {
          plugins: [
            { removeTitle: true },
            { convertColors: { shorthex: false } },
            { convertPathData: false }
          ]
        }
      }]
  }

When importing a sprite:

import * as education from "../src/assets/svg/education.svg";

instead of getting a symbol object as depicted here, I’m getting a url in the form of:

/static/media/education.3a3782a8.svg

I can’t seem to find the reason causing it. Thanks in advance.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
dsklinecommented, Jun 7, 2021

Probably due to a recent update, one of the webpack rules in the default storybook config has a rule.test that returns undefined now. I made a small tweak to the if statement:

  config.module.rules = config.module.rules.map((rule) => {
    if (rule.test && rule.test.toString().includes("svg")) {
      const test = rule.test.toString().replace("svg|", "").replace(/\//g, "");
      return { ...rule, test: new RegExp(test) };
    } else {
      return rule;
    }
  });

  config.module.rules.push({
    test: /\.svg$/,
    loader: require.resolve("svg-sprite-loader"),
  });
3reactions
Pet3riscommented, Jan 28, 2021

This is the full implementation if someone is interested:

  config.module.rules = config.module.rules.map(rule => {
    if (rule.test.toString().includes('svg')) {
      const test = rule.test.toString().replace('svg|', '').replace(/\//g, '')
      return { ...rule, test: new RegExp(test) }
    } else {
      return rule
    }
  });

  config.module.rules.push(
    {  
      test: /\.svg$/i,
      loader: require.resolve('svg-sprite-loader')
    }
  );
Read more comments on GitHub >

github_iconTop Results From Across the Web

SVG loader returns a string instead of a symbol object when ...
my workaround was to override the default rule set by applying my loaders directly onto their webpack override module, located under .storybook/ ...
Read more >
React Storybook SVG Failed to execute 'createElement' on ...
This is the culprit, you need to make sure that the file load is excluding all inline SVG file otherwise it will error....
Read more >
Import SVG Components in Storybook - Duncan Leung
Storybook Not Importing SVGs I was running into an issue where SVG imports in Storybook were instead of being imported as React components…...
Read more >
svg-loader: A Different Way to Work With External SVG
Load external SVGs as inline elements. To address this, I have created a library called svg-loader. Simply put, it fetches the SVG files...
Read more >
module build failed: typeerror [err_invalid_arg_type] - You.com
Module build failed: TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received type undefined at D:\css-loader!
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