vite-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVite
Vite
Based on Vite 8.0 stable (March 2026). Vite 8 uses Rolldown bundler and Oxc transformer, replacing esbuild + Rollup.
Vite is a next-generation frontend build tool with fast dev server (native ESM + HMR) and optimized production builds powered by Rolldown.
基于Vite 8.0稳定版(2026年3月)。Vite 8采用Rolldown打包器和Oxc转换器,替代了原有的esbuild + Rollup组合。
Vite是下一代前端构建工具,拥有快速的开发服务器(原生ESM + HMR)和基于Rolldown优化的生产构建能力。
Key Vite 8 Changes
Vite 8主要变更
Vite 8 replaces the dual esbuild+Rollup architecture with Rolldown (unified Rust-based bundler) and Oxc (transformer/minifier). A compatibility layer auto-converts old and configs, but both are deprecated — always use the new names in new code:
esbuildrollupOptions| Deprecated (still works) | Replacement |
|---|---|
| |
| |
| |
| |
Vite 8用Rolldown(基于Rust的统一打包器)和Oxc(转换器/压缩器)替代了原有的esbuild+Rollup双架构。兼容层会自动转换旧的和配置,但两者均已被弃用——在新代码中请务必使用新名称:
esbuildrollupOptions| 已弃用(仍可使用) | 替代方案 |
|---|---|
| |
| |
| |
| |
Preferences
推荐实践
- Use TypeScript: prefer
vite.config.ts - Always use ESM — avoid CommonJS
- Use (ESM) not
import.meta.dirname(CJS) in config files__dirname - Use not
rolldownOptionsin new coderollupOptions - Use not
oxcin new codeesbuild
- 使用TypeScript:优先选择
vite.config.ts - 始终使用ESM——避免CommonJS
- 在配置文件中使用(ESM)而非
import.meta.dirname(CJS)__dirname - 在新代码中使用而非
rolldownOptionsrollupOptions - 在新代码中使用而非
oxcesbuild
Core
核心内容
| Topic | Description | Reference |
|---|---|---|
| Configuration | | core-config |
| Features | | core-features |
| Plugin API | Vite/Rolldown hooks, virtual modules, hook filters, plugin ordering | core-plugin-api |
| 主题 | 描述 | 参考文档 |
|---|---|---|
| 配置 | | core-config |
| 特性 | | core-features |
| 插件API | Vite/Rolldown钩子、虚拟模块、钩子过滤器、插件排序 | core-plugin-api |
Build & SSR
构建与SSR
| Topic | Description | Reference |
|---|---|---|
| Build & SSR | Library mode, SSR middleware, | build-and-ssr |
| 主题 | 描述 | 参考文档 |
|---|---|---|
| 构建与SSR | 库模式、SSR中间件、 | build-and-ssr |
Advanced
进阶内容
| Topic | Description | Reference |
|---|---|---|
| Environment API | Vite 6+ multi-environment support, custom runtimes | environment-api |
| Rolldown Migration | Vite 8 migration: complete esbuild→oxc and rollupOptions→rolldownOptions mapping, breaking changes | rolldown-migration |
| 主题 | 描述 | 参考文档 |
|---|---|---|
| 环境API | Vite 6+多环境支持、自定义运行时 | environment-api |
| Rolldown迁移 | Vite 8迁移:完整的esbuild→oxc和rollupOptions→rolldownOptions映射、破坏性变更 | rolldown-migration |
Quick Reference
快速参考
CLI Commands
CLI命令
bash
vite # Start dev server
vite build # Production build
vite preview # Preview production build
vite build --ssr # SSR buildbash
vite # 启动开发服务器
vite build # 生产构建
vite preview # 预览生产构建产物
vite build --ssr # SSR构建Common Config (Vite 8)
常见配置(Vite 8)
ts
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [],
resolve: {
alias: { '@': '/src' },
tsconfigPaths: true, // New in v8: auto-resolve TS path aliases
},
server: {
port: 3000,
proxy: { '/api': 'http://localhost:8080' },
forwardConsole: true, // New in v8: browser logs → terminal
},
build: {
target: 'esnext',
outDir: 'dist',
rolldownOptions: {}, // NOT rollupOptions
},
oxc: { // NOT esbuild
jsx: {
runtime: 'automatic',
importSource: 'react',
},
},
})ts
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [],
resolve: {
alias: { '@': '/src' },
tsconfigPaths: true, // v8新增:自动解析TS路径别名
},
server: {
port: 3000,
proxy: { '/api': 'http://localhost:8080' },
forwardConsole: true, // v8新增:浏览器日志转发至终端
},
build: {
target: 'esnext',
outDir: 'dist',
rolldownOptions: {}, // 请勿使用rollupOptions
},
oxc: { // 请勿使用esbuild
jsx: {
runtime: 'automatic',
importSource: 'react',
},
},
})Official Plugins
官方插件
- v6 — React with Oxc transforms (Babel removed)
@vitejs/plugin-react - — React with SWC
@vitejs/plugin-react-swc - — Vue 3 SFC support
@vitejs/plugin-vue - — Vue 3 JSX
@vitejs/plugin-vue-jsx - — Legacy browser support
@vitejs/plugin-legacy
- v6 — 基于Oxc转换的React支持(已移除Babel)
@vitejs/plugin-react - — 基于SWC的React支持
@vitejs/plugin-react-swc - — Vue 3单文件组件支持
@vitejs/plugin-vue - — Vue 3 JSX支持
@vitejs/plugin-vue-jsx - — 旧版浏览器支持
@vitejs/plugin-legacy
Oxc JSX Quick Reference
Oxc JSX快速参考
ts
// React (automatic runtime — default)
oxc: { jsx: { runtime: 'automatic', importSource: 'react' } }
// Preact (automatic)
oxc: { jsx: { runtime: 'automatic', importSource: 'preact' } }
// Preact (classic with h/Fragment)
oxc: { jsx: { runtime: 'classic', pragma: 'h', pragmaFrag: 'Fragment' } }
// Auto-inject React import (legacy patterns)
oxc: { jsxInject: `import React from 'react'` }ts
// React(自动运行时——默认)
oxc: { jsx: { runtime: 'automatic', importSource: 'react' } }
// Preact(自动运行时)
oxc: { jsx: { runtime: 'automatic', importSource: 'preact' } }
// Preact(经典运行时,使用h/Fragment)
oxc: { jsx: { runtime: 'classic', pragma: 'h', pragmaFrag: 'Fragment' } }
// 自动注入React导入(旧模式)
oxc: { jsxInject: `import React from 'react'` }