By default, Rslib treats SVG images as static assets.
By adding the @rsbuild/plugin-svgr plugin, Rslib supports transforming SVG to React components via SVGR.
You can install the plugin using the following command:
You can register the plugin in the rslib.config.ts
file:
In bundle mode, all usages supported by @rsbuild/plugin-svgr are available in Rslib. The only difference is that when using an SVG file in URL form, Rslib will retain the import
statement for the static asset in the output according to Import static assets.
Here is an example of usage:
If the import path does not include the ?react
suffix, the SVG will be treated as a normal static asset, and the import
statement for the static asset will be retained.
@rsbuild/plugin-svgr
also supports default imports and mixed imports:
@rsbuild/plugin-svgr
also supports default import and mixed import:
'default'
.In bundleless mode, since each file undergoes code transformation independently, the import ways of ?react
and ?url
are not supported. However, the following two usage forms are supported:
@rsbuild/plugin-svgr
supports named import of ReactComponent
to use SVGR. You need to set svgrOptions.exportType to 'named'
.
All .svg
files will be transformed into React components. At this time, you can:
'default'
.If your library has both URL and React component import forms for SVG files, you can also use mixed import.
At this time, the imported SVG file will export both the URL and the React component.
For more information, refer to the mixedImport option.