vite-patterns
它能做什么
这个技能帮助你配置和优化 Vite,一个用于构建 Web 项目的工具。它会讲解 Vite 的工作原理、如何调整其设置,以及如何使用插件添加功能。
适用场景
- ✓配置 `vite.config.ts` 文件时。
- ✓调整 Vite 为生产环境准备项目的方式时。
- ✓排查开发服务器或构建过程中的问题时。
技能链接
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/vite-patterns/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我该如何配置 Vite 使用 API 代理?”
每周 5 个新技能,1 分钟读完。
工作原理
Vite 使用一个快速的开发服务器,按需转换你的代码。在生产环境中,它会使用 Rollup 或 Rolldown 等工具打包代码,优化体积和性能。它还会预构建依赖项,以加快启动速度。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Vite Patterns
Build tool and dev server patterns for Vite 8+ projects. Covers configuration, environment variables, proxy setup, library mode, dependency pre-bundling, and common production pitfalls.
When to Use
- Configuring
vite.config.tsorvite.config.js - Setting up environment variables or
.envfiles - Configuring dev server proxy for API backends
- Optimizing build output (chunks, minification, assets)
- Publishing libraries with
build.lib - Troubleshooting dependency pre-bundling or CJS/ESM interop
- Debugging HMR, dev server, or build errors
- Choosing or ordering Vite plugins
How It Works
- Dev mode serves source files as native ESM — no bundling. Transforms happen on-demand per module request, which is why cold starts are fast and HMR is precise.
- Build mode uses Rolldown (v7+) or Rollup (v5–v6) to bundle the app for production with tree-shaking, code-splitting, and Oxc-based minification.
- Dependency pre-bundling converts CJS/UMD deps to ESM once via esbuild and caches the result under
node_modules/.vite, so subsequent starts skip the work. - Plugins share a unified interface across dev and build — the same plugin object works for both the dev server's on-demand transforms and the production pipeline.
- Environment variables are statically inlined at build time.
VITE_-prefixed vars become public constants in the bundle; everything unprefixed is invisible to client code.
Examples
Config Structure
Basic Config
// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
resolve: {
alias: { '@': new URL('./src', import.meta.url).pathname },
},
})
Conditional Config
// vite.config.ts
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd()) // VITE_ prefixed only (safe)
return {
plugins: [react()],
server: command === 'serve' ? { port: 3000 } : undefined,
define: {
__API_URL__: JSON.stringify(env.VITE_API_URL),
},
}
})
Key Config Options
| Key | Default | Description |
|---|---|---|
root |
'.' |
Project root (where index.html lives) |
base |
'/' |
Public base path for deployed assets |
envPrefix |
'VITE_' |
Prefix for client-exposed env vars |
build.outDir |
'dist' |
Output directory |
build.minify |
'oxc' |
Minifier ('oxc', 'terser', or false) |
build.sourcemap |
false |
true, 'inline', or 'hidden' |
Plugins
Essential Plugins
Most plugin needs are covered by a handful of well-maintained packages. Reach for these before writing your own.
| Plugin | Purpose | When to use |
|---|---|---|
@vitejs/plugin-react-swc |
React HMR + Fast Refresh via SWC | Default for React apps (faster than Babel variant) |
@vitejs/plugin-react |
React HMR + Fast Refresh via Babel | Only if you need Babel plugins (emotion, MobX decorators) |
@vitejs/plugin-vue |
Vue 3 SFC support | Vue apps |
vite-plugin-checker |
Runs tsc + ESLint in worker thread with HMR overlay |
Any TypeScript app — Vite does NOT type-check during vite build |
vite-tsconfig-paths |
Honors tsconfig.json paths aliases |
Any time you already have aliases in tsconfig.json |
vite-plugin-dts |
Emits .d.ts files in library mode |
Publishing TypeScript libraries |
vite-plugin-svgr |
Imports SVGs as React components | React apps using SVGs as components |
rollup-plugin-visualizer |
Bundle treemap/sunburst report | Periodic bundle size audits (use enforce: 'post') |
vite-plugin-pwa |
Zero-config PWA + Workbox | Offline-capable apps |
Critical callout: vite build transpiles but does NOT type-check. Type errors silently ship to production unless you add vite-plugin-checker or run tsc --noEmit in CI.
Authoring Custom Plugins
Authoring is rare — most needs are covered by existing plugins. When you do need one, start inline in vite.config.ts and only extract if reused.
// vite.config.ts — minimal inline plugin
function myPlugin(): Plugin {
return {
name: 'my-plugin', // required, must be unique
enforce: 'pre', // 'pre' | 'post' (optional)
…
常见问题
什么是 HMR?
HMR(热模块替换)可以在浏览器中实时更新代码的部分内容,无需整页刷新,让开发更加快捷。
为什么要在 Vite 中使用插件?
插件可以扩展 Vite 的功能,让你添加 React 或 Vue 支持、图片优化,或者 PWA(渐进式 Web 应用)集成等能力。
Vite 如何处理环境变量?
以 `VITE_` 为前缀的环境变量会被包含在项目代码中,供前端使用。其他变量出于安全考虑不会暴露出来。
详情
作者的原始描述
Vite build tool patterns including config, plugins, HMR, env variables, proxy setup, SSR, library mode, dependency pre-bundling, and build optimization. Activate when working with vite.config.ts, Vite plugins, or Vite-based projects.
repo: affaan-m/ECC
热度
- GitHub 星标
- 280k
- 排名
- #256
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
accessibility
280k帮你的智能体创建人人都能使用的界面。确保网站和应用对残障人士友好,比如失明或行动不便的用户也能顺畅使用。
agent-architecture-audit
280k让你的智能体检查一切是否正常运行。它能找出并修复 AI 系统中的问题,确保系统按预期工作。
agent-eval
280k帮你的智能体比较不同的 AI 编程工具。它能衡量哪个工具在特定任务上更快、更便宜、更可靠。
agent-harness-construction
280k让你的智能体更好地理解如何完成任务。它会优化智能体使用的工具和指令,提高成功的几率。