-
-
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
docs: correct the resourceQuery comment in code #650
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/gregberge/svgr/2a6AxiPHCLzkio44vgC1CULAWc1x |
Thanks! |
Also i want to report that the new config of version 6 is still not working correcly for me. So I am still using this code to make things work correctly: module: {
rules: [
{
test: /\.(eot|woff|woff2|ttf)$/i,
type: 'asset/resource'
},
{
test: /\.(png|jpe?g|gif)$/i,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: wmConfig.webpack.inlineAssetMaxSize
}
}
},
/** svg
* https://react-svgr.com/docs/webpack/#handle-svg-in-css-sass-or-less
* https://github.com/gregberge/svgr/issues/551#issuecomment-883073902
* https://webpack.js.org/configuration/module/#ruleoneof
*/
{
test: /\.svg$/,
oneOf: [
{
issuer: /\.[jt]sx?$/,
resourceQuery: /react/, // *.svg?react
use: ['@svgr/webpack']
},
{
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: wmConfig.webpack.inlineAssetMaxSize
}
}
},
],
},
/** */
{
test: /\.css$/,
use: [{ loader: 'css-loader' }]
},
{
test: /\.txt$/,
type: 'asset/source',
},
{
test: /\.s[ac]ss$/i,
use: [{ loader: 'css-loader' }, { loader: 'sass-loader' }]
},
],
}, |
refer to example - react.svg is 5.45 KB you can also change the value of inline asset size in const wmConfig = {
...
webpack: {
...
inlineAssetMaxSize: 6 * 1024, // in Bytes <--- here import "./index.css";
import React from 'react';
import ReactDOM from 'react-dom';
import svg from './react.svg';
import Svg from './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-svg-sample {
background-image: url(./react.svg);
background-size: contain;
width: 200px;
height: 200px;
} when inlined ( inlineAssetMaxSize: 6 * 1024,)here both css data url and img src data url is same. when not inlined ( inlineAssetMaxSize: 0 * 1024,)here both css url and img src url is same. |
I am able to reproduce the same output as above by using config provided in docs only when I add rules: [
// rule 1
{
test: /\.svg$/i,
type: 'asset',
resourceQuery: /url/, // *.svg?url
},
// rule 2
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
use: ['@svgr/webpack'],
resourceQuery: ""
},
], import "./index.css";
import React from 'react';
import ReactDOM from 'react-dom';
import svg from './react.svg?url';
import Svg from './react.svg';
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-svg-sample {
background-image: url(./react.svg?url);
background-size: contain;
width: 200px;
height: 200px;
} |
it seems like both the rules are working for: import svg from './react.svg?url'; |
Also note regarding the following config: react.svg is 5.45 KB {
test: /\.svg$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 6*1024 // 6KB
}
}
}, import svg from './react.svg?bar';
function App() {
return (
<div>
<img src={svg} width="200" height="200" />
<div id="css-svg-sample"></div>
</div>
)
} #css-svg-sample {
background-image: url(./react.svg?foo);
background-size: contain;
width: 200px;
height: 200px;
} Webpack output same data url for both One thing need to be corrected. In the following (from the docs): rules: [
// rule 1
{
type: 'asset',
resourceQuery: /url/, // *.svg?url
},
// rule 2
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
use: ['@svgr/webpack'],
},
], rule 1 should be defined as follows: rules: [
// rule 1
{
test: /\.svg$/i, // <-- webpack should also be provided with specific path to test for (here path containing .svg)
type: 'asset',
resourceQuery: /url/, // *.svg?url
},
// rule 2
], So i conclude that rule 2 (defined for svgr) is also working for path .svg?url |
Summary
The doc page of webpack has incorrect resourceQuery comment in code section.
Changed it from
// *.svg?react
to// *.svg?url
Test plan