Skip to content

Instantly share code, notes, and snippets.

@moshiurse
Created August 24, 2023 01:18
Show Gist options
  • Save moshiurse/a7e2f7eef77d83709e931a58adb99a06 to your computer and use it in GitHub Desktop.
Save moshiurse/a7e2f7eef77d83709e931a58adb99a06 to your computer and use it in GitHub Desktop.
Migration from CRA to Vite

First, install Vite and all React related libraries (here: Vite's React Plugin) as development dependencies:

npm install vite @vitejs/plugin-react --save-dev

Create a vite.config.ts onn root path

import react from '@vitejs/plugin-react';

export default defineConfig(() => {
  return {
<!--  If issue arrises related cra use define  -->
   define: {
      'process.env': {},
    },
    server: {
      open: true, //if you want browser open immediately
    },
    build: {
      outDir: 'build',
    },
    plugins: [react()],
  };
});

Moving and updating your index.html

Move public/index.html to root path.
Remove all %PUBLIC_URL% occurrences in the index.html file.

mv public/index.html .

<link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> =>=>
<link rel="icon" href="/favicon.ico" />

Add script Tag in index.html

<div id="root"></div>
<script type="module" src="/src/index.tsx"></script> // jsx if you use JS

Uninstall CRA

npm uninstall react-scripts

update package.json

"scripts": {
  "start": "vite",
  "build": "tsc && vite build",
  "serve": "vite preview"
},

Update Typescript configs(update only following if you use ts)

"target": "ESNext",
"lib": [
  "dom",
  "dom.iterable",
  "esnext"
],
"types": ["vite/client", "vite-plugin-svgr/client"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment