vite-plugin-svgr ãã©ã°ã¤ã³ãã¢ãããã¼ãããè¨å®ãªã©ãå¤ãã£ã¦ããã®ã§æ¹ãã¦ã¡ã¢
ç°å¢
- vite-plugin-svgr
4.2.0
- Vite
4.4.9
- TypeScript
5.1.6
vite-plugin-svgr v3 â v4
çµè«ã¯æä¸é¨ã«ããã¾ãããæ¥ãã®æ¹ã¯éä¸ã¹ããããã¦ãã ããï¼
Before
ãã©ã°ã¤ã³ã®è¨å®
vite.config.ts
import svgr from 'vite-plugin-svgr'; export default { // ... plugins: [svgr()], }
TypeScript ã« SVG ã React Component ã¨ãã¦è§£éãããããã®åãã¡ã¤ã«ãä½æ
@/types/svg.d.ts
declare module '*.svg' { import React = require('react'); export const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>; }
cf. reactjs - Module '"*.svg"' has no exported member 'ReactComponent' - Stack Overflow
App.tsx
import { ReactComponent as Logo } from './logo.svg'; return App: FC = () => { return <div><Logo /></div>; };
After
svg 㯠ReactComponent as ã使ãã .svg?react
ã§ã¤ã³ãã¼ããã
å
¬å¼ã®ããã¥ã¡ã³ãã« import Logo from "./logo.svg?react";
ã®ããã«ã¤ã³ãã¼ãããããã«æ¸ããã¦ãã
1. {ReactComponent as Component}
ã default import ã«ä¿®æ£ãã
App.tsx
import Logo from './logo.svg'; return App: FC = () => { return <div><Logo /></div>; };
ãã®ç¶æ
ã ã¨ããã³ã㧠Failed to execute 'createElement' on 'Document': The tag name provided ('/src/logo.svg') is not a valid name.
ã®ãããªã¨ã©ã¼ãåºã
2. .svg?react
㧠import ããããã«ãã
ã©ããã SVG 㯠.svg?react
ã®å½¢ã§ã¤ã³ãã¼ãããªããã°ãã©ã°ã¤ã³ãåä½ããªãããã«ãªã£ã¦ããã£ã½ã
tsconfig.json
ãå¤æ´ã㦠.svg?react
ã許容ããããã«ãã
{ "compilerOptions": { }, "include": ["src", "**/*.svg?react"], }
App.tsx
- import Logo from './logo.svg'; + import Logo from './logo.svg?react'; return App: FC = () => { return <div><Logo /></div>; };
3. vite-env.d
ã« <reference types="vite-plugin-svgr/client" />
ã追å ãã
ãã®ç¶æ
ã 㨠import Logo from './logo.svg?react
ã®ç®æã Cannot find module '@/logo.svg?react' or its corresponding type declarations.ts (2307)
ã¨ãã Type Error ãçºçãã¦ãã¾ãã
å
¬å¼ããã¥ã¡ã³ãã«ããããã« vite-env.d
ã« /// <reference types="vite-plugin-svgr/client" />
ã¨ããä¸æã追å ããã¨ãã®ã¨ã©ã¼ã¯è§£æ¶ããã
vite-env.d
/// <reference types="vite/client" /> /// <reference types="vite-plugin-svgr/client" />
ãã㧠vite-plugin-svgr v4 㧠SVG ã React Component ã¨ãã¦æ±ããããã«ãªãã¾ããï¼
.svg?react
㧠import ãããã®ãã¤ã±ã¦ãªãâ¦
ãã©ã°ã¤ã³ã .svg?react
ã§ã®ã¤ã³ãã¼ããç¯æ£ãã¦ãããããã« tsconfig ãä¿®æ£ããããSVG ã使ãããæã«é½åº¦ ?react
ã追å ããã®ãé¢åã§ã¤ã±ã¦ãªãã®ã§ ?react
ç¡ãã§ãã©ã°ã¤ã³ãåä½ããããã«ããã
ãã©ã°ã¤ã³ã®è¨å®ã§ ?react
ç¡ãã«ã§ããï¼
ãã©ã°ã¤ã³ã®ã³ã¼ããè¦ã¦ããã¨ä¸è¨ã®ããã«ãã©ã°ã¤ã³ã®åæåé¨åã§æ¡å¼µåããªãã·ã§ã³ã«ãªã£ã¦ãã¾ãã
export default function vitePluginSvgr({ svgrOptions, esbuildOptions, include = "**/*.svg?react", exclude, }: VitePluginSvgrOptions = {}): Plugin {
cf. vite-plugin-svgr/src/index.ts#L14-L19
vite.config.ts
ã§ãã©ã°ã¤ã³ãå®è¡ãããç®æ㧠include
ãªãã·ã§ã³ã使ã£ã¦ ?react
ç¡ãã® svg ã対象ã«ãã¦ãã¾ãã° ?react
ç¡ãã§åä½ããããã¨ãã§ãã¾ãã
vite.config.ts
import svgr from 'vite-plugin-svgr'; export default { // ... - plugins: [svgr()], + plugins: [svgr({ include: "**/*.svg" })], }
.svg?react
ã使ç¨ããªãã®ã§ tsconfig 㧠*.svg?react
ãæå¹åããå¿
è¦ãããã¾ãã
tsconfig.json
{ "compilerOptions": { }, - "include": ["src", "**/*.svg?react"], + "include": ["src"], }
åæ§ã« /// <reference types="vite-plugin-svgr/client" />
ã®è¨å®ã .svg?react
ã¨ããæ¡å¼µåã®åã¨ã©ã¼ã«å¯¾å¿ãããã®ã ã£ãã®ä¸è¦
vite-env.d
/// <reference types="vite/client" /> - /// <reference types="vite-plugin-svgr/client" /> +
ãã㧠import Logo from './logo.svg'
ã®ãããªå½¢ã§ SVG ã React Componentn ã¨ãã¦æ±ããããã«ãªãã¾ããï¼
?react
ã®ãªãã·ã§ã³è¦ããªããªãï¼ã£ã¦æã£ã¦ãã¾ã£ãâ¦
çµè« vite-plugin-svgr v4 ã¯ããã©ã«ãã§ã¯ .svg?react
æ¡å¼µåã対象ã«ãã¦ãããããªãã·ã§ã³ã§ .svg
ã対象ã«å¤æ´ããã¨ç°¡åï¼
vite.config.ts
import svgr from 'vite-plugin-svgr'; export default { // ... plugins: [svgr({ include: "**/*.svg" })], }
App.tsx
import Logo from './logo.svg'; return App: FC = () => { return <div><Logo /></div>; };
ããã³ãã¨ã³ã ã¹ããã¯æè¦ã§ä»æ§ãå¤ããããã¢ãããã¼ãã§ããããã¡â¦
ãããï¼
[åè]