{
test: /\.svg/,
use: [
{
loader: 'url-loader'
},
{
loader: 'svg-react-loader'
}
]
}
This is module: { rules: [
section regarding SVG loaders in my webpack.config.js
Anyone used similar setup and had issues with importing svg files in React?
I need both to load svg in CSS and to import them in React. If I use
- url-loader alone CSS works.
- If I use svg-react-loader alone imports in React works.
But together imports in React fails with the following error:
Module build failed: Error: Non-whitespace before first tag.
Here is how I import the SVG in React cmp:
import StarIcon from 'svg-react-loader!mdi-svg/svg/star.svg';
{
test: /\.svg/,
use: [
{
loader: 'url-loader'
},
{
loader: 'svg-react-loader'
}
]
}
This is module: { rules: [
section regarding SVG loaders in my webpack.config.js
Anyone used similar setup and had issues with importing svg files in React?
I need both to load svg in CSS and to import them in React. If I use
- url-loader alone CSS works.
- If I use svg-react-loader alone imports in React works.
But together imports in React fails with the following error:
Module build failed: Error: Non-whitespace before first tag.
Here is how I import the SVG in React cmp:
import StarIcon from 'svg-react-loader!mdi-svg/svg/star.svg';
Share
Improve this question
asked Jan 4, 2018 at 21:28
DevelogerDeveloger
3,9982 gold badges25 silver badges38 bronze badges
2 Answers
Reset to default 5Apparently the cleanest way to handle loaders without a need to reference the loader in the actual JS / CSS code during the imports is to use oneOf with exclude property:
{
test: /\.svg$/,
oneOf: [
{
exclude: path.resolve(__dirname, '../src/'),
use: 'svg-react-loader'
},
{
exclude: path.resolve(__dirname, '../node_modules/'),
use: 'url-loader'
},
],
}
A better way is to use oneOf
, but with include
option, which is logically more correct, than exclude
option:
{
test: /\.svg$/,
oneOf: [
{
include: path.resolve(__dirname, '../node_modules/'),
use: 'svg-react-loader'
},
{
include: path.resolve(__dirname, '../src/'),
use: 'url-loader'
},
],
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745557096a4632887.html
评论列表(0条)