admin管理员组文章数量:1431037
{
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'
},
],
}
本文标签: javascriptWebpackReactmultiple SVG loaders issueStack Overflow
版权声明:本文标题:javascript - Webpack + React + multiple SVG loaders issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745557096a2663253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论