Conventional Commits规范在Vue项目里怎么总报错?

静静 阅读 65

最近在Vue项目里用Conventional Commits规范,但每次提交代码husky检查就报错。比如我写了这样的组件:


<template>
  <button class="btn" @click="handleClick">{{ text }}</button>
</template>

<script>
export default {
  props: ['text'],
  methods: {
    handleClick() {
      this.$emit('clicked');
    }
  }
}
</script>

提交时用”feat(vue-button): 添加点击反馈”却被提示类型错误。试过改commitlint.config.js里的规则,把”feat”加到allowedValues里还是不行。是不是规范里不能用vue-button这种范围格式?或者类型命名有特殊要求?

我来解答 赞 12 收藏
二维码
手机扫码查看
2 条解答
书生シ红瑞
看你的描述,问题大概率出在 commitlint 的配置上。

你提到把 "feat" 加到 allowedValues 里,这个配置名可能写错了。Conventional Commits 规范里 type 的配置项叫 type-enum,不是 allowedValues。

一个能用的 commitlint.config.js 长这样:

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


你那个 "feat(vue-button): 添加点击反馈" 格式完全没问题,scope 用 kebab-case 是允许的。

另外提醒一下,props 定义方式也有问题。你写的 props: ['text'] 是数组形式,这种写法在 Vue 2 里是合法的,但 Vue 3 已经不支持了,建议改成对象形式:

props: {
text: {
type: String,
default: ''
}
}


不过这是另一个问题了,先把 commitlint 配置改对,提交应该就能过了。
点赞
2026-03-19 23:00
萌新.梓童
先说结论,问题出在 scope 的命名规则上。Conventional Commits 规范对 scope 有严格的格式要求,默认情况下只允许小写字母、数字和短横线,不能包含特殊字符或大写字母。

你用的 vue-button 这个 scope 包含了连字符,虽然语法上没问题,但很可能被 commitlint 配置中的正则表达式给拦截了。性能上来说,最简单的解决方法是修改 commitlint 配置文件,让 scope 支持这种命名方式。

具体做法是找到 commitlint.config.js,把 parserPreset 部分的 scope 校验规则改一下,改成类似这样的正则:

module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'scope-case': [2, 'always', 'lowerCase'],
'scope-enum': [2, 'always', ['vue-button', '其他范围']],
'type-enum': [
2,
'always',
['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore']
]
}
};


这样配置完记得重启 husky,不然可能会缓存旧规则。如果还不行,可能是 husky 版本太老导致的兼容性问题,建议升级到最新版。说实话我之前也被这个坑过好几次,尤其是团队协作的时候,提交规范这块确实挺烦人的。
点赞 8
2026-02-16 06:00