캡스톤 디자인 프로젝트를 진행하면서 빠른 세팅을 위해 CRA(Create React App)를 사용했지만, 아래와 같은 단점에 따라 마이그레이션을 진행하기로 했다.
대안으로 Next.js와 Vite를 고려했으나, 페이지 수가 많지 않은 프로젝트 특성상 가볍고 빠른 Vite를 선택하여 마이그레이션을 진행했다.
npm install -D vite @vitejs/plugin-react
설치 중 문제
npm install -D @types/node@latest
npm uninstall react-scripts
Vite로 전환되었음을 반영하기 위해 scripts를 다음과 같이 수정:
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
}
플러그인 추가:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import svgr from "vite-plugin-svgr";
export default defineConfig({
plugins: [
react({
babel: {
plugins: ["babel-plugin-macros", "babel-plugin-styled-components"],
},
}),
svgr(),
],
server: {
port: 3000,
},
});
{
"compilerOptions": {
...
"types": ["vite/client"]
},
"include": ["src", "tailwind.config.js"]
}
Vite는 CRA와 다른 방식으로 정적 파일을 처리하므로 index.html을 프로젝트 최상단 디렉토리로 이동하고, 다음과 같이 수정
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
기존 CRA에서 사용되던 %PUBLIC_URL% 경로를 제거해야 함.
Vite는 환경 변수 앞에 VITE 접두사를 요구하므로 기존 REACT_APP 변수를 모두 VITE로 변경
마이그레이션 과정에서 사용하지 않게 된 아래 라이브러리를 제거:
기존 CRA와 Vite의 차이로 인해 import 방식 수정이 필요했음
// CRA 방식
import { ReactComponent as PlayListIcon } from "../../assets/playlist-icon.svg";
// Vite 방식
import PlayListIcon from "../../assets/playlist-icon.svg";
Vite의 환경 변수 처리 방식에 맞게 코드를 수정
export const axiosInstance = axios.create({
baseURL: import.meta.env.VITE_SERVER_URL,
headers: {
"Content-Type": "application/json",
},
withCredentials: true,
});
Vite로 마이그레이션 한 후, 프로젝트의 개발 서버 속도와 빌드 속도가 크게 향상되었음
CRA 프로젝트 개발 서버 구동시간, 빌드시간: 5초, 약 50초
Vite+React 개발 서버 구동시간, 빌드시간: 0.5초, 약 25초