Build Process
Production Build
Vite optimizes your application for production with automatic code splitting, minification, and asset optimization.vite build which:
- Bundles all source files using Rollup
- Minifies JavaScript with esbuild (extremely fast)
- Optimizes CSS and removes unused styles
- Generates hashed filenames for cache busting
- Creates source maps (when in dev mode)
- Outputs to the
dist/directory
The build process uses React SWC (
@vitejs/plugin-react-swc:2) for faster compilation compared to standard Babel.Development Build
For debugging production builds with source maps:vite build --mode development which:
- Includes source maps for easier debugging
- Preserves readable code structure
- Still optimizes bundle size
Build Output Structure
After runningnpm run build, the dist/ directory contains:
Understanding the Output
index.html
Production HTML with:
- Minified markup
- Preload hints for critical resources
- Hashed asset references for cache busting
- Meta tags from
index.html:4-15
assets/index-[hash].js
Main JavaScript bundle containing:
- React runtime and React DOM
- Application components
- React Router logic
- TanStack Query client
- Tree-shaken dependencies (unused code removed)
assets/index-[hash].css
Compiled CSS with:
- Tailwind utility classes (only used ones)
- Custom component styles
- CSS animations from
tailwind.config.ts:70-88 - Vendor prefixes via autoprefixer
Preview Production Build Locally
Before deploying, test the production build:http://localhost:4173 serving the dist/ directory.
Deployment Options
Option 1: Vercel (Recommended)
Best for: Zero-config deployments with automatic CI/CD.
Features:
- Automatic HTTPS
- Global CDN
- Instant rollbacks
- Preview deployments for every commit
- Zero configuration for Vite projects
Option 2: Netlify
Best for: Simple drag-and-drop deployments with powerful build plugins.
Netlify Configuration (
netlify.toml):
Option 3: GitHub Pages
Best for: Free hosting for open-source projects.Option 4: Docker Container
Best for: Self-hosted deployments or cloud platforms (AWS, GCP, Azure). CreateDockerfile:
nginx.conf:
Environment Variables
Vite exposes environment variables prefixed withVITE_ to client-side code.
Usage
Environment Files
| File | Purpose | Committed? |
|---|---|---|
.env | Default values | ✅ Yes |
.env.local | Local overrides (secrets) | ❌ No |
.env.production | Production-only values | ✅ Yes |
.env.development | Development-only values | ✅ Yes |
Production Optimization Tips
1. Lazy Load Routes
Split code by route for faster initial load:2. Optimize Images
Thepublic/ folder contains:
favicon.svg- Optimized vector iconplaceholder.svg- Lightweight placeholder
- WebP format for better compression
- Responsive images with
<picture>element - Image CDNs (Cloudinary, imgix) for automatic optimization
3. Enable Compression
Most hosting providers enable gzip/brotli automatically. For self-hosted: Nginx example:4. Set Cache Headers
For hashed assets (assets/*-[hash].js):
5. Analyze Bundle Size
Visualize what’s in your bundle:6. Remove Unused Dependencies
The project includes 65 dependencies frompackage.json:15-65. Audit regularly:
Performance Checklist
Before deploying:- Run
npm run buildwithout errors - Test
npm run previewlocally - Check bundle size (keep main bundle < 300KB gzipped)
- Verify all images are optimized
- Test on mobile devices and slow connections
- Run Lighthouse audit (aim for 90+ scores)
- Configure proper cache headers
- Set up error tracking (Sentry, LogRocket)
- Enable analytics (Google Analytics, Plausible)
Troubleshooting
Routes 404 after deployment
Solution: Configure your hosting provider to redirect all routes to
index.html for SPA routing.- Vercel/Netlify: Automatic
- Nginx: Add
try_files $uri /index.html - Apache: Use
.htaccesswithRewriteRule
Assets not loading (404)
Solution: Check the
base option in vite.config.ts. For subdirectory deployments:Large bundle size
Solution:
- Use dynamic imports for heavy dependencies
- Remove unused UI components from
src/components/ui/ - Check for duplicate dependencies with
npm dedupe - Enable tree-shaking for libraries
Environment variables not working
Solution:
- Prefix all variables with
VITE_ - Restart dev server after changing
.env - Use
import.meta.env.VITE_*, notprocess.env.*
Next Steps
Local Setup
Return to development environment setup
Project Structure
Review the codebase organization