vite
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVite
Vite
Overview
概述
Vite is a next-generation frontend build tool that provides instant dev server start via native ES modules and optimized production builds via Rollup. It supports TypeScript, JSX, CSS preprocessors, and static assets out of the box with zero configuration.
When to use: Single-page apps, multi-page apps, library publishing, SSR applications, monorepo packages, any modern frontend project needing fast dev feedback and optimized builds.
When NOT to use: Legacy browsers requiring ES5 output without transpilation, projects locked to Webpack-specific loaders with no Vite equivalents, non-JavaScript build pipelines.
Vite是下一代前端构建工具,通过原生ES模块实现即时开发服务器启动,并通过Rollup实现优化的生产构建。它开箱即用地支持TypeScript、JSX、CSS预处理器和静态资源,无需任何配置。
适用场景: 单页面应用、多页面应用、库发布、SSR应用、单仓库多包项目(monorepo)、任何需要快速开发反馈和优化构建的现代前端项目。
不适用场景: 需要ES5输出且无需转译的旧版浏览器项目、依赖Webpack特定加载器且无Vite替代方案的项目、非JavaScript构建流水线的项目。
Quick Reference
快速参考
| Pattern | API | Key Points |
|---|---|---|
| Config file | | Type-safe config with IDE support |
| Conditional config | | Different config per command/mode |
| Path alias | | Map |
| Dev proxy | | Forward API requests to backend |
| HMR config | | WebSocket host/port/protocol |
| HTTPS dev | | Pass TLS cert/key options |
| Build target | | ES module target for output |
| Manual chunks | | Control code splitting |
| Library mode | | Publish ES/CJS/UMD packages |
| SSR build | | Server-side rendering config |
| Env variables | | Client-exposed env vars |
| loadEnv | | Load env vars in config |
| CSS modules | | Scoped CSS class names |
| Preprocessors | | Sass/Less/Stylus options |
| PostCSS | | Inline or external PostCSS config |
| Static assets | | Returns resolved public URL |
| Plugin | | Hook-based plugin system |
| Virtual module | | Generate modules at build time |
| Multi-page | | Multiple HTML entry points |
| 模式 | API | 关键点 |
|---|---|---|
| 配置文件 | | 类型安全的配置,支持IDE提示 |
| 条件配置 | | 根据命令/模式使用不同的配置 |
| 路径别名 | | 将 |
| 开发服务器代理 | | 将API请求转发到后端 |
| HMR配置 | | WebSocket主机/端口/协议设置 |
| HTTPS开发服务器 | | 传入TLS证书/密钥选项 |
| 构建目标 | | 输出的ES模块版本目标 |
| 手动分包 | | 控制代码分割逻辑 |
| 库模式 | | 发布ES/CJS/UMD格式的包 |
| SSR构建 | | 服务端渲染配置 |
| 环境变量 | | 客户端可访问的环境变量 |
| loadEnv函数 | | 在配置文件中加载环境变量 |
| CSS Modules | | 作用域CSS类名 |
| 预处理器 | | Sass/Less/Stylus的配置选项 |
| PostCSS | | 内联或外部PostCSS配置 |
| 静态资源 | | 返回解析后的公共URL |
| 插件 | | 基于钩子的插件系统 |
| 虚拟模块 | | 构建时生成模块 |
| 多页面应用 | | 多个HTML入口文件 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Exposing secrets via | Only prefix client-safe vars with |
Using | Use |
Modifying | Always use |
| Not externalizing peer deps in library mode | Add React/Vue to |
| Creating QueryClient-style singletons in SSR | Ensure per-request state in SSR to avoid cross-request leaks |
Inline PostCSS config alongside | Use one or the other; inline config disables config file search |
Setting | |
Using | Use |
| 错误内容 | 正确做法 |
|---|---|
通过 | 仅对客户端安全的变量使用 |
在客户端代码中使用 | 使用 |
修改 | 始终使用 |
| 在库模式下未外部化peer依赖 | 将React/Vue添加到 |
| 在SSR中创建QueryClient风格的单例对象 | 在SSR中确保每个请求拥有独立状态,避免跨请求数据泄漏 |
同时使用内联PostCSS配置和 | 二选一;内联配置会禁用配置文件的自动搜索 |
设置 | |
在ESM配置文件中使用 | 使用 |
Delegation
任务委托
- Plugin discovery: Use agent
Explore - Build analysis: Use agent
Task - Config review: Delegate to agent
code-reviewer
If theskill is available, delegate test configuration and Vitest setup to it. If thevitest-testingskill is available, delegate Tailwind CSS configuration and utility patterns to it. If thetailwindskill is available, delegate React component patterns and hooks to it. If thereact-patternsskill is available, delegate TypeScript configuration and type patterns to it. If thetypescript-patternsskill is available, delegate monorepo workspace configuration to it.pnpm-workspace
- 插件探索:使用Agent
Explore - 构建分析:使用Agent
Task - 配置审核:委托给Agent
code-reviewer
如果技能可用,将测试配置和Vitest设置委托给它。 如果vitest-testing技能可用,将Tailwind CSS配置和工具类模式委托给它。 如果tailwind技能可用,将React组件模式和Hooks委托给它。 如果react-patterns技能可用,将TypeScript配置和类型模式委托给它。 如果typescript-patterns技能可用,将单仓库多包(monorepo)工作区配置委托给它。pnpm-workspace
References
参考资料
- Configuration fundamentals and defineConfig patterns
- Plugin authoring and popular plugins
- Dev server setup: proxy, HMR, HTTPS, and middleware
- Build optimization: chunking, tree-shaking, and output control
- Library mode for publishing packages
- SSR configuration and Express integration
- Environment variables and .env file handling
- CSS handling: PostCSS, CSS modules, preprocessors, and asset management
- 配置基础与defineConfig模式
- 插件开发与热门插件
- 开发服务器设置:代理、HMR、HTTPS与中间件
- 构建优化:代码分割、Tree-shaking与输出控制
- 用于发布包的库模式
- SSR配置与Express集成
- 环境变量与.env文件处理
- CSS处理:PostCSS、CSS Modules、预处理器与资源管理