Vite is a modern frontend build tool that provides fast development and optimized production builds. It leverages native ES modules and supports frameworks like Vue, React, and Svelte.
- Lightning-fast cold starts
- Hot Module Replacement (HMR)
- Out-of-the-box support for TypeScript, JSX, CSS, and more
- Optimized production builds with Rollup
- Install Vite:
npm create vite@latest
- Start Development Server:
npm run dev
- Build for Production:
npm run build
- Preview Production Build:
npm run preview
- Vite uses
vite.config.jsfor project configuration. - Supports plugins for extending functionality.
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
export default defineConfig({
plugins: [vue()],
server: {
port: 3000,
},
});- Use environment variables for configuration.
- Organize source files in
src/. - Leverage Vite plugins for framework support and optimizations.
Vite has a rich plugin ecosystem. You can use official and community plugins or write your own:
import legacy from "@vitejs/plugin-legacy";
export default defineConfig({
plugins: [vue(), legacy()],
});- Prefix with
VITE_to expose to client code. - Use
.env,.env.development,.env.productionfor different environments.
VITE_API_URL=https://api.example.comAccess in code: import.meta.env.VITE_API_URL
Simplify imports with path aliases:
import { defineConfig } from "vite";
import path from "path";
export default defineConfig({
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
},
},
});- Use
--debugflag for verbose logs. - Clear cache with
rm -rf node_modules/.vite. - Check Vite and plugin versions for compatibility.
- Build output is in the
dist/folder. - Deploy to static hosts like Netlify, Vercel, or GitHub Pages.