文章目錄
- 前言
- ? 1. ESLint 限制
- 🔧 配置位置:
- ? 啟用了哪些規則(核心):
- 📦 使用的插件和標準:
- ? 2. TSC 編譯限制
- 關鍵選項:
- ? 3. Git Hook 校驗工具鏈
- 配置例子(`package.json`):
- ? 總結
前言
Vue 3 官方項目對 eslint
和 tsc
的嚴格限制,主要是通過 集中統一的配置文件 + Git Hook 工具鏈(如 lint-staged、simple-git-hooks、husky) 實現的,具體如下:
? 1. ESLint 限制
🔧 配置位置:
.eslintrc.cjs
? 啟用了哪些規則(核心):
rules: {'no-console': ['error', { allow: ['warn', 'error', 'info'] }],'no-debugger': 'error',...
}
解釋:
- 禁止所有
console.*
,僅保留warn
,error
,info
- 提交前如果不符合規則,Git 會拒絕提交(通過 pre-commit hook)
📦 使用的插件和標準:
eslint
eslint-plugin-vue
@vue/eslint-config-prettier
@vue/eslint-config-typescript
? 2. TSC 編譯限制
Vue 項目結構嚴格使用了:
tsconfig.json
tsconfig.base.json
tsconfig.config.json
關鍵選項:
{"compilerOptions": {"strict": true,"declaration": true,"isolatedModules": true,"noImplicitAny": true,"noUnusedLocals": true,"noEmitOnError": true}
}
isolatedModules: true
:必須為所有export function
添加返回類型(你遇到的)noEmitOnError: true
:類型報錯時拒絕生成.d.ts
文件declaration: true
:生成.d.ts
,用于包發布和 IDE 智能提示
? 3. Git Hook 校驗工具鏈
官方用的通常是:
simple-git-hooks
lint-staged
配置例子(package.json
):
"simple-git-hooks": {"pre-commit": "lint-staged"
},
"lint-staged": {"*.{ts,js,json}": ["eslint --fix","prettier --write"]
}
🔁 你執行 git commit
時,它自動執行:
eslint --fix
prettier --write
- 拒絕不符合標準的代碼提交(你看到的就是這個)
? 總結
限制機制 | 實現方式 | 示例 |
---|---|---|
ESLint | .eslintrc.cjs 中的規則設置 | no-console , no-debugger 等 |
TypeScript | tsconfig.json 嚴格模式 | isolatedModules , noEmitOnError |
Git 鉤子 | simple-git-hooks + lint-staged | 阻止格式錯誤代碼提交 |