precommit.gif
一红柱、pre-commit預(yù)提交格式化代碼
- 安裝相關(guān)插件如下:
npm install husky --save-dev
npm install prettier pretty-quick --save-dev
- 添加腳本執(zhí)行如下命令(package.json文件中):
"scripts": {
"prepare": "husky install",
},
# 然后執(zhí)行如下命令:
npm run prepare
- 添加pre-commit鉤子
npx husky add .husky/pre-commit "npx pretty-quick --staged"
- 接下來需要添加相關(guān)配置項(xiàng)承匣,控制格式化代碼的范圍
項(xiàng)目根目錄下新增如下2個(gè)文件:
4.1.prettierignore
not format
.husky
.vscode
dist
node_modules
package-lock.json
yarn.lock
4.2 .prettierrc.json
{
"singleQuote": true,
"semi": false,
"trailingComma": "all",
"arrowParens": "always",
}
=========================== ok ===========================
二、commit-msg提交格式化規(guī)范
- 安裝相關(guān)插件如下:
npm install @commitlint/cli @commitlint/config-conventional --save-dev
- 添加提交鉤子校驗(yàn)提交規(guī)范
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
- 添加標(biāo)準(zhǔn)化配置文件锤悄,執(zhí)行如下命令
echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
提交方式大致如下:
git commit -m 'fix(組件名稱): 登錄頁面bug修復(fù)'
git commit -m 'style: 代碼格式化'
具體使用請(qǐng)參考本地文件:/node_modules/@commitlint/config-conventional/README.md
三韧骗、命令窗式提交,執(zhí)行如下命令
npm install cz-git commitizen --save-dev
在`package.json`文件中添加如下配置
"config": {
"commitizen": {
"path": "./node_modules/cz-git"
}
}
npx git-cz
或者如下:
"scripts": {
"start": "npm run dev",
"dev": "webpack serve --config ./build/webpack.dev.js --progress",
"build": "webpack --config ./build/webpack.prod.js --progress",
"prepare": "husky install",
"format": "prettier --write .",
"cz": "git-cz"
},
npm run cz 亦可
cz-git.gif