Override SVG Loader in custom Webpack
See original GitHub issueNot 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:
- Created 4 years ago
- Reactions:13
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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:
It is actually working in conjunction with
vue-svg-loader
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!