vite 优化核心是限监听路径、精简 tsconfig 包含范围、启用 eslint 缓存;需配置 checker 插件 watchpath 为 './src',server.watch.ignored 排除 node_modules/dist/.git,tsconfig.include 限定 src,eslint 启用 --cache 与 content 策略。

Vite 本身不因文件变化“重启”,但频繁触发类型检查、ESLint 校验或 HMR 失效,常被误认为“重启”。真正需要优化的是文件监听范围和检查触发逻辑,避免非关键路径变动引发无意义的检查开销。
只监听 src 目录,排除 node_modules 和构建产物
Vite 默认会监听整个项目目录(包括 node_modules、.git、dist 等),而 vite-plugin-checker(如 ESLint、TypeScript 检查)若未限制路径,也会响应这些无关变更。
正确做法是:显式指定 watchPath 或通过 lintCommand 限定扫描范围。
- 在
vite.config.ts中配置checker插件时,设置watchPath:
import { checker } from 'vite-plugin-checker'
export default defineConfig({
plugins: [
checker({
eslint: {
lintCommand: 'eslint "./src/**/*.{ts,tsx,vue}"',
watchPath: './src', // ✅ 只监听 src 下文件
},
typescript: {
tsconfigPath: './tsconfig.json',
// TypeScript 检查默认也受 Vite 的 server.watch 配置影响
}
})
],
})
- 同时建议补充 Vite 自身的
server.watch配置,进一步收紧监听:
server: {
watch: {
ignored: ['**/node_modules/**', '**/dist/**', '**/.git/**'],
}
}
这样可防止 node_modules 内部更新、pnpm 链接变动、Git 提交等行为意外触发检查。
关闭不必要的类型检查监听(TS Server 优化)
Vite 开发时默认启用 tsc --noEmit 类型检查,但该检查由 TypeScript Language Server 托管,其监听行为受 tsconfig.json 中 include / exclude 控制。
确保 tsconfig.json 不包含宽泛路径:
{
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "types"]
}
⚠️ 避免写 "include": ["."] 或 "include": ["**/*"] —— 这会让 TS Server 监听整个项目,显著拖慢响应。
禁用开发时自动类型检查(按需开启)
如果你主要依赖 IDE(如 VS Code)做实时类型提示,可关闭 Vite 的内置类型检查,减少 CPU 占用:
export default defineConfig({
plugins: [
checker({
typescript: false, // ❌ 关闭 TS 检查(仅保留 ESLint)
// 或保留但设为仅保存时检查:
// typescript: { overlay: { initialIsOpen: false } }
})
]
})
配合 VS Code 的 TypeScript: Auto Fix on Save 和 ESLint 插件,体验更轻量且精准。
ESLint 缓存 + 内容感知校验
即使监听范围收窄,重复保存同一文件仍可能触发全量检查。启用缓存能跳过未改动文件:
checker({
eslint: {
lintCommand: 'eslint "./src/**/*.{ts,tsx}" --cache --cache-location .eslintcache',
dev: {
overrideConfig: {
cache: true,
cacheLocation: '.eslintcache',
cacheStrategy: 'content' // ✅ 基于文件内容哈希判断是否需重检
}
}
}
})
这样,改一行代码才重新检查该文件,而非每次保存都扫一遍 src/ 下所有 .ts 文件。
不复杂但容易忽略。核心就三点:限监听路径、精简 tsconfig 包含范围、开缓存。做完后,编辑 README.md 或 package.json 就再也不会触发检查了。










