webpack4&&ESlint
乔文飞 Lv8

ESlint

ESLint是一个用来识别ECMAScript 并且按照规则给出报告的代码检测工具,使用它可以避免低级错误和统一代码的风格。

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//.eslint.js
// 区分生产环境、开发环境
const _mode = process.env.NODE_ENV || 'production';

module.exports = {
"env": {
"browser": true,
"es6": true,
"node": true,
},
"globals": {
"$": true,
"process": true,
"dirname": true,
},
"parser": "babel-eslint",
"extends": "eslint:recommended",
"parserOptions": {
"ecmaFeatures": {
"jsx": true,
"legacyDecorators": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"no-console": "off",
"no-debugger": _mode==='development' ? 0 : 2,
"no-alert": _mode==='development' ? 0 : 2,
// "no-multi-spaces": "error",
"no-unused-vars": "off", // react中不适用
"no-constant-condition": "off",
"no-fallthrough": "off",
// "keyword-spacing": ["error", { "before": true} ], // 不生效,先注释
// "indent": [
// "error",
// 2
// ],
"linebreak-style": [
"error",
"unix"
],
// "quotes": [
// "error",
// "single"
// ],
"semi": [0],
"no-unexpected-multiline": 0,
"no-class-assign": 0,
}
};

检查eslint

  • 方式一: 安装husky,增加npm script,适合老项目
1
2
3
4
5
6
7
8
9
10
11
12
13
"scripts": {
//"precommit": "eslint --ext .js --ext .jsx src/",
"precommit": "eslint lint-staged", // 增量检查修改的文件
},
"lint-staged": {
//"src/**/*.js": [
// "eslint --ext .js --ext .jsx",
// "git add"
//]
"linters": {
"*.[js,scss]": ["eslint --fix", "git add"]
}
}
  • 方式二:webpack与eslint结合,新项目
1
2
3
4
5
6
7
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: ['babel-loader', 'eslint-loader']
}
]

其实新项目中,可以将两种方式同时使用

  • 本文标题:webpack4&&ESlint
  • 本文作者:乔文飞
  • 创建时间:2020-12-15 16:23:27
  • 本文链接:http://www.feidom.com/2020/12/15/webpack4&&ESlint/
  • 版权声明:本博客所有文章为作者学习笔记,有转载其他前端大佬的文章。转载时请注明出处。