Storybook: Override SVG Loader in custom Webpack

Created on 10 May 2019  路  8Comments  路  Source: storybookjs/storybook

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.

babel / webpack good first issue has workaround inactive

Most helpful comment

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

All 8 comments

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

Thanks for sharing! Updated my comment above.

One thing I forgot to mention: I had the same problem in Gatsby.js. However, there it is neatly solved with their plugin approach. There exists one plugin which just deals with the SVGR integration. Maybe it would be great to have such approach for Storybook as well. CC @shilman

@rwieruch we just started to release presets for this kind of thing.

here's the first one: https://www.npmjs.com/package/@storybook/preset-typescript

and here's the docs: https://github.com/storybooks/storybook/pull/5333

Any interest in writing a preset-svgr?

Any interest: yes. But lack of time :( I will ask around in my React community! I guess it's a good issue for developers new to open source.

Automention: Hey @igor-dv @ndelangen, you've been tagged! Can you give a hand here?

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!

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!

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dnlsandiego picture dnlsandiego  路  3Comments

tlrobinson picture tlrobinson  路  3Comments

shilman picture shilman  路  3Comments

sakulstra picture sakulstra  路  3Comments

shilman picture shilman  路  3Comments