SVGR plugin
By default, Rsbuild treats SVG files as static assets. For processing rules, see Static assets.
With the SVGR plugin, Rsbuild supports transforming SVG to React components via SVGR.
Quick start
Install plugin
You can install the plugin using the following command:
Register plugin
You can register the plugin in the rsbuild.config.ts file:
Example
Default usage
After registering the plugin, when you import an SVG in a JavaScript file, if the imported path includes the ?react suffix, Rsbuild will call SVGR to transform the SVG into a React component.
If the imported path doesn't include the ?react suffix, the SVG will be treated as a normal static asset and you will get a URL string or base64 URL. See Static assets.
Named import
@rsbuild/plugin-svgr supports named imports for ReactComponent when using SVGR. You need to set svgrOptions.exportType to 'named':
@rsbuild/plugin-svgr also supports default imports and mixed imports:
- Enable default imports by setting svgrOptions.exportType to
'default'. - Enable mixed imports by setting the mixedImport option to use both default and named imports at the same time.
Options
To customize the compilation behavior of Svgr, use the following options.
- Type:
svgrOptions
Modifies the options of SVGR, the passed object will be deep merged with the default value. See SVGR - Options for details.
- Type:
import('@svgr/core').Config - Default:
- Example:
When you set svgoConfig.plugins, the configuration for plugins with the same name is automatically merged. For example, the following configuration will be merged with the built-in preset-default:
The merged svgoConfig will be:
svgrOptions.exportType
Set the export type of SVG React components.
- Type:
'default' | 'named' - Default:
undefined
exportType can be set as:
default: use default export.named: useReactComponentnamed export.
For example, set the default export of SVG file as a React component:
Then import the SVG, you'll get a React component instead of a URL:
At this time, you can also specify the ?url query to import the URL, for example:
When svgrOptions.exportType is set to 'default', the named imports (ReactComponent) cannot be used.
mixedImport
- Type:
boolean - Default:
false
Whether to enable mixed import, allowing to use default import and named import at the same time.
Mixed import is usually used with svgrOptions.exportType: 'named', for example:
At this time, the imported SVG file will export both URL and the React component:
When mixedImport is enabled, svgrOptions.exportType defaults to 'named' if not explicitly configured.
Limitations
We recommend using ?react to convert an SVG into a React component rather than relying on mixed imports, which have the following limitations:
- Increased bundle size: Mixed import causes a single SVG module to be compiled into two types of code (even if some exports are not used), which will increase the bundle size.
- Slow down compiling: Mixed import will cause extra compilation overhead. Even if the ReactComponent export is not used in the code, the SVG file will still be compiled by SVGR. And SVGR is based on Babel, which has a high performance overhead.
query
- Type:
RegExp - Default:
/react/
Used to custom the query suffix to match SVGR transformation.
For example, if you need to match import paths with the ?svgr suffix:
exclude
- Type: RuleSetCondition
- Default:
undefined
Exclude some SVG files, they will not be transformed by SVGR.
For example, if a project includes a.svg and b.svg, you can add b.svg to exclude:
When imported, a.svg will be transformed into a React component, while b.svg will be treated as a regular static asset:
excludeImporter
- Type: RuleSetCondition
- Default:
undefined
Exclude some modules, the SVGs imported by these modules will not be transformed by SVGR.
For example, if your project contains page-a/index.ts and page-b/index.ts, you can add page-b to excludeImporter:
- SVGs referenced in page-a will be transformed to React components:
- SVGs referenced in page-b will be treated as static assets:
The query in the module path has a higher priority than exclude and excludeImporter. For example, if a module is excluded, adding ?react can still make it transformed by SVGR.
Type declaration
When you reference an SVG asset in TypeScript code, TypeScript may prompt that the module is missing a type definition:
To fix this, add type declarations for the SVG assets by creating a src/env.d.ts file and adding the declarations below.
- By default, you can add the following type declarations:
- If the value of
svgrOptions.exportTypeis'default', set the type declaration to:
- If the value of
svgrOptions.exportTypeis'named', set the type declaration to:
- If the value of
svgrOptions.exportTypeis'named', andmixedImportis enabled, set the type declaration to:
After adding the type declarations, if the type error still exists, you can try to restart the IDE, or adjust the directory where env.d.ts is located, making sure that TypeScript can correctly identify the type definition.

