Skip to content

Commit 3a49417

Browse files
authored
fix(svgr): ?url query should work in bundle mode (#804)
1 parent 035f855 commit 3a49417

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

packages/core/src/asset/assetConfig.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,25 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
117117
});
118118
config.plugin(LibSvgrPatchPlugin.name).use(LibSvgrPatchPlugin, []);
119119
}
120-
// 2. in bundleless, do not support queryImport
121-
// remove issuer to make every svg asset is transformed by svgr-loader
122-
if (!bundle) {
123-
if (isUsingSvgr) {
120+
121+
if (isUsingSvgr) {
122+
// 2. in bundle, ?url should also be set importMode to preserve
123+
if (bundle) {
124+
const rule = config.module
125+
.rule(CHAIN_ID.RULE.SVG)
126+
.oneOf(CHAIN_ID.ONE_OF.SVG_URL);
127+
const originalGeneratorOptions = rule.get('generator');
128+
129+
const generatorOptions = isUserSetPublicPath
130+
? originalGeneratorOptions
131+
: {
132+
...originalGeneratorOptions,
133+
importMode: 'preserve',
134+
};
135+
rule.generator(generatorOptions);
136+
} else {
137+
// 3. in bundleless, do not support queryImport
138+
// remove issuer to make every svg asset is transformed by svgr-loader
124139
const rule = config.module
125140
.rule(CHAIN_ID.RULE.SVG)
126141
.oneOf(CHAIN_ID.ONE_OF.SVG);

tests/integration/asset/__snapshots__/index.test.ts.snap

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_r
288288
})
289289
});
290290
const logoreact = SvgLogo;
291+
import logourl_namespaceObject from "./static/svg/logo.svg";
291292
console.log('queryImport', 'ReactComponent', logoreact);
293+
console.log('queryImport', 'Url', logourl_namespaceObject);
292294
const logo_SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("svg", {
293295
xmlns: "http://www.w3.org/2000/svg",
294296
viewBox: "0 0 841.9 595.3",
@@ -312,7 +314,7 @@ const logo_SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_
312314
});
313315
const logo = __rslib_svgr_url__0__;
314316
console.log('namedImport', 'ReactComponent', logo_SvgLogo);
315-
console.log('default Import', 'reexport', logo);
317+
console.log('defaultImport', 'Url', logo);
316318
"
317319
`;
318320

@@ -343,7 +345,9 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("svg
343345
})
344346
});
345347
const logoreact = SvgLogo;
348+
const logourl_namespaceObject = require("./static/svg/logo.svg");
346349
console.log('queryImport', 'ReactComponent', logoreact);
350+
console.log('queryImport', 'Url', logourl_namespaceObject);
347351
const logo_SvgLogo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("svg", {
348352
xmlns: "http://www.w3.org/2000/svg",
349353
viewBox: "0 0 841.9 595.3",
@@ -367,7 +371,7 @@ const logo_SvgLogo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)
367371
});
368372
const logo = require("./static/svg/logo.svg");
369373
console.log('namedImport', 'ReactComponent', logo_SvgLogo);
370-
console.log('default Import', 'reexport', logo);
374+
console.log('defaultImport', 'Url', logo);
371375
var __webpack_export_target__ = exports;
372376
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
373377
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import svgUrl from './assets/logo.svg';
1+
import Url from './assets/logo.svg';
22

3-
console.log('default Import', 'reexport', svgUrl);
3+
console.log('defaultImport', 'Url', Url);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import ReactComponent from './assets/logo.svg?react';
2+
import Url from './assets/logo.svg?url';
23

34
console.log('queryImport', 'ReactComponent', ReactComponent);
5+
console.log('queryImport', 'Url', Url);

0 commit comments

Comments
 (0)