项目搭建
1.安装脚手架
npm install -g create-react-app
2.通过脚手架搭建react项目
npx create-react-app react-web --template typescript
3.安装craco,创建craco配置文件,配置路径别名
npm install @craco/craco@alpha -D
// 1.在项目目录下创建craco.config.js
const path = require('path')
module.exports = {
webpack:{
alias:{
'@': path.resolve(__dirname, 'src')
}
}
}
// 2.在tsconfig.json添加配置信息
"baseUrl": ".",
"paths": {
"@/*": [
"src/*"
]
}
// 3.修改package.json中修改启动命令
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "react-scripts eject"
},
craco工具会与默认脚手架的webpack配置合并
4.集成editorconfig配置
EditorConfig 有助于为不同 IDE 编辑器上处理同一项目的多个开发人员维护一致的编码风格。
项目目录下创建.editorconfig文件,VSCode需要安装一个插件:EditorConfig for VS Code
# http://editorconfig.org
root = true
[*] # 表示所有文件适用
charset = utf-8 # 设置文件字符集为 utf-8
indent_style = space # 缩进风格(tab | space)
indent_size = 2 # 缩进大小
end_of_line = lf # 控制换行类型(lf | cr | crlf)
trim_trailing_whitespace = true # 去除行首的任意空白字符
insert_final_newline = true # 始终在文件末尾插入一个新行
[*.md] # 表示仅 md 文件适用以下规则
max_line_length = off
trim_trailing_whitespace = false
5.安装prettier工具
Prettier 是一款强大的代码格式化工具,支持 JavaScript、TypeScript、CSS、SCSS、Less、JSX、Angular、Vue、GraphQL、JSON、Markdown 等语言,基本上前端能用到的文件格式它都可以搞定,是当下最流行的代码格式化工具
// 1.安装prettier
npm install prettier -D
// 2.项目目录创建.prettierrc文件
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": true,
"trailingComma": "none",
"semi": false
}
- useTabs:使用tab缩进还是空格缩进,选择false;
- tabWidth:tab是空格的情况下,是几个空格,选择2个;
- printWidth:当行字符的长度,推荐80;
- singleQuote:使用单引号还是双引号,选择true,使用单引号;
- trailingComma:在多行输入的尾逗号是否添加,设置为 `none`;
- semi:语句末尾是否要加分号,默认值true,选择false表示不加;
// 3.创建.prettierignore忽略文件
/dist/*
.local
.output.js
/node_modules/**z
**/*.svg
**/*.sh
/public/*
// 4.VSCode需要安装prettier的插件,在package.json中配置一个scripts:
"prettier": "prettier --write ."
在vscode设置中配置prettier格式化
6.配置eslint
1.安装依赖包
npm install eslint -D
2.安装成后生成配置文件
npx eslint --init
3.配置脚本 package.json
"scripts": {
"eslint": "eslint src",
"eslint:fix": "eslint src --fix",
}
相关eslint配置参考 zhuanlan.zhihu.com/p/711202471
6.husky安装集成
1.安装husky
npm install --save-dev husky
2.初始化
npx husky-init
初始化后会在根目录下生成个一个 .husky 目录,目录下有个 pre-commit 文件,文件中的命令会在 git commit 时执行。
3.安装lint-staged,保证校验的区域只在暂存区
npm i lint-staged -D
//配置package.json
"scripts": {
"lint:lint-staged": "lint-staged"
}
"lint-staged": {
"*.{js,jsx,,ts,tsx}": [
"npm run prettier",
"npm run eslint:fix"
]
}
//修改.husky/pre-commit
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm run lint:lint-staged
4.安装commitlint用于检查提交信息是否符合预定的规范
npm install @commitlint/cli @commitlint/config-conventional -D
//在项目根目录下,新建一个 `commitlint.config.js` 文件
module.exports = {
// 继承的规则,采用官方推荐的规范
extends: ['@commitlint/config-conventional'],
// 自定义规则
rules: {
// 提交类型必须符合下列类型之一
'type-enum': [
2,
'always',
[
'feat', // 新功能
'fix', // 修复 bug
'docs', // 文档变更
'style', // 代码格式修改,不影响逻辑
'refactor', // 代码重构,无功能或 bug 修复
'perf', // 性能优化
'test', // 添加测试
'chore', // 构建工具或其他依赖变动
'revert', // 回滚操作
'build' // 构建打包相关
]
],
// 允许提交的 message 忽略大小写
'subject-case': [0]
}
}
//执行下面命令生成 `commint-msg` 钩子用于 git 提交信息校验
npx husky add .husky/commit-msg "npx --no -- commitlint --edit $1"