255 words
1 minute
Why Vite? (And How to Set It Up)
Why Vite?
Forget Create React App. Vite is the answer.
Vite vs CRA - No Contest
| Feature | Vite | Create React App |
|---|---|---|
| Dev Server Start | ~300ms | ~30+ seconds |
| Hot Reload | Instant | 1-3 seconds |
| Build Time | Fast | Slow |
| Bundle Size | Smaller | Larger |
| Modern JS | Native | Polyfills |
Quick Start
React + TypeScript
npm create vite@latest my-app -- --template react-tscd my-appnpm installnpm run devVue
npm create vite@latest my-app -- --template vueVanilla
npm create vite@latest my-app -- --template vanillaProject Structure
my-app/├── public/│ └── favicon.ico├── src/│ ├── components/│ ├── App.tsx│ ├── main.tsx│ └── index.css├── index.html├── package.json├── tsconfig.json└── vite.config.tsMy Vite Config
import { defineConfig } from 'vite';import react from '@vitejs/plugin-react';import path from 'path';
export default defineConfig({ plugins: [react()], resolve: { alias: { '@': path.resolve(__dirname, './src'), }, }, server: { port: 3000, open: true, }, build: { outDir: 'dist', sourcemap: true, },});Path Aliases (Game Changer)
resolve: { alias: { '@': path.resolve(__dirname, './src'), '@components': path.resolve(__dirname, './src/components'), '@utils': path.resolve(__dirname, './src/utils'), },}Now you can do:
import Button from '@components/Button';import { formatDate } from '@utils/date';Instead of:
import Button from '../../../components/Button';// grossEnvironment Variables
VITE_API_URL=https://api.example.comVITE_APP_NAME=My App// Use it like thisconst apiUrl = import.meta.env.VITE_API_URL;Adding Tailwind
npm install -D tailwindcss postcss autoprefixernpx tailwindcss init -p@tailwind base;@tailwind components;@tailwind utilities;Why Is Vite So Fast?
- Native ESM — No bundling in dev mode
- esbuild — 10-100x faster than webpack
- Hot Module Replacement — Only updates what changed
- On-demand compilation — Only compiles what you need
Commands
# Developmentnpm run dev
# Build for productionnpm run build
# Preview production buildnpm run previewOnce you go Vite, you never go back!
Seriously, try it. You’ll thank me later.
Why Vite? (And How to Set It Up)
https://blog.lukkid.dev/posts/why-vite/