vite-react-svg

March 29, 2026 ยท View on GitHub

Coverage Status ci NPM Version

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):

PluginTime (ms)Output Size (bytes)
vite-react-svg53.19783
vite-plugin-svgr158.15958

Relative Performance: vite-react-svg is 3x faster!

Notes

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 for rolldown;
  • 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 viewBox and xmlns properties are somewhat required in order for the SVG to be rendered properly;
  • The plugin will also resolve SVG files from the /public folder or any valid viteConfig.publicDir option.

Acknowledgments

License

vite-react-svg is released under MIT License.