Closed nicgordon closed 7 years ago
Can confirm; having the same problem.
Same here. Don't know enough about webpack yet to suggest a proper solution, but one possible workaround is to adopt a specific naming convention for .svgs used in React components, e.g. .inline.svg
, and adjust your test:
regex-es correspondingly.
Besides naming conventions, you can use the loader configuration's include
, and exclude
properties to define regular expressions to only include, or exclude, certain paths/files.
For example my current company ran across an issue when using an SVG font. Here was the solution:
{
test: /\.svg$/,
exclude: [ /node_modules/, /fonts?/ ],
loader: 'svg-react'
},
{
test: /\.woff|\.woff2|\.eot|\.ttf|\.svg/,
include: /\/fonts?\//,
loader: 'url-loader?limit=100000&name=[name]-[sha1:hash:hex:7].[ext]'
}
Note: We are using
svg-react-loader@next
which does not require ababel-loader
Both cases doesn't resolve the problem. If i want use same svg file for styles and for rendering as react component, i must duplicate it in another folder or with another name.
This problem can be quite easily resolved with Webpack Rule.issuer and (optionally) Rule.oneOf for more precise configuration. No images duplication is necessary.
Example config:
{
test: /\.(png|jpg|gif|svg)$/,
// As opposed to Webpack loaders order (from last to first),
// only the first matching Rule will be used here
oneOf: [
{
test: /\.svg$/,
// Use this loader for SVG-into-JavaScript imports only
issuer: /\.(js|jsx)$/,
loader: 'svg-react-loader'
},
{
// Use file-loader for all other kinds of images
loader: 'file-loader'
}
]
}
Hello, I have a tricky situation: we are requiring SVG files and would like them to be converted to react components which this library is working for, but then we also use some styles from a third party library which requires an SVG file, and when that conversion is taking place this loader injects JS into our compiled CSS file.
Our loaders config looks like this:
The result looks something like this:
which obviously isn't valid CSS 😩
Is there a way of differentiating between SVGs that are loaded via
require()
and ones that are loaded viaurl()
inside CSS? If there is then I might be able to set a separate loader for those SVGs. Thank you in advance.