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.

Override SVG Loader in custom Webpack

See original GitHub issue

Not sure whether this is explained somewhere, but I had to override the SVG loader used by Storybook to integrate SVG. In case someone stumbles upon this issue, here is what I have done in my custom Webpack config:

module.exports = ({ config }) => {
  // Don't use Storybook's default SVG Configuration
  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: /\.(js|jsx)$/,
      exclude: /node_modules/,
      use: ['babel-loader'],
    },
    {
      test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
      use: ['file-loader'],
    },
    {
      test: /stories\.(js|jsx)?$/,
      loaders: [require.resolve('@storybook/addon-storysource/loader')],
      enforce: 'pre',
    },
    // Use SVG Configuration for SVGR yourself
    {
      test: /\.svg$/,
      use: ['@svgr/webpack'],
    },
  );

  return config;
};

In case there is another approach to this, please leave a comment. Anyway, just thought this may help someone who runs into the same thing.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

25reactions
hazzocommented, May 24, 2019

Thanks for the advice but I was having still problems because the generated regex was a string and with an extra \ and it could not load some fonts.

Here is my approach:

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
    }
  })

It is actually working in conjunction with vue-svg-loader

0reactions
stale[bot]commented, Sep 2, 2019

Hey there, it’s me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Override SVG Loader in custom Webpack · Issue #6758 - GitHub
In case someone stumbles upon this issue, here is what I have done in my custom Webpack config: module.exports = ({ config })...
Read more >
svg-inline-loader - webpack - JS.ORG
This Webpack loader inlines SVG as module. If you use Adobe suite or Sketch to export SVGs, you will get auto-generated, unneeded crusts....
Read more >
Webpack - SVGR
SVGR provides an official webpack.js loader to import SVG as React components. ... The named export defaults to ReactComponent and can be customized...
Read more >
How to Load SVG with React and Webpack | Pluralsight
To transform an SVG image into a Data URL, we will need an appropriate webpack loader in our bundler. The most common webpack...
Read more >
Override webpack loader config for a specific file
The goal is to keep original webpack configuration and only override the rule for the jsoneditor-icons.svg file.
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