Skip to content

Commit 0fbb6e7

Browse files
authored
fix: failed to disable emitAssets when using SVGR (#2136)
1 parent c4ea766 commit 0fbb6e7

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect, test } from '@playwright/test';
2+
import { build, gotoPage } from '@e2e/helper';
3+
4+
test('should import svg with SVGR plugin and query URL correctly', async () => {
5+
const rsbuild = await build({
6+
cwd: __dirname,
7+
});
8+
9+
const files = await rsbuild.unwrapOutputJSON();
10+
const filenames = Object.keys(files);
11+
12+
expect(
13+
filenames.some((filename) =>
14+
filename.includes('dist/static/svg/mobile.svg'),
15+
),
16+
).toBeFalsy();
17+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineConfig } from '@rsbuild/core';
2+
import { pluginReact } from '@rsbuild/plugin-react';
3+
import { pluginSvgr } from '@rsbuild/plugin-svgr';
4+
5+
export default defineConfig({
6+
plugins: [
7+
pluginReact(),
8+
pluginSvgr({
9+
svgrOptions: {
10+
exportType: 'default',
11+
},
12+
}),
13+
],
14+
output: {
15+
filenameHash: false,
16+
emitAssets: () => false,
17+
},
18+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import svgImg from '@assets/mobile.svg?url';
2+
3+
console.log(svgImg);

packages/plugin-svgr/src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,18 @@ export const pluginSvgr = (options: PluginSvgrOptions = {}): RsbuildPlugin => ({
7575
const maxSize =
7676
typeof dataUriLimit === 'number' ? dataUriLimit : dataUriLimit.svg;
7777

78-
// delete Rsbuild builtin SVG rules
79-
chain.module.rules.delete(CHAIN_ID.RULE.SVG);
78+
let generatorOptions: Rspack.GeneratorOptionsByModuleType['asset/resource'] =
79+
{};
80+
81+
if (chain.module.rules.has(CHAIN_ID.RULE.SVG)) {
82+
generatorOptions = chain.module.rules
83+
.get(CHAIN_ID.RULE.SVG)
84+
.oneOfs.get(CHAIN_ID.ONE_OF.SVG_URL)
85+
.get('generator');
86+
87+
// delete Rsbuild builtin SVG rules
88+
chain.module.rules.delete(CHAIN_ID.RULE.SVG);
89+
}
8090

8191
const rule = chain.module.rule(CHAIN_ID.RULE.SVG).test(SVG_REGEX);
8292

@@ -93,9 +103,7 @@ export const pluginSvgr = (options: PluginSvgrOptions = {}): RsbuildPlugin => ({
93103
.oneOf(CHAIN_ID.ONE_OF.SVG_URL)
94104
.type('asset/resource')
95105
.resourceQuery(/(__inline=false|url)/)
96-
.set('generator', {
97-
filename: outputName,
98-
});
106+
.set('generator', generatorOptions);
99107

100108
// force to inline: "foo.svg?inline"
101109
rule
@@ -169,9 +177,7 @@ export const pluginSvgr = (options: PluginSvgrOptions = {}): RsbuildPlugin => ({
169177
maxSize,
170178
},
171179
})
172-
.set('generator', {
173-
filename: outputName,
174-
});
180+
.set('generator', generatorOptions);
175181

176182
// apply current JS transform rule to SVGR rules
177183
const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);

0 commit comments

Comments
 (0)