Skip to content

前端项目工程化

husky :https://typicode.github.io/husky/zh/

lint-staged:https://github.com/lint-staged/lint-staged

commitlint:https://commitlint.js.org/reference/configuration.html

commit 提交规范

keyvalue
feat新增 feature
fix修复 bug
docs仅仅修改了文档,比如 README, CHANGELOG, CONTRIBUTE 等等
style不改变代码逻辑。仅仅修改了空格、格式缩进、逗号等等
refactor代码重构,没有加新功能或者修复 bug
perf优化相关,比如提升性能、体验
test测试用例,包括单元测试、集成测试等
chore改变构建流程、或者增加依赖库、工具等
revert回滚到上一个版本

跳过 Git 钩子

单次跳过 -n/--no-verify

sh
git commit -m "..." -n

临时禁用所有 Git 钩子

sh
HUSKY=0 git commit -m "..."

更长的禁用

sh
export HUSKY=0 # 禁用所有 Git 钩子
git commit -m "..."
git commit -m "..."
unset HUSKY # 重新启用钩子

重新开始搭建

sh
pnpm add husky -D

# npx husky init
# pnpm exec husky init
pnpm husky init
sh
pnpm add lint-staged eslint -D

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

lint-staged 配置不多,可以直接加在 package.json 中

json
"lint-staged": {
  "*.{js,jsx,ts,tsx,vue}": [
    "prettier --write"
  ]
},

lint-staged 用最标准的用法就行了,不要自己乱加! lint-staged 就推荐 prettier 与 eslint 即可

创建 .commitlintrc.js

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

参考

参考 1: https://juejin.cn/post/6982192362583752741

参考 2: https://blog.esonwong.com/husky/

参考 3bili: https://gitee.com/liguangtao_home/course/tree/master/husky