ESLint集成Webpack和Webstorm

本文详细介绍ESLint的功能,包括默认规则集、配置选项、风格检查、自定义插件及规则等。提供ESLint的安装与配置步骤,涵盖命令行运行、Webstorm集成、Webpack配置等内容,帮助开发者提升代码质量。

Why ESLint?

Robust set of default rules covering all of the rules that exist in JSLint and JSHint, making a migration to this tool fairly easy.

Configurable rules - including error levels, allowing you to decide what is a warning, error, or simply disabled.

Rules for style checking, which can help keep the code format consistent across teams.

The ability to write your own plugins and rules.

安装

npm i --save-dev eslint babel-eslint eslint-plugin-babel eslint-plugin-react
npm i -g eslint babel-eslint eslint-plugin-babel eslint-plugin-react

命令行运行

eslint -c .eslintrc ./src
eslint -c .eslintrc ./src --fix
eslint -c .eslintrc ./

.eslintrc

{
  "parser": "babel-eslint",
  "plugins": [
    "babel"
  ],
  "rules": {
    "indent": [2, 2, { "SwitchCase": 1, "VariableDeclarator": {"var": 2, "let": 2, "const": 3} }],
    "default-case": 2,
    "eqeqeq": 2,
    "curly": 2,
    "quotes": [2, "single"],
    "linebreak-style": [2, "unix"],
    "semi": [2, "always"],
    "no-console": 0,
    "comma-style": [2, "last"],
    "arrow-parens": 0,
    "handle-callback-err": 2,
    "no-lonely-if": 2,
    "no-multiple-empty-lines": [2, { "max": 1 }],
    "babel/arrow-parens": 0,
    "space-infix-ops": 2,
    "generator-star-spacing": 1,
    "no-var": 2,
    "array-bracket-spacing": 1,
    "babel/object-shorthand": 0,
    "babel/no-await-in-loop": 0,
    "comma-dangle": 1,
    "react/prop-types": 0,
    "react/display-name": 0
  },
  "env": {
    "es6": true,
    "node": true,
    "browser": true
  },
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true,
      "modules": true,
      "arrowFunctions":true,
      "classes":true
    },
    "sourceType": "module"
  },
  "extends": [
    "plugin:react/recommended"
  ],
  "globals": {
  }
}

.eslintignore

node_modules/*
src/interfaceUtil/node_modules/*
snapshotKoa/node_modules/*
dist/*

packages.json

{
...
  "lint": "eslint -c .eslintrc ./ --fix"
...
}

命令行运行:

  • npm run lint

webpack集成

npm i --save-dev eslint-loader

webpack.config.js

  module: {
    rules: [{
      test: /\.js$/,
      use: ['babel-loader?cacheDirectory=true', 'eslint-loader'],
      include: path.join(__dirname, 'src'),
      exclude: [/dist/, /node_modules/, /src\/interfaceUtil\/node_modules/, /snapshotKoa\/node_modules/]
    }, {
      test: /\.(png|jpg|gif)$/,
      use: [{
        loader: 'url-loader',
        options: {
          limit: 8192
        }
      }]
    }]
  },

如果ESLint不同过的话,npm run build会报错。

Webstorm集成

    1. Command + ,
    2. Language & Frameworks
    3. JavaScript
    4. Code Quality Tools
    5. ESLint
    6. Enable
    7. ESLint package (选择全局或者本地的安装)
    8. Configuration file(选择项目的.eslintrc配置文件)
    9. Apply

转载于:https://www.cnblogs.com/olojiang/p/9206066.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值