灏天阁

vue3.2/Vite/Ts6: 配置 commitlint

· Yin灏

对于我们的 commit 信息也是有统一的规范的,不能随便书写,要让每个人都按照统一的标准来执行,我们可以利用 commitlint 来实现。

安装包

pnpm add @commitlint/config-conventional @commitlint/cli -D

添加配置文件,新建 commitlint.config.cjs,然后添加下面的代码:

module.exports = {
	extends: ['@commitlint/config-conventional'],
	rules: {
		'type-enum': [
			2,
			'always',
			[
				'feat',
				'fix',
				'docs',
				'style',
				'refactor',
				'perf',
				'test',
				'chore',
				'revert',
				'build'
			]
		],
		'type-case': [0],
		'type-empty': [0],
		'scope-empty': [0],
		'scope-case': [0],
		'subject-full-stop': [0, 'never'],
		'subject-case': [0, 'never'],
		'header-max-length': [0, 'always', 72]
	}
}

在 package.json 中配置 scripts 命令:

//...
"scripts": {
	"commitlint": "commitlint --config commitlint.config.cjs -e -v"
}
//...

配置结束后,现在当我们填写 commit 信息的时候,前面就需要带着下面的 subject:

  • ‘feat’: 新特性,新功能
  • ‘fix’: 修改 bug
  • ‘docs’: 文档修改
  • ‘style’: 代码格式修改,注意不是 css 修改
  • ‘refactor’: 代码重构
  • ‘perf’: 优化相关,比如提升性能、体验
  • ’test’: 测试用例修改
  • ‘chore’: 其他修改,比如改变构建流程、或者增加依赖库、工具等
  • ‘revert’: 回滚到上一个版本
  • ‘build’: 编译相关的修改,例如发布版本、对项目构建挥或者依赖的改动

配置 husky

npx husky add .husky/commit-msg

在生成 commit-msg 文件中添加下面的命令

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
pnpm commitlint

当我们提交 commit 的时候,就不能随意写了。必须是 git commit -m 'fix: xxxx' 符合类型的才可以,需要注意的是类型的后面需要用英文的:(冒号),并且冒号后面是需要空一格的,这个是不能省略的。

- Book Lists -