Build Process
Villa Buena E-Commerce uses Vite as its build tool, which provides an optimized production build with code splitting, minification, and asset optimization.Build Commands
The following npm scripts are available for building and previewing the application:Production Build
vite build and performs the following optimizations:
- Bundles JavaScript modules with Rollup
- Minifies JavaScript and CSS
- Optimizes and fingerprints assets for caching
- Generates source maps for debugging
- Code-splits for optimal loading performance
- Tree-shakes unused code
The build output is generated in the
dist/ directory by default.Preview Production Build
- Serves the
dist/directory - Simulates production environment
- Useful for testing before deployment
Build Configuration
The build process is configured invite.config.js:
Current Configuration
- Plugin:
@vitejs/plugin-reactfor React Fast Refresh and JSX transformation - Build Target: Modern browsers (ES2020+)
- Output Directory:
dist/(Vite default)
Build Output Structure
After runningnpm run build, the dist/ directory will contain:
Optimization Features
Automatic Code Splitting
Vite automatically splits code based on:- Dynamic imports: Routes and components loaded on demand
- Vendor chunks: Third-party dependencies bundled separately
- Common modules: Shared code extracted to reduce duplication
Asset Optimization
- Images: Automatically optimized and fingerprinted
- CSS: Minified and purged of unused styles
- JavaScript: Minified with terser
- Dependencies: Analyzed for optimal chunking
Tree Shaking
Vite’s build process includes tree shaking to eliminate dead code:- Removes unused exports from modules
- Optimizes bundle size
- Works best with ES modules
The project uses
"type": "module" in package.json, which ensures optimal tree shaking.Environment-Specific Builds
Production Environment Variables
Create a.env.production file for production-specific configuration:
Build Modes
Vite supports custom build modes:.env.staging or .env.custom files for each mode.
Pre-Build Checklist
Verify environment variables
Check that all required environment variables are set in
.env.production:VITE_API_URL- Production API endpoint- Any Auth0 variables if migrated to env vars
Build Optimization Tips
Analyze Bundle Size
To analyze what’s included in your build:vite.config.js:
Lazy Load Routes
Implement route-based code splitting for better performance:Optimize Dependencies
Consider the size of dependencies:- Bootstrap 5.3.8 - Import only needed components
- Lucide React - Use tree-shakeable imports:
import { ShoppingCart } from 'lucide-react'
Deployment
Static File Hosting
Thedist/ directory can be deployed to any static hosting service:
Vercel
Netlify
AWS S3
GitHub Pages
Configure base path in vite.config.js and deploy dist/ to gh-pages branch
Server Configuration
Since this is a Single Page Application (SPA) with client-side routing, configure your server to:-
Serve index.html for all routes:
-
Set proper cache headers:
- Long cache for hashed assets:
Cache-Control: max-age=31536000, immutable - No cache for index.html:
Cache-Control: no-cache
- Long cache for hashed assets:
Troubleshooting
Build fails with memory errors
Increase Node.js memory limit:Assets not loading after deployment
- Check the base path configuration in
vite.config.js - Verify asset paths are relative, not absolute
- Ensure your CDN/server is serving all files from
dist/
Environment variables not working
- Rebuild after changing
.env.production - Verify variables are prefixed with
VITE_ - Check that variables are accessed via
import.meta.env, notprocess.env
