vite-react-svg
March 29, 2026 ยท View on GitHub
A Vite plugin that transforms SVG files into React components using the DOMParser.
Performance
This plugin significantly outperforms alternatives in terms of speed and consistency. You can boost the render speed of your SVG components by up to 3 times.
Performance Benchmark (100 iterations x 5 samples):
| Plugin | Time (ms) | Output Size (bytes) |
|---|---|---|
| vite-react-svg | 53.19 | 783 |
| vite-plugin-svgr | 158.15 | 958 |
Relative Performance: vite-react-svg is 3x faster!
Notes
- find demo/benchmarks.ts and run these tests yourself.
- other tools like unplugin-icons require SVGR and fall into the same performance category.
Key Advantages
- ๐ Superior Speed: Processes SVGs ~5x faster than alternatives;
- ๐ฏ Consistent Performance: Much lower variance in processing time (ยฑ11.21ms vs ยฑ101.89ms);
- ๐ Ecosystem Compatible: Uses esbuild formatter for seamless integration with other Vite plugins.
Visual Performance Comparison:
Processing Time (ms) - Lower is better
vite-react-svg โโโโโโ 53ms
vite-plugin-svgrโโโโโโโโโโโโโโโโโ 158ms
Note - the results are coming from a desktop PC with NodeJS v24.x and vite 8. Your results my vary.
Features
- ๐ Fast transformation using DOMParser
- ๐ฏ TypeScript support
- ๐ง Configurable transformation options
- ๐ฅ Hot Module Replacement (HMR) support
- โก Vitest powered tests
Installation
npm install -D vite-react-svg
pnpm add -D vite-react-svg
yarn add -D vite-react-svg
deno add -D npm:vite-react-svg
bun add -D vite-react-svg
Usage
Configuration
// vite.config.ts
import { defineConfig } from 'vite'
import reactSVG from 'vite-react-svg'
export default defineConfig({
plugins: [
// other plugins
reactSVG({
// optional
})
]
})
Options
While the default options work just fine, for your convenience the plugin allows you to set them all:
interface VitePluginReactSvgOptions {
oxcOptions?: OxcOptions,
esbuildOptions?: EsbuildTransformOptions,
include?: string | RegExp | (string | RegExp)[]
exclude?: string | RegExp | (string | RegExp)[]
}
oxcOptions: additional oxc transformation options forrolldown;esbuildOptions: deprecated EsbuildTransformOptions esbuild will make sure the plugin will work seamless within the Vite ecosystem and provides some additional options;include: filter option to include one or more RegExp for file IDs; the default value is["**/*.svg?react"];exclude: filter option to exclude one or more RegExp for file IDs.
Note - If you modify or add more include filters and you're using Typescript, you should also define new global modules.
Typescript
To add typescript support, edit your src/vite-env.d.ts (or any global types you have set in your app) as follows:
/// <reference types="vite/client" />
/// <reference types="vite-react-svg" />
In Your Code
import Icon from './icon.svg?react'
const app = () => {
return <div>
<Icon
class='my-icon'
width={24} height={24}
style={{ fill: "currentColor" }}
/>
</div>
}
Notes:
- All properties present in the markup of your SVG files will be used as default values;
- The
viewBoxandxmlnsproperties are somewhat required in order for the SVG to be rendered properly; - The plugin will also resolve SVG files from the
/publicfolder or any validviteConfig.publicDiroption.
Acknowledgments
- vite-plugin-svgr - For inspiration on the plugin architecture.
- vite-solid-svg - For a SolidJS version.
License
vite-react-svg is released under MIT License.