File tree Expand file tree Collapse file tree 1 file changed +31
-1
lines changed
Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change 1+ import { readFileSync } from "node:fs" ;
12import { resolve } from "node:path" ;
3+ import { transform as svgrTransform } from "@svgr/core" ;
24import react from "@vitejs/plugin-react" ;
5+ import { transform as esbuildTransform } from "esbuild" ;
6+ import type { Plugin } from "vite" ;
37import { defineConfig } from "vitest/config" ;
48
9+ /**
10+ * Minimal Vite plugin that transforms .svg imports into React components
11+ * using @svgr/core (already installed as a dependency of @svgr/webpack).
12+ * esbuild (bundled with Vite) compiles the resulting JSX to plain JS.
13+ */
14+ function svgr ( ) : Plugin {
15+ return {
16+ name : "vite-svgr" ,
17+ enforce : "pre" ,
18+ async transform ( _code , id ) {
19+ if ( ! id . endsWith ( ".svg" ) ) return null ;
20+ const svg = readFileSync ( id , "utf-8" ) ;
21+ const jsx = await svgrTransform (
22+ svg ,
23+ { jsxRuntime : "automatic" , plugins : [ "@svgr/plugin-jsx" ] } ,
24+ { filePath : id } ,
25+ ) ;
26+ const { code } = await esbuildTransform ( jsx , {
27+ loader : "jsx" ,
28+ jsx : "automatic" ,
29+ } ) ;
30+ return { code, map : null } ;
31+ } ,
32+ } ;
33+ }
34+
535export default defineConfig ( {
6- plugins : [ react ( ) ] ,
36+ plugins : [ svgr ( ) , react ( ) ] ,
737 resolve : {
838 alias : {
939 "@" : resolve ( __dirname , "./src" ) ,
You can’t perform that action at this time.
0 commit comments