前端工程化校验配置

爱吃猪头爱吃肉

为了更好项目质量,对代码和提交信息进行校验配置

校验配置信息:
Eslint
Prettier
husky
lint-staged
commitlint
stylelint (未配置)

eslint

安装

npm install eslint -D

初始化安装

npx eslint --init

自定义选择配置

  1. 选择如何使用Eslint
  • 仅检查语法
  • 检查语法并查找问题 (默认) ✅
  • 检查语法、查找问题并强制执行代码样式

image.png

  1. 您的项目使用什么类型的模块?
  • JavaScript modules (import/export)(默认) ✅
  • CommonJS (require/exports)
  • None of these

image.png

  1. 您的项目使用哪种框架?
    • React(默认) ✅
    • Vue
    • None of these

image.png

  1. 您的项目是否使用TypeScript
    • Yes ✅
    • No(默认)

image.png

  1. 你的代码运行
    • brower(默认) ✅
    • Node

image.png

  1. 您希望配置文件采用什么格式?
    • JavaScript(默认) ✅
    • YAML
    • JSON

image.png

  1. 是否需要现在安装配置所需依赖

image.png

  1. 使用什么工具安装

image.png

自定义配置

rules中配置eslint配置

image.png

commitlint

安装

npm install @commitlint/cli @commitlint/config-conventional -D

配置 commitlint.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module.exports = {
extends: ['@commitlint/config-conventional'], // 引入默认的配置规则
rules: { // 配置规则 使用feat: xxxx的方式
'type-enum': [
2,
'always',
[
'upd',
'feat',
'fix',
'refactor',
'docs',
'chore',
'style',
'revert',
],
],
'type-case': [0],
'type-empty': [0],
'scope-empty': [0],
'scope-case': [0],
'subject-full-stop': [0, 'never'],
'subject-case': [0, 'never'],
'header-max-length': [0, 'always', 72],
},
};

prettier

安装

npm install prettier -D

配置 .prettierrc.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
//每行最多多少个字符换行
"printWidth": 100,
// tab缩进大小,默认为2
"tabWidth": 4,
// 使用tab缩进,默认false
"useTabs": false,
// 使用分号, 默认true
"semi": false,
// 使用单引号, 默认false(在jsx中配置无效, 默认都是双引号)
"singleQuote": false,
// 行尾逗号,默认none,可选 none|es5|all
// es5 包括es5中的数组、对象
// all 包括函数对象等所有可选
"TrailingCooma": "all",
// 对象中的空格 默认true
// true: { foo: bar }
// false: {foo: bar}
"bracketSpacing": true,
// JSX标签闭合位置 默认false
// false: <div
// className=""
// style={{}}
// >
// true: <div
// className=""
// style={{}} >
"jsxBracketSameLine": false,
// 箭头函数参数括号 默认avoid 可选 avoid| always
// avoid 能省略括号的时候就省略 例如x => x
// always 总是有括号
"arrowParens": "avoid"
}

lint-staged

针对git暂存区的代码进行检测

安装

npm install lint-staged -D

lint-staged版本 > 13 会报错(奇奇怪怪),改为12版本

配置

1
2
3
4
5
6
"lint-staged": { 
"*.{js,ts,tsx.css,md}": [
"eslint --fix"
"prettier --write .",
]
}

husky

爱斯基摩狗,更简单的使用git钩子

安装

npm install husky

使用

  1. 写入script

    npm set-script prepare "husky install"

    初次配置需要手动执行 npx husky installnpm run prepare

  2. 初始化husky

    npx husky init

  3. 设置commit钩子,用于校验commitlint

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

  4. 设置pre-commit钩子,用于校验暂存区代码

    npx husky add .husky/pre-commit "npx --no-install lint-staged"

  • 标题: 前端工程化校验配置
  • 作者: 爱吃猪头爱吃肉
  • 创建于: 2023-04-16 09:53:51
  • 更新于: 2023-04-21 07:33:16
  • 链接: https://zsasjy.github.io/2023/04/16/前端工程化校验配置/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
推荐文章
搭建react开发环境 搭建react开发环境 前端 CSS 代码规范 前端 CSS 代码规范 git常见指令 git常见指令 webpack常用配置汇总 webpack常用配置汇总 Docker基本用法 Docker基本用法 浏览器架构 浏览器架构
 评论