1. vite-plugin-svgr 다운
pnpm add -D vite-plugin-svgr
2. 설정 추가
vite.config.ts
plugins에 svgr() 추가
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import svgr from 'vite-plugin-svgr'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react(),
svgr({
svgrOptions: {
// svgr options
},
}),
],
})
2-1. vite-plugin-svgr ^4.0.0
vite-env.d.ts
/// <reference types="vite-plugin-svgr/client" />
App.tsx
vite-plugin-svgr ^4.0.0 : 파일명 뒤에 ?react query suffix 추가
import SvgIcon from '@/assets/svgs/svgIcon.svg?react';
function App() {
return (
<SvgIcon width="25px" height="25px" />
);
}
2-2. vite-plugin-svgr 4.0 이하
vite-env.d.ts
/// <reference types="vite/client" />
declare module '*.svg' {
import React = require('react');
export const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>;
const src: string;
export default src;
}
import { ReactComponent as SvgIcon } from './svgIcon.svg';
참고자료
https://stackoverflow.com/questions/70309561/unable-to-import-svg-with-vite-as-reactcomponent
'dev💻 > 프론트엔드' 카테고리의 다른 글
[React, Typescript] 구글 로그인 (0) | 2024.10.06 |
---|---|
[React] 인프라 설정으로 CORS 이슈 피하고 화면 캡쳐하기 (html2canvas, puppeteer, AWS S3) (0) | 2024.05.26 |
[React] 브라우저 쿠키를 하나의 파일에서 관리하기 (0) | 2023.09.25 |
[React] 스크롤 높이에 반응하는 이벤트 붙이기 (0) | 2023.08.24 |
[Vite+Typescript+Eslint] 절대경로 설정 (0) | 2023.08.22 |