Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve Webpack fonts and svg image loader Regex conflict?

Tags:

webpack

Here is my regex for fonts and svg image loader and they are in conflict, since they both target *.svg file. How to solve it?

 {test: /\.(eot|svg|ttf|woff|woff2)$/, loader: 'file?name=/Presentation/_dist/fonts/Interstate/[name].[ext]'},
 {test:/\.(png|jpg|gif|svg)$/, loader: 'url?limit=10000&name=/Presentation/_dist/images/[name].[ext]'}
like image 478
Nicolas S.Xu Avatar asked Oct 21 '16 18:10

Nicolas S.Xu


People also ask

How do I load SVG files in a custom Webpack config?

A custom webpack config for Storybook needs to be set up to specify @svgr/webpack as a webpack loader for .svg assets. The @svgr/webpack loader rule needs to occur before that other loaders like the file-loader rule so that @svgr/webpack takes precedence.

How to place the @svgr/Webpack loader before other loaders?

The @svgr/webpack loader rule needs to occur before that other loaders like the file-loader rule so that @svgr/webpack takes precedence. Place the @svgr/webpack loader before existing webpack asset loaders using Array.prototype.unshift ().

How to avoid font errors with Webpack?

To summarize, to avoid any font related errors with webpack, you need to download file-loader, integrate it within your webpack config, and import whatever fonts you wish to use into your CSS or SCSS files.

What is Webpack File Loader in Webpack?

webpack file loader is a loader used mainly for supporting images such as SVG and PNG, and fonts in your webpack project. There are different configurations for webpack 4 and webpack 5.


1 Answers

You can use exclude and include to eliminate the problem. Something like:

{
    test: /\.(eot|svg|ttf|woff|woff2)$/,
    include: [
        path.resolve(__dirname, "MY-FONTS-FOLDER")
        //, Any other svg font path
    ],
    loader: 'file?name=/Presentation/_dist/fonts/Interstate/[name].[ext]'
},
{
    test:/\.(png|jpg|gif|svg)$/,
    exclude: [
        path.resolve(__dirname, "MY-FONTS-FOLDER")
        //, Any other svg font path
    ],
    loader: 'url?limit=10000&name=/Presentation/_dist/images/[name].[ext]'
}

Note:

  • Replace MY-FONTS-FOLDER with the actual fonts folder path.
  • You might also use regex to match font.svg for the file-loader and .svg BUT NOT font.svg(maybe negative lookahead) for url-loader.
like image 117
Karthik Avatar answered Sep 28 '22 01:09

Karthik