28.webpack配置js语法检查

198 阅读1分钟

这个配置用来在webpack执行打包时对js基本语法错误或隐患,进行检查和自动更改

  1. 安装loader
npm install eslint-loader eslint -D
  1. 安装检查规则库 eslint-config airbnb-base 定制(提炼)了一套标准且常用的js语法检查规则,推荐使用
    eslint-plugin-import 可以将eslint-config airbnb-base导入到eslint-loader eslint
npm install eslint-config-airbnb-base eslint-plugin-import -D
  1. 配置loader
module: {
    rules: [
    {
        // 对js进行语法检查
        test: /\.js$/,
        exclude: /node_modules/,
        enforce: 'pre', // 优先执行
        loader: 'eslint-loader',
        options:
            fix: true // 若有问题自动修复,重要! ! ! !
    }
}
  1. 添加配置至package.json
    (注:当Loader中没有配置时,查找eslint.config.js配置规则文件,若没有,继续查找package.json中对应的配置)
"eslintConfig": {
    "extends": "airbnb-base", // 直接使用airbnb-base提供的规则
    "env": {
        "browser": true // true:浏览器运行模式;false:node运行模式
    }
}
  1. 运行
npm run build // yarn build

此时在控制台出现语法检查提示