简体   繁体   English

Webpack eslint-loader ignore.eslintrc 解析器

[英]Webpack eslint-loader ignore .eslintrc parser

I'm having an issue with webpack , specifically eslint-loader .我遇到了webpack的问题,特别是eslint-loader

I have a JS file, with code like:我有一个 JS 文件,代码如下:

class Test {

    MyProp = "MyValue"

}

export default Test;

Initially, when I called npx eslint.最初,当我调用npx eslint. , I got: , 我有:

D:\repro\src\main\js\index.js
  3:12  error  Parsing error: Unexpected token =

I've read somewhere I had to add "parser": "babel-eslint" to.eslintrc for some reason.我读过某处我不得不添加"parser": "babel-eslint" to.eslintrc 出于某种原因。

That did fix the issue with npx eslint , but I still have the issue with npx webpack :那确实解决了npx eslint的问题,但我仍然遇到npx webpack的问题:

ERROR in ./src/main/js/index.js 3:11
Module parse failed: Unexpected token (3:11)
File was processed with these loaders:
 * ./node_modules/eslint-loader/dist/cjs.js

I must have forgot some configuration somewhere, but I can't seem to find where.我一定在某处忘记了一些配置,但我似乎找不到在哪里。

To reproduce this, consider this repo:要重现这一点,请考虑这个 repo:

https://github.com/slacaze/webpack-eslint-issue https://github.com/slacaze/webpack-eslint-issue

  1. npm install
  2. npx eslint => No error (.eslintrc.json uses babel-eslint as parser) npx eslint => 没有错误(.eslintrc.json 使用babel-eslint作为解析器)
  3. npx webpack => Error as above npx webpack => 错误如上

The issue is not that your eslint is failing, it is that this source is not packable without running it through babel.问题不是你的 eslint 失败了,而是这个源如果不通过 babel 运行它是不可打包的。 You need to use babel-loader to actually transpile your code, estlint-loader is merely checking your syntax.您需要使用 babel-loader 来实际转译您的代码,estlint-loader 只是检查您的语法。

First, you need to add the babel-loader as shown here: https://webpack.js.org/loaders/babel-loader/首先,您需要添加 babel-loader,如下所示: https://webpack.js.org/loaders/babel-loader/

Install:安装:

npm install -D babel-loader @babel/core @babel/preset-env webpack

Then configure (remember the order is bottom to top so put this above the eslint entry):然后配置(记住顺序是从下到上,所以把它放在 eslint 条目的上方):

    {
        test: /\.?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
            loader: 'babel-loader',
            options: {
                presets: ['@babel/preset-env']
            }
        }
    },

Also, classProperties is not enabled by default so you'll need that as well: https://babeljs.io/docs/en/babel-plugin-proposal-class-properties此外,默认情况下未启用 classProperties,因此您也需要它: https://babeljs.io/docs/en/babel-plugin-proposal-class-properties

Install:安装:

npm install --save-dev @babel/plugin-proposal-class-properties

Configure by adding a.babelrc:通过添加 a.babelrc 进行配置:

{
    "plugins": ["@babel/plugin-proposal-class-properties"]
}

As in this updated version:与此更新版本一样:

https://github.com/dpwrussell/webpack-eslint-issue https://github.com/dpwrussell/webpack-eslint-issue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM