When to use
- The project uses
react-scripts(CRA, deprecated since 14 Feb 2025) and the user wants a supported build. - If the user wants a framework instead (Next.js, React Router framework mode), follow that framework's CRA guide; this skill covers a client-only SPA on Vite (see
frontend/react-vite-spa).
Before you start
- Read
react-scripts, React, TypeScript, CRACO/react-app-rewired config,src/setupProxy.js,.env*files,public/index.html, Jest config inpackage.json, and how SVGs, CSS modules and images are imported. - Working tree clean; new branch (
chore/vite); record the current build output size and a list of pages to smoke-test. - Ask the user:
- whether to switch tests to Vitest now or keep Jest for this change;
- the deployment base path (
homepageinpackage.json) and the hosting setup; - whether network installs are approved (
core/supply-chaincooldown applies).
Steps
-
Baseline:
npm test -- --watchAll=false,npm run build, and a smoke test of key pages. -
Install:
npm install -D vite @vitejs/plugin-react(andvite-plugin-svgrif the app imports SVGs as React components,vite-tsconfig-pathsif it usesbaseUrl/paths). Removereact-scripts, CRACO or react-app-rewired after the build works. -
Config: add
vite.config.ts:import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' export default defineConfig({ plugins: [react()], server: { port: 3000 }, // keep CRA's port if scripts or docs depend on it build: { outDir: 'build' }, // keep CRA's output folder if deploys expect it })Set
baseif the app is served from a sub-path (CRA'shomepage). -
index.html: movepublic/index.htmlto the project root, remove%PUBLIC_URL%(use/paths), and add<script type="module" src="/src/index.tsx"></script>(the entry file). Assets inpublic/keep working at/. -
Environment variables: rename
REACT_APP_*toVITE_*in.env*, CI and hosting settings; replaceprocess.env.REACT_APP_Xwithimport.meta.env.VITE_Xandprocess.env.NODE_ENVwithimport.meta.env.MODE/DEV/PROD. Addsrc/vite-env.d.ts(/// <reference types="vite/client" />). Everything prefixedVITE_is public; keep secrets out. -
Imports: SVG components via
vite-plugin-svgr(import Logo from './logo.svg?react'); files with JSX must use.jsx/.tsxextensions (rename.jsfiles that contain JSX); replacerequire()calls with imports. -
Proxy: move
setupProxy.jsor theproxyfield toserver.proxyinvite.config.ts. -
Scripts:
"dev": "vite","build": "tsc -b && vite build"(orvite build),"preview": "vite preview"; update CI and Dockerfiles that callreact-scripts. -
Tests: either move to Vitest (
vitest,jsdom,@testing-library/jest-dom;test.environment = 'jsdom',setupFiles; replacejest.fnwithvi.fn,jest.mockwithvi.mock), or keep Jest with an explicitjest.configand Babel/ts-jest sincereact-scripts testis gone. Seetesting/unit-js. -
Lint: replace
eslint-config-react-appwith the project's flat ESLint config (eslint-plugin-react-hooks,typescript-eslint). -
Verify (next section); show the diff and results; commit only when asked (
git-commitskill). Then usefrontend/react-vite-spa.
Verify
npm ci
npm ls react-scripts 2>/dev/null || echo "react-scripts removed"
npx tsc --noEmit
npx eslint .
npx vitest run # or: npx jest
npm run build && npx vite preview # then smoke-test
npm audit --audit-level=moderate
grep -rn "REACT_APP_\|process\.env\.\|%PUBLIC_URL%" src index.html .env* 2>/dev/null # nothing left
Smoke test the preview build: deep links load (configure the host's SPA fallback), images and fonts from public/ load, API calls reach the proxy or real API, environment-dependent features show the right values.
Safety
- Never put secrets into
VITE_*variables while renaming; check eachREACT_APP_*value is safe to expose (it already was public). - Never combine the build migration with a React major upgrade or a router change.
- Never expose the Vite dev server with
--hoston shared networks (seefrontend/react-vite-spa). - Never auto-commit, push or deploy; don't hand-edit the lockfile.