Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions e2e/cases/plugin-svgr/import-attributes-text/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { expect, test } from '@e2e/helper';

test('should import SVG as text with import attributes when using pluginSvgr', async ({
page,
runBothServe,
}) => {
await runBothServe(async () => {
expect(await page.evaluate('window.svgText')).toBe(
readFileSync(join(import.meta.dirname, '../../../assets/circle.svg'), 'utf-8'),
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from '@rsbuild/core';
import { pluginSvgr } from '@rsbuild/plugin-svgr';

export default defineConfig({
plugins: [pluginSvgr()],
});
3 changes: 3 additions & 0 deletions e2e/cases/plugin-svgr/import-attributes-text/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import svgText from '@e2e/assets/circle.svg' with { type: 'text' };

window.svgText = svgText;
1 change: 1 addition & 0 deletions packages/core/src/configChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const CHAIN_ID = {
/** SVG oneOf rules */
SVG: 'svg',
SVG_RAW: 'svg-asset-raw',
SVG_TEXT: 'svg-asset-text',
SVG_URL: 'svg-asset-url',
SVG_ASSET: 'svg-asset',
SVG_REACT: 'svg-react',
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-svgr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ export const pluginSvgr = (options: PluginSvgrOptions = {}): RsbuildPlugin => ({
.type('asset/inline')
.resourceQuery(/^\?inline$/);

// get SVG source: `import source from "foo.svg" with { type: "text" }`
if (CHAIN_ID.ONE_OF.SVG_TEXT) {
rule.oneOf(CHAIN_ID.ONE_OF.SVG_TEXT).type('asset/source').with({ type: 'text' });
Comment thread
chenjiahan marked this conversation as resolved.
}

// get raw content: "foo.svg?raw"
if (CHAIN_ID.ONE_OF.SVG_RAW) {
rule
Expand Down
30 changes: 30 additions & 0 deletions packages/plugin-svgr/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ exports[`svgr > configure SVGR options 1`] = `
"resourceQuery": /\\^\\\\\\?inline\\$/,
"type": "asset/inline",
},
{
"type": "asset/source",
"with": {
"type": "text",
},
},
{
"resourceQuery": /\\^\\\\\\?raw\\$/,
"type": "asset/source",
Expand Down Expand Up @@ -116,6 +122,12 @@ exports[`svgr > exportType default / mixedImport false 1`] = `
"resourceQuery": /\\^\\\\\\?inline\\$/,
"type": "asset/inline",
},
{
"type": "asset/source",
"with": {
"type": "text",
},
},
{
"resourceQuery": /\\^\\\\\\?raw\\$/,
"type": "asset/source",
Expand Down Expand Up @@ -287,6 +299,12 @@ exports[`svgr > exportType default / mixedImport true 1`] = `
"resourceQuery": /\\^\\\\\\?inline\\$/,
"type": "asset/inline",
},
{
"type": "asset/source",
"with": {
"type": "text",
},
},
{
"resourceQuery": /\\^\\\\\\?raw\\$/,
"type": "asset/source",
Expand Down Expand Up @@ -458,6 +476,12 @@ exports[`svgr > exportType named / mixedImport false 1`] = `
"resourceQuery": /\\^\\\\\\?inline\\$/,
"type": "asset/inline",
},
{
"type": "asset/source",
"with": {
"type": "text",
},
},
{
"resourceQuery": /\\^\\\\\\?raw\\$/,
"type": "asset/source",
Expand Down Expand Up @@ -629,6 +653,12 @@ exports[`svgr > exportType named / mixedImport true 1`] = `
"resourceQuery": /\\^\\\\\\?inline\\$/,
"type": "asset/inline",
},
{
"type": "asset/source",
"with": {
"type": "text",
},
},
{
"resourceQuery": /\\^\\\\\\?raw\\$/,
"type": "asset/source",
Expand Down
Loading