webpack5配置eslint 使用standard版本
时间: 2024-03-07 22:49:56 浏览: 28
首先,你需要安装 `eslint-config-standard` 和 `eslint-plugin-standard`。
```
npm install eslint-config-standard eslint-plugin-standard --save-dev
```
然后,在你的webpack配置文件中添加eslint配置。
```javascript
module.exports = {
// ... other configurations
module: {
rules: [
{
test: /\.js$/,
loader: 'eslint-loader',
enforce: 'pre',
include: [path.resolve(__dirname, 'src')], // 指定检查的目录
options: {
formatter: require('eslint-friendly-formatter'), // 指定错误报告的格式规范
eslintPath: require.resolve('eslint'),
configFile: '.eslintrc.json', // 指定eslint配置文件路径
plugins: ['standard'], // 配置eslint插件
globals: [], // 声明全局变量
rules: {
'no-console': 'error', // 禁止使用console
'no-debugger': 'error', // 禁止使用debugger
'no-alert': 'error', // 禁止使用alert
'no-unused-vars': 'error', // 禁止未使用的变量
'standard/no-callback-literal': 'error' // 禁止在回调函数中直接使用字面量
}
}
},
// ... other loaders
]
}
}
```
最后,你需要在项目根目录下创建 `.eslintrc.json` 文件,并进行以下配置:
```json
{
"extends": [
"standard"
],
"env": {
"browser": true,
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
"semi": ["error", "always"],
"quotes": ["error", "single"]
}
}
```
上述配置表示,我们使用 `standard` 规则集进行代码检查,支持浏览器和 node.js 环境,使用 ECMAScript 2020 版本,强制使用分号和单引号。
这样,你就可以在开发过程中使用 `eslint-loader` 进行代码检查了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)