2024最新Vue3+TypeScript项目配置代码提交规范
书接上回
Vue3+ TypeScript中如何使用Eslint V9.0.0
这一篇文章中使用的Eslint V9.0.0最新版本和typescript-eslint V7.6.0存在版本兼容性问题,在拉取项目时候执行pnpm i
,提示如下错误:
- 解决办法(一):执行
npm i --force
,跳过版本兼容,强制拉包 - (推荐)解决办法(二):把typescript-eslint改成v7.7.0版本,如下:
typescript-eslint开源社团也在积极的升级拥抱Eslint v9,更新的很快:
husky v9
作用
husky 是一个 Git 钩子工具,可以在提交代码时自动检测到代码提交时修改的文件,然后执行相应的检查命令。
核心内容是配置 Husky 的 pre-commit 和 commit-msg 两个钩子:
- pre-commit:Husky + Lint-staged 整合实现 Git 提交前代码规范检测/格式化 (前提:ESlint + Prettier + Stylelint 代码统一规范。
- commit-msg: Husky + Commitlint + Commitizen + cz-git 整合实现生成规范化且高度自定义的 Git commit message。
安装
我们husky v9版本,它和v8安装过程不太一样,大家要注意!husky v9与v8区别
pnpm install husky -D
npx husky init
这命令做了四件事儿:
- 安装 husky 到开发依赖
- 在项目根目录下创建 .husky 目录
- 在 .husky 目录创建 pre-commit hook,并初始化 pre-commit 命令为 npm test
- 修改 package.json 的 scripts,增加 "prepare": "husky"
配置
当我们安装lint-staged之后再配置
使用
pnpm prepare
lint-staged
作用
lint-staged 也是一个 Git 钩子工具,当我们在 git add 到暂存区的文件运行 linters (ESLint/Prettier/StyleLint) 的工具,避免在 git commit 提交时在整个项目执行。
安装
pnpm install -D lint-staged
配置
- 在package.json配置中添加:
"lint-staged": {
"*.{js,ts}": [
"eslint --fix",
"prettier --write"
],
"*.{cjs,json}": [
"prettier --write"
],
"*.{vue,html}": [
"eslint --fix",
"prettier --write",
"stylelint --fix"
],
"*.{scss,css}": [
"stylelint --fix",
"prettier --write"
],
"*.md": [
"prettier --write"
]
}
- 在scripts添加lint-staged命令:
"lint:lint-staged": "lint-staged"
- 修改husky提交前前钩子命令: 根目录 .husky 目录下 pre-commit 文件中的 npm test 修改为 npm run lint:lint-staged
使用
Git提交代码检测
git add .
git commit -m "测试"
Commitlint
作用
commitlint 是一个 git commit 校验约束工具,检查您的提交消息是否符合 Conventional commit format
安装
pnpm install @commitlint/config-conventional @commitlint/cli --save-dev
pnpm install -D commitizen
配置
- 根目录创建 commitlint.config.cjs 配置文件,示例配置:
// commitlint.config.cjs
module.exports = {
// 继承的规则
extends: ['@commitlint/config-conventional'],
// 自定义规则
rules: {
// @see https://commitlint.js.org/#/reference-rules
// 提交类型枚举,git提交type必须是以下类型
'type-enum': [
2,
'always',
[
'feat', // 新增功能
'fix', // 修复缺陷
'docs', // 文档变更
'style', // 代码格式(不影响功能,例如空格、分号等格式修正)
'refactor', // 代码重构(不包括 bug 修复、功能新增)
'perf', // 性能优化
'test', // 添加疏漏测试或已有测试改动
'build', // 构建流程、外部依赖变更(如升级 npm 包、修改 webpack 配置等)
'ci', // 修改 CI 配置、脚本
'revert', // 回滚 commit
'chore', // 对构建过程或辅助工具和库的更改(不影响源文件、测试用例)
],
],
'subject-case': [0], // subject大小写不做校验
},
prompt: {
messages: {
type: '选择你要提交的类型 :',
scope: '选择一个提交范围(可选):',
customScope: '请输入自定义的提交范围 :',
subject: '填写简短精炼的变更描述 :\n',
body: '填写更加详细的变更描述(可选)。使用 "|" 换行 :\n',
breaking: '列举非兼容性重大的变更(可选)。使用 "|" 换行 :\n',
footerPrefixesSelect: '选择关联issue前缀(可选):',
customFooterPrefix: '输入自定义issue前缀 :',
footer: '列举关联issue (可选) 例如: #31, #I3244 :\n',
generatingByAI: '正在通过 AI 生成你的提交简短描述...',
generatedSelectByAI: '选择一个 AI 生成的简短描述:',
confirmCommit: '是否提交或修改commit ?',
},
// prettier-ignore
types: [
{ value: "feat", name: "特性: ✨ 新增功能", emoji: ":sparkles:" },
{ value: "fix", name: "修复: 🐛 修复缺陷", emoji: ":bug:" },
{ value: "docs", name: "文档: 📝 文档变更", emoji: ":memo:" },
{ value: "style", name: "格式: 🌈 代码格式(不影响功能,例如空格、分号等格式修正)", emoji: ":lipstick:" },
{ value: "refactor", name: "重构: 🔄 代码重构(不包括 bug 修复、功能新增)", emoji: ":recycle:" },
{ value: "perf", name: "性能: 🚀 性能优化", emoji: ":zap:" },
{ value: "test", name: "测试: 🧪 添加疏漏测试或已有测试改动", emoji: ":white_check_mark:" },
{ value: "build", name: "构建: 📦️ 构建流程、外部依赖变更(如升级 npm 包、修改 vite 配置等)", emoji: ":package:" },
{ value: "ci", name: "集成: ⚙️ 修改 CI 配置、脚本", emoji: ":ferris_wheel:" },
{ value: "revert", name: "回退: ↩️ 回滚 commit", emoji: ":rewind:" },
{ value: "chore", name: "其他: 🛠️ 对构建过程或辅助工具和库的更改(不影响源文件、测试用例)", emoji: ":hammer:" },
],
useEmoji: true,
emojiAlign: 'center',
useAI: false,
aiNumber: 1,
themeColorCode: '',
scopes: [],
allowCustomScopes: true,
allowEmptyScopes: true,
customScopesAlign: 'bottom',
customScopesAlias: 'custom',
emptyScopesAlias: 'empty',
upperCaseSubject: false,
markBreakingChangeMode: false,
allowBreakingChanges: ['feat', 'fix'],
breaklineNumber: 100,
breaklineChar: '|',
skipQuestions: [],
issuePrefixes: [{ value: 'closed', name: 'closed: ISSUES has been processed' }],
customIssuePrefixAlign: 'top',
emptyIssuePrefixAlias: 'skip',
customIssuePrefixAlias: 'custom',
allowCustomIssuePrefix: true,
allowEmptyIssuePrefix: true,
confirmColorize: true,
maxHeaderLength: Infinity,
maxSubjectLength: Infinity,
minSubjectLength: 0,
scopeOverrides: undefined,
defaultBody: '',
defaultIssues: '',
defaultScope: '',
defaultSubject: '',
},
}
- 执行下面命令生成 commint-msg 钩子用于 git 提交信息校验
echo "npx --no -- commitlint --edit $1" > .husky/commit-msg
cz-git
作用
与commitizen搭配使用,是一款工程性更强,轻量级,高度自定义,标准输出格式的 commitizen 适配器。
安装
pnpm install -D cz-git
配置
- 在package.json配置中添加:
"config": {
"commitizen": {
"path": "node_modules/cz-git"
}
},
- 在script添加提交指令
"commit": "git pull && git add -A && git-cz && git push"
使用
pnpm commit
成果展示
代码仓库
转载自:https://juejin.cn/post/7358352353295335464