本教程将指导你如何在Vue项目中集成一个规范化的Git提交流程,包括代码规范检查、提交信息规范和自动化工具配置。
一个完整的Git提交流程通常包含以下环节:
这样的流程可以帮助团队保持一致的代码风格和提交历史,提高协作效率。
首先,我们需要安装一系列开发依赖:
# 使用npm
npm install --save-dev eslint prettier @vue/eslint-config-prettier husky lint-staged @commitlint/cli @commitlint/config-conventional commitizen cz-conventional-changelog conventional-changelog-cli
# 或使用yarn
yarn add --dev eslint prettier @vue/eslint-config-prettier husky lint-staged @commitlint/cli @commitlint/config-conventional commitizen cz-conventional-changelog conventional-changelog-cli
# 或使用pnpm
pnpm add --save-dev eslint prettier @vue/eslint-config-prettier husky lint-staged @commitlint/cli @commitlint/config-conventional commitizen cz-conventional-changelog conventional-changelog-cli
在项目根目录创建.editorconfig
文件,确保在不同编辑器中保持一致的编码风格:
# .editorconfig
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
如果你的Vue项目还没有ESLint配置,在项目根目录创建.eslintrc.js
文件:
// .eslintrc.js
module.exports = {
root: true,
env: {
node: true,
browser: true,
},
extends: [
'plugin:vue/vue3-recommended', // 或 'plugin:vue/recommended' 用于Vue 2
'eslint:recommended',
'@vue/prettier',
],
parserOptions: {
ecmaVersion: 2020,
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
},
};
创建.prettierrc
文件:
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 100,
"bracketSpacing": true,
"arrowParens": "avoid"
}
同时创建.prettierignore
忽略某些文件:
/dist
/node_modules
Husky用于设置Git hooks,在关键Git操作前执行脚本。
npx husky install
在package.json
中添加一个脚本,确保其他开发者在安装依赖后自动启用husky:
{
"scripts": {
"prepare": "husky install"
}
}
npx husky add .husky/pre-commit "npx lint-staged"
npx husky add .husky/commit-msg "npx --no -- commitlint --edit $1"
在package.json
中添加lint-staged配置:
{
"lint-staged": {
"*.{js,vue,ts,jsx,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{css,scss,less,styl}": [
"prettier --write"
],
"*.{json,md}": [
"prettier --write"
]
}
}
或者创建单独的.lintstagedrc.js
文件:
// .lintstagedrc.js
module.exports = {
'*.{js,vue,ts,jsx,tsx}': [
'eslint --fix',
'prettier --write',
],
'*.{css,scss,less,styl}': [
'prettier --write',
],
'*.{json,md}': [
'prettier --write',
],
};
创建commitlint.config.js
文件:
// commitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'feat', // 新功能
'fix', // 修复Bug
'docs', // 文档变更
'style', // 代码格式(不影响代码运行的变动)
'refactor', // 重构(既不是新增功能,也不是修改bug的代码变动)
'perf', // 性能优化
'test', // 增加测试
'chore', // 构建过程或辅助工具的变动
'revert', // 回滚到上一个版本
'build', // 打包
'ci', // CI相关变更
],
],
'type-case': [2, 'always', 'lower'],
'type-empty': [2, 'never'],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
'header-max-length': [2, 'always', 72],
},
};
Commitizen可以帮助开发者创建符合约定的提交信息。
在package.json
中添加:
{
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
}
}
在package.json
中添加:
{
"scripts": {
"commit": "cz"
}
}
这样开发者可以使用npm run commit
(或yarn commit
、pnpm commit
)来代替git commit
进行提交。
用于自动生成变更日志。
在package.json
中添加脚本:
{
"scripts": {
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"changelog:init": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0"
}
}
changelog
会在现有CHANGELOG.md上追加最新版本信息changelog:init
会重新生成整个CHANGELOG.md下面是一个典型的开发和提交流程:
编写代码:开发新功能或修复bug
暂存更改:git add .
规范提交:npm run commit
(而不是git commit
)
推送代码:git push
版本发布前:npm run changelog
生成新版本的变更日志
确保husky正确安装并且文件有执行权限:
chmod +x .husky/pre-commit
chmod +x .husky/commit-msg
可以先使用--fix
选项批量修复:
npx eslint --fix src/
可以使用--no-verify
参数:
git commit -m "紧急修复" --no-verify
但请注意,这应该只用于特殊情况。
修改commitlint.config.js
中的type-enum
规则,添加你需要的类型。
默认配置支持中文提交信息,无需额外配置。
通过遵循本教程,你可以在Vue项目中建立一套完整的Git提交工作流,提高团队协作效率和代码质量。这套流程既适用于个人项目,也适用于团队协作。