-
-
Notifications
You must be signed in to change notification settings - Fork 425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using svgr with webpack 5 #551
Comments
Hey @vyachsed 👋, |
UPDATE: My problem is actually webpack config related, not SVGR's nor NextJS'. I'm also having problems with SVGR and Webpack5. I'm using NextJS and when enabling Webpack5, I get the following error:
When removing the I tried changin
to
which resulted in no compile errors but SVGR wasn't working. |
@feRpicoral I believe you can use just |
I got it working but it's showing this: <w> [webpack.cache.PackFileCacheStrategy/webpack.FileSystemInfo] Resolving '@babel/core/lib/config/validation/option-assertions' in /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/config/validation for build dependencies doesn't lead to expected result '/Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/config/validation/option-assertions.js', but to '/Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/node_modules/@babel/core/lib/config/validation/option-assertions.js' instead. Resolving dependencies are ignored for this path.
<w> at unknown 4 @babel/core/lib/config/validation/option-assertions
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/config/validation/options.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/config/validation/options.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/config/partial.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/config/partial.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/config/index.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/config/index.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/transform-file.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/transform-file.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/index.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/node_modules/@babel/core/lib/index.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/lib/index.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/@babel/plugin-proposal-object-rest-spread/lib/index.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-build/node_modules/@babel/preset-env/lib/available-plugins.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-build/node_modules/@babel/preset-env/lib/available-plugins.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-build/node_modules/@babel/preset-env/lib/index.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-build/node_modules/@babel/preset-env/lib/index.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-build/build/lib/bundle.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-build/build/lib/bundle.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-webpack-plugin/build/generate-sw.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-webpack-plugin/build/generate-sw.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-webpack-plugin/build/index.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/workbox-webpack-plugin/build/index.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/node_modules/next-pwa/index.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/node_modules/next-pwa/index.js
<w> at file dependencies /Users/jahir/dev/jahir/synaptiko/fe/next.config.js
<w> at file /Users/jahir/dev/jahir/synaptiko/fe/next.config.js
<w> at resolve commonjs /Users/jahir/dev/jahir/synaptiko/fe/next.config.js |
I took a peek at how docusaurus does this in their config and I made it compile w/o errors using the following config:
Hope it helps 🙏🏼 |
@JonasBa for me the viewbox attribute is still stripped from my svgs despite Edit: The option works, it was my fault as I didn't have proper camelCase naming for the |
If you are trying to handle both css/scss also need svgr component, import using file-loader(Webpack 4) aka AssetModule(assets/resource in Weback5) try something like this
Then you import like this js/jsx
|
@JonasBa you're a life savior, thank you for sharing this 🙏🏻🙏🏻🙏🏻 |
I made a small loader {
test: /\.svg$/,
- use: ['@svgr/webpack', 'url-loader'],
+ oneOf: [
+ {
+ dependency: { not: ['url'] },
+ use: ['@svgr/webpack', 'new-url-loader'],
+ },
+ {
+ type: 'asset', // Use 'asset/resource' for file-loader
+ },
+ ],
} Now you can continue to use both the URL and React component: import starUrl, { ReactComponent as Star } from './star.svg'; See README for more info. I also created a minimal example. |
I tried the solution mentioned by @JonasBa but I'm still getting
|
@njerschow fixed with this vercel/next.js#26130 (comment) |
Anyone still looking for a fix, its not that file-loader still can't be used along with webpack5, it just needs some additional modification. Try the following out. {
test: /\.svg$/,
use: [
{
loader: "@svgr/webpack"
},
{
loader: "file-loader"
}
],
type: "javascript/auto",
issuer: {
and: [/\.(ts|tsx|js|jsx|md|mdx)$/]
}
}, |
@privateOmega thank you, this is what worked for me. |
This posted solution worked for me, sharing here to maybe save someone some time: https://stackoverflow.com/questions/61498644/storybook-failed-to-execute-createelement-on-svg-files-using-svgr-webpack |
Note that I'm unclear whether {
test: /\.svg$/,
oneOf: [
{
issuer: /\.s?css$/,
type: 'asset/resource',
},
{
use: [
{
loader: '@svgr/webpack',
options: {
svgoConfig: {
plugins: [{removeViewBox: false}],
},
},
},
{
loader: 'file-loader',
},
],
type: 'javascript/auto',
},
],
}, |
tks 👍👍👍 |
This works, and I think the documentation on issuer should be updated, otherwise it's really confusing. |
I was struggling to load a sprite SVG using this syntax: import { ReactComponent as Sprite } from './sprite.svg'
function App() {
return (
<>
<Sprite
style={{
position: 'absolute',
width: 0,
height: 0,
overflow: 'hidden'
}}
/>
<div className="App">
... Nothing from this thread worked. What did the trick was this config: {
test: /\.svg$/,
use: [
{
loader: '@svgr/webpack',
options: {
prettier: false,
svgo: false,
svgoConfig: {
plugins: [{ removeViewBox: false }]
},
titleProp: true,
ref: true
}
},
{
loader: 'file-loader',
options: {
name: 'static/media/[name].[hash].[ext]'
}
}
],
issuer: {
and: [/\.(ts|tsx|js|jsx|md|mdx)$/]
}
} Got this config from React script's webpack config |
OK guys, I think we have a webpack bug here. Isn't it? Anything SVGR can do as a workaround? |
@levymetal thanks. I have modified your answer. Lets say we have a Webpack config: {
test: /\.svg$/,
oneOf: [
{
issuer: /\.[jt]sx?$/,
resourceQuery: /react/, // *.svg?react
use: ['@svgr/webpack']
},
{
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 200
}
}
},
],
}, In your react component: import "@styles/index.css";
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import svg from '@assets/react.svg';
import Svg from '@assets/react.svg?react';
function App() {
return (
<div>
<img src={svg} width="200" height="200" />
<div id="css-svg-sample"></div>
<Svg width="200" height="200" viewBox="0 0 3500 3500"/>
</div>
)
}
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
); css file: #css-svg-sample {
background-image: url(@assets/react.svg);
background-size: contain;
width: 200px;
height: 200px;
} Since our Here is the demo: https://github.com/nikhilnayyar002/react-min References: No need to add file-loader & url-loader! |
If you use |
@privateOmega fixed with this gregberge/svgr/issues/551 (comment) oh thank @privateOmega so much I struggled all day yesterday, but I solved it with the code you gave me. thank you!. |
Part of new documentation. |
Somebody knows workaround for typescript?
|
Sorry, it was code editor typings resolution problems. declare module '*.svg?svgr' {
import type { ComponentType, SVGProps } from 'react';
type SvgComponent = ComponentType<SVGProps>;
const Svg: SvgComponent;
export = Svg;
} |
Fixing Typescript "Cannot find module" for svg assets: [svgr d.ts](gregberge/svgr#551 (comment))
* fix: remove lineHeight whitespace * feat: mobile about * feat: tablet about Fixing Typescript "Cannot find module" for svg assets: [svgr d.ts](gregberge/svgr#551 (comment)) * feat: desktop about
Hi! I am using two kinds of imports in my application, like: import starUrl from './star.svg' and import { ReactComponent as Star } from './star.svg'
This works well if using file-loader. But in webpack 5 the file-loader, raw-loader, raw-loader is deprecated. Now we need to use asset-modules (https://webpack.js.org/guides/asset-modules/). I tried to use all kinds, but nothing worked for me.
The text was updated successfully, but these errors were encountered: