工程化相关篇
新 2020-03-12
interview
# 如何减少 npm install 的时间
- 选择时延低的 registry,需要企业技术基础建设支持
- NODE_ENV=production,只安装生产环境必要的包
- CI=true,npm 会在此环境变量下自动优化
- 结合 CI 的缓存功能,充分利用 npm cache
- 使用 npm ci 代替 npm i,既提升速度又保障应用安全性
# Git Commit规范
良好的 git commit 规范优势:
- 加快 code review 的流程
- 根据 git commit 的元数据生成 changelog
- 后续维护者可以知道 feature 被修改的原因
# 技术方案
Git提交格式
- 统一团队Git commit 日志标准,便于后续代码review和版本发布
- 使用angular的git commit 日志作为基本规范
- 提交类型限制为:feat, fix, docs, style, refactor, perf, test, chore, revert等
- 提交信息分为两部分,标题(首字母不大写,末尾不要标点)、主题内容(正常的描述信息即可)
- 日志提交时友好的类型选择提示 --- 使用commitize 工具
- 不符合要求格式的日志拒绝提交的保障机制
- 使用validate-commit-msg 工具
- 需要同时在客户端、gitlab server hook做
- 统一 changelog 文档信息生成 --- 使用conventional-changelog-cli 工具
格式要求
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
1
2
3
4
5
2
3
4
5
对格式的说明如下:
- type代表某次提交的类型,比如是修复一个bug还是增加一个新的feature。所有的type类型如下:
- feat:新增feature
- fix:修复bug
- docs:仅仅修改了文档,比如README, CHANGELOG等
- style:仅仅修改了空格,格式缩进,不改变代码逻辑
- refactor:代码重构,没有加新功能或者修复bug
- perf:优化相关,比如提升性能,体验
- test:测试用例,包括单元测试,集成测试等
- chore:改变构建流程、或者增加依赖库、工具等
- revert:回滚到上一个版本
本地开发阶段配置
安装依赖
npm i @commitlint/cli @commitlint/config-conventional commitizen cz-conventional-changelog husky -D
{
"scripts": {
"commit": "git add -A && git-cz"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"devDependencies": {
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"commitizen": "^4.0.3",
"cz-conventional-changelog": "^3.0.2",
"husky": "^3.0.1"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
如果最后产生一个这样的错误:
Error: Could not resolve /Users/Elite/web/node_modules/cz-conventional-changelog.
Cannot find module '/Users/Elite/web/node_modules/cz-conventional-changelog'
1
2
2
只需做个软连接即可:
ln -s /Users/Elite/node_modules /Users/Elite/web/node_modules
1
或者改变 package.json
"config": {
"commitizen": {
"path": "./web/Public/static/node_modules/cz-conventional-changelog"
}
},
1
2
3
4
5
2
3
4
5