Skip to content

Commit f033669

Browse files
authored
feat(asset): support svgr exportType usage (#705)
1 parent 4743d59 commit f033669

File tree

5 files changed

+312
-69
lines changed

5 files changed

+312
-69
lines changed

packages/core/src/asset/assetConfig.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ const RSBUILD_SVGR_PLUGIN_NAME = 'rsbuild:svgr';
1313
/**
1414
* Be compatible to css-extract importModule and experimentalLibPreserveExports
1515
* when set experimentalLibPreserveExports to true, the css-loader result can not executed in node side, so clone the assets rule
16-
* 1. js assets: original rule set issuer and experimentalLibPreserveExports: true
17-
* 2. css assets: a copy of original rule
16+
* 1. js assets: original rule set issuer and experimentalLibPreserveExports: true, `import url from './foo.svg'`
17+
* 2. css assets: a copy of original rule, `url('./foo.svg')`
1818
*/
1919
const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
2020
name: PLUGIN_NAME,
2121
pre: [RSBUILD_SVGR_PLUGIN_NAME],
2222
setup(api) {
2323
api.modifyBundlerChain((config, { CHAIN_ID }) => {
24+
// for separating css assets and js assets
2425
// 1. modify svg rule first, svg is special because of svgr
2526
const svgAssetRule = config.module.rules
2627
.get(CHAIN_ID.RULE.SVG)
@@ -92,14 +93,17 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
9293
}
9394

9495
// for svgr
96+
const isUsingSvgr: boolean = config.module
97+
.rule(CHAIN_ID.RULE.SVG)
98+
.oneOf(CHAIN_ID.RULE.SVG)
99+
.uses.has(CHAIN_ID.USE.SVGR);
100+
const isUsingSvgrUrlLoader = config.module
101+
.rule(CHAIN_ID.RULE.SVG)
102+
.oneOf(CHAIN_ID.ONE_OF.SVG)
103+
.uses.has(CHAIN_ID.USE.URL);
104+
95105
// 1. remove __webpack_require__.p in svgr url-loader and file-loader
96-
const isUsingSvgr = Boolean(
97-
config.module
98-
.rule(CHAIN_ID.RULE.SVG)
99-
.oneOf(CHAIN_ID.RULE.SVG)
100-
.uses.has(CHAIN_ID.USE.SVGR),
101-
);
102-
if (isUsingSvgr) {
106+
if (isUsingSvgr && isUsingSvgrUrlLoader) {
103107
const urlLoaderRule = config.module
104108
.rule(CHAIN_ID.RULE.SVG)
105109
.oneOf(CHAIN_ID.ONE_OF.SVG)
@@ -113,8 +117,8 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
113117
});
114118
config.plugin(LibSvgrPatchPlugin.name).use(LibSvgrPatchPlugin, []);
115119
}
116-
// 2. in bundleless, only support transform the svg asset to mixedImport svgr file
117-
// remove issuer to make every svg asset is transformed
120+
// 2. in bundleless, do not support queryImport
121+
// remove issuer to make every svg asset is transformed by svgr-loader
118122
if (!bundle) {
119123
if (isUsingSvgr) {
120124
const rule = config.module

0 commit comments

Comments
 (0)