VITE_GUIDE.md

md8 kBIn Examples.zip

⚡ Vite Guide for React

🚀 What is Vite?

Vite (French word for "quick", pronounced /vit/) is a modern build tool that provides:

  • ⚡️ Lightning Fast HMR (Hot Module Replacement)
  • 🔥 Instant Server Start - No bundling in development
  • 📦 Optimized Build - Uses Rollup for production
  • 🎯 Pre-configured - Works out of the box
  • 🔧 Powerful - Rich plugin ecosystem

Why Vite Over Create React App?

Feature Vite Create React App
Dev Server Start Instant 10-30 seconds
HMR Speed < 100ms 1-3 seconds
Build Tool Rollup Webpack
Configuration Simple Complex
File Size Smaller Larger
Modern Yes Becoming outdated

📁 Vite Project Structure

my-react-app/
├── index.html          # Entry HTML (at root, not in public/)
├── package.json        # Dependencies with Vite
├── vite.config.js      # Vite configuration
├── public/             # Static assets
│   └── (images, fonts, etc.)
└── src/
    ├── main.jsx        # Entry point (not index.js)
    ├── App.jsx         # Main component
    ├── App.css         # Component styles
    └── index.css       # Global styles

Key Differences from Create React App:

  1. index.html is at root (not in public/)
  2. Entry file is main.jsx (not index.js)
  3. Use .jsx extension for files with JSX
  4. No PUBLIC_URL - use / for public assets

🛠️ Creating a New Vite React Project

Quick Start:

Shell
# Using npm
npm create vite@latest my-react-app -- --template react

# Using yarn
yarn create vite my-react-app --template react

# Using pnpm
pnpm create vite my-react-app --template react

Step-by-Step:

Shell
# Create project
npm create vite@latest my-react-app -- --template react

# Navigate to project
cd my-react-app

# Install dependencies
npm install

# Start development server
npm run dev

📦 Package.json Configuration

JSON
{
  "name": "my-react-app",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@vitejs/plugin-react": "^4.2.1",
    "vite": "^5.0.8"
  }
}

Available Scripts:

  • npm run dev - Start development server (default: http://localhost:5173)
  • npm run build - Build for production
  • npm run preview - Preview production build locally

⚙️ Vite Configuration (vite.config.js)

Basic Configuration:

JavaScript
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  server: {
    port: 3000,  // Default is 5173
    open: true   // Auto-open browser
  }
})

Advanced Configuration:

JavaScript
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  server: {
    port: 3000,
    host: true,
    open: true
  },
  build: {
    outDir: 'dist',
    sourcemap: true
  },
  resolve: {
    alias: {
      '@': '/src'
    }
  }
})

📄 index.html Structure

Vite uses index.html as the entry point:

HTML
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My React App</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.jsx"></script>
  </body>
</html>

Key Points:

  • Located at project root (not in public/)
  • Uses <script type="module"> to load entry file
  • References /src/main.jsx directly

🎯 Entry Point (main.jsx)

JavaScript
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
)

🖼️ Using Assets

Images in src/:

JavaScript
import logo from './assets/logo.png'

function App() {
  return <img src={logo} alt="Logo" />
}

Images in public/:

JavaScript
function App() {
  return <img src="/images/logo.png" alt="Logo" />
}

CSS/Styles:

JavaScript
import './App.css'  // Works the same as CRA

🔌 Environment Variables

.env File:

Shell
# Use VITE_ prefix (not REACT_APP_)
VITE_API_URL=https://api.example.com
VITE_APP_TITLE=My App

Usage:

JavaScript
const apiUrl = import.meta.env.VITE_API_URL
const title = import.meta.env.VITE_APP_TITLE

// Built-in variables
const isDev = import.meta.env.DEV
const isProd = import.meta.env.PROD

Key Differences from CRA:

  • Use VITE_ prefix instead of REACT_APP_
  • Access via import.meta.env instead of process.env

📦 Installing Additional Packages

React Router:

Shell
npm install react-router-dom

Styled Components:

Shell
npm install styled-components

Axios:

Shell
npm install axios

🚀 Deployment

Build for Production:

Shell
npm run build

This creates a dist/ folder with optimized files.

Preview Production Build:

Shell
npm run preview

Deploy to Various Platforms:

Vercel:

Shell
npm install -g vercel
vercel

Netlify:

Shell
npm install -g netlify-cli
netlify deploy --prod

GitHub Pages:

Shell
# Add to vite.config.js
base: '/your-repo-name/'

# Build and deploy
npm run build

🔥 Hot Module Replacement (HMR)

Vite's HMR is incredibly fast:

JavaScript
// Automatically enabled for React components
// Changes appear instantly without full page reload

// For custom HMR:
if (import.meta.hot) {
  import.meta.hot.accept()
}

🎨 CSS/Styling Options

Plain CSS:

JavaScript
import './App.css'

CSS Modules:

JavaScript
import styles from './App.module.css'

function App() {
  return <div className={styles.container}>Hello</div>
}

SCSS/SASS:

Shell
npm install -D sass
JavaScript
import './App.scss'

Styled Components:

Shell
npm install styled-components
JavaScript
import styled from 'styled-components'

⚡ Performance Tips

  1. Use Dynamic Imports:
JavaScript
const Component = lazy(() => import('./Component'))
  1. Optimize Images:
  • Use WebP format
  • Place large images in public/
  • Use proper image sizes
  1. Code Splitting:
  • Vite automatically splits code
  • Use React.lazy() for route-based splitting
  1. Tree Shaking:
  • Import only what you need
  • Vite automatically removes unused code

🐛 Troubleshooting

Port Already in Use:

Shell
# Change port in vite.config.js
server: {
  port: 3001
}

Clear Cache:

Shell
rm -rf node_modules/.vite
npm run dev

Build Errors:

Shell
# Clean and rebuild
rm -rf dist node_modules
npm install
npm run build

📊 Vite vs CRA Migration Checklist

  • Move public/index.html to root index.html
  • Rename src/index.js to src/main.jsx
  • Update package.json (vite instead of react-scripts)
  • Add vite.config.js
  • Update index.html with <script type="module">
  • Change .js to .jsx for files with JSX
  • Update env variables: REACT_APP_ → VITE_
  • Update env access: process.env → import.meta.env
  • Update scripts: start → dev

🎯 Quick Comparison Table

Aspect Create React App Vite
Command npm start npm run dev
Port 3000 5173 (configurable)
Entry src/index.js src/main.jsx
HTML public/index.html index.html
Env Prefix REACT_APP_ VITE_
Env Access process.env import.meta.env
Build Output build/ dist/

🌟 Why Students Should Use Vite

  1. Instant Feedback - See changes in < 100ms
  2. Modern Standard - Industry is moving to Vite
  3. Better DX - Faster development experience
  4. Learn Modern Tools - Vite is the future
  5. Easy Setup - Simpler configuration

📚 Additional Resources


Ready to use Vite? All our React examples now use Vite! ⚡

In Examples.zip

Chapter01-Introduction/6
src/4App.css3 kBApp.jsx4 kBindex.css1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBserve-dist.bat1 kBstart-dev-server.bat1 kBvite.config.js1 kB
Chapter02-GettingStarted/5
public/1index.html1 kB
src/4App.css1 kBApp.jsx2 kBindex.css1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter03-JSX/5
public/1index.html1 kB
src/4App.css1 kBApp.jsx5 kBindex.css1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter04-Components/5
public/1index.html1 kB
src/5
components/3Card.jsx1 kBFooter.jsx1 kBHeader.jsx1 kB
App.css1 kBApp.jsx4 kBindex.css1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter05-Props/5
public/1index.html1 kB
src/4App.css1 kBApp.jsx7 kBindex.css1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter06-Events/4
src/4App.css1 kBApp.jsx10 kBindex.css1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter06-State/3
public/1index.html1 kB
src/4App.css3 kBApp.js5 kBindex.css1 kBindex.js1 kB
package.json1 kB
Chapter07-Hooks/5
public/1index.html1 kB
src/4App.css1 kBApp.jsx8 kBindex.css1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter08-Conditional/4
src/4App.css1 kBApp.jsx11 kBindex.css1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter08-Events/3
public/1index.html1 kB
src/4App.css3 kBApp.js7 kBindex.css1 kBindex.js1 kB
package.json1 kB
Chapter09-Forms/5
public/1index.html1 kB
src/4App.css1 kBApp.jsx12 kBindex.css1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter10-Router/5
public/1index.html1 kB
src/5App.css1 kBApp.js6 kBindex.css1 kBindex.js1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter11-Styling/5
public/1index.html1 kB
src/6App.css3 kBApp.js5 kBButton.module.css1 kBindex.css1 kBindex.js1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter12-UseEffect/5
public/1index.html1 kB
src/5App.css2 kBApp.js7 kBindex.css1 kBindex.js1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter13-UseContext/5
public/1index.html1 kB
src/5App.css3 kBApp.js7 kBindex.css1 kBindex.js1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter14-UseRef/5
public/1index.html1 kB
src/5App.css3 kBApp.js7 kBindex.css1 kBindex.js1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
Chapter15-CustomHooks/5
public/1index.html1 kB
src/5App.css3 kBApp.js8 kBindex.css1 kBindex.js1 kBmain.jsx1 kB
index.html1 kBpackage.json1 kBvite.config.js1 kB
⚠️_READ_THIS_FIRST.md1 kBINSTALLATION_GUIDE.md6 kBVITE_GUIDE.md8 kB