Vite
Vite 6+ frontend build tool — native ESM dev server + optimized production builds.
Workflow
- Configure —
vite.config.tswithdefineConfig. Always use TypeScript + ESM. - Develop —
vitestarts dev server with instant HMR - Build —
vite buildfor production,vite previewto test output locally - Deploy — output in
dist/, setbaseoption for non-root paths (e.g. GitHub Pages)
Config Example
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
resolve: { alias: { '@': '/src' } },
server: {
port: 3000,
proxy: { '/api': 'http://localhost:8080' },
},
build: {
target: 'esnext',
outDir: 'dist',
},
})
Use defineConfig with a function for environment-conditional config:
export default defineConfig(({ command, mode }) => ({
plugins: [react()],
define: {
__DEV__: command === 'serve',
},
}))
Key Features
Glob imports — dynamically load matching modules:
const modules = import.meta.glob('./modules/*.ts')
// eager: true loads all immediately instead of lazy
const eager = import.meta.glob('./locale/*.json', { eager: true })
Asset queries — import assets with transform hints:
import url from './img.png?url' // resolves to public URL string
import raw from './shader.glsl?raw' // resolves to file contents as string
import worker from './worker.ts?worker' // wraps as Web Worker
Env variables — only VITE_-prefixed vars are exposed to client code:
// .env
VITE_API_URL=https://api.example.com
SECRET_KEY=do-not-expose // NOT accessible in client
// in source
const url = import.meta.env.VITE_API_URL
CSS Modules — auto-enabled for .module.css files:
import styles from './app.module.css'
// styles.container → scoped class name
Plugin Authoring
Vite plugins extend Rollup's plugin interface with additional Vite-specific hooks:
import type { Plugin } from 'vite'
function myPlugin(): Plugin {
return {
name: 'my-plugin',
// Vite hook: runs before Rollup
configResolved(config) {
console.log('resolved base:', config.base)
},
// Rollup hook: transform source files
transform(code, id) {
if (!id.endsWith('.custom')) return
return { code: transformCustom(code), map: null }
},
}
}
Plugin ordering: enforce: 'pre' runs before framework plugins, enforce: 'post' runs after.
SSR
vite build --ssr src/entry-server.ts # build server bundle
vite build # build client bundle separately
In dev, use server.ssrLoadModule to load modules through Vite's pipeline:
const { render } = await viteDevServer.ssrLoadModule('/src/entry-server.ts')
Official Plugins
@vitejs/plugin-vue— Vue 3 SFC support@vitejs/plugin-react— React with Babel/Oxc (Vite 8+)@vitejs/plugin-react-swc— React with SWC (faster transforms)@vitejs/plugin-legacy— transpile for older browsers with@babel/preset-env
Pitfalls
process.envdoes not work in client code — useimport.meta.envinstead- Forgetting the
VITE_prefix on env vars — they will beundefinedin the browser with no warning - CJS-only dependencies fail at runtime in the browser — add them to
optimizeDeps.includeso Vite pre-bundles them as ESM - HMR not triggering for a module — the module needs to call
import.meta.hot.accept()or have an ancestor that does; otherwise Vite does a full reload - Large glob imports without
{ eager: false }(the default) add lazy dynamic imports; witheager: truethey all land in the initial bundle