前端构建相关


1. husky

使用git提交代码时可使用husky关联相关的git hooks进行相应的处理。

安装:

npm install husky -D   (-D等价于--save-dev)

package.json 添加 prepare 脚本,并且执行一次:

npm set-script prepare "husky install"
npm run prepare

添加pre-commit hook钩子:

npx husky add .husky/pre-commit "npm test"
git add .husky/pre-commit

其中npm test即为pre-commit时执行的脚本,可根据具体需求改成自己的脚本;

npx命令会先去全局查找husky命令,全局没找到再去当前目录找,当前目录也没有则安装该模块, 后面是执行的shell

验证pre-commit:

git commit -m "test"

前面pre-commit添加的`npm test`脚本将在每次提交前执行。

注意:上面的是husky v7版本的步骤,v4版本有很大的区别。(区别可参考:https://typicode.github.io/husky/#/)

参考文档:https://github.com/typicode/husky#readme

2. commitlint

安装:

npm install --save-dev @commitlint/{config-conventional,cli}

安装常规配置和cli,windows下为:

npm install --save-dev @commitlint/config-conventional @commitlint/cli

配置commitlint文件

echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

添加commit-msg hook:

npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'

如果出错可能是系统原因,可参考这里面的写法:https://github.com/conventional-changelog/commitlint

验证:

git commit -m 'test'

会报错

subject may not be empty [subject-empty]

修改为:git commit -m 'feat: test'  验证通过。

commitlint规则为: type(scope?): subject 

scope可选,比如feat(blog): add comment section

通用的type可以是:

[
'build',
'chore',
'ci',
'docs',
'feat',
'fix',
'perf',
'refactor',
'revert',
'style',
'test'
];

参考文档:https://github.com/conventional-changelog/commitlint