由于弹出后的 lint 警告,无法编译 create-react-app

cha*_*ham 5 reactjs eslint webpack create-react-app

我一直在学习 React 课程。下一步已经使用,npm run eject以便可以使用 css 模块。

由于弹出我不能使用 npm start。页面无法编译。出现一长串 linting 警告(实际上似乎来自 webpack 配置文件)。

我为这些文件和其他文件创建了一个 .eslintignore 文件:

./reactNotes.jsx
./nodeTest.js
./config
./scripts
Run Code Online (Sandbox Code Playgroud)

但是我的代码编辑器(vs 代码)或 webpack 似乎都没有注意到这个文件。eslint 是全局安装的。

我还研究了 eslint 的 webpack 配置,其中包含诸如 exclude 之类的选项:

  {
    test: /\.(js|jsx|mjs)$/,
    enforce: 'pre',
    use: [
      {
        options: {
          formatter: eslintFormatter,
          eslintPath: require.resolve('eslint'),
        },
        loader: require.resolve('eslint-loader'),
      },
    ],
    include: paths.appSrc,
    exclude: /config/
  }
Run Code Online (Sandbox Code Playgroud)

VS Code 在我的用户设置中启用了 eslint。

如何设置 webpack(甚至可能是 vs 代码)来忽略目录和文件?

更新:这里是.eslintrc:

{
    "parser": "babel-eslint",
    "extends": "react-app",
    "plugins": [
        "react",
        "jsx-a11y",
        "import"
    ],
    "rules": {
        "linebreak-style": [
            "error",
            "windows"
        ],
        "indent": [
            "error",
            4
        ],
        "react/prop-types": 0,
        "react/jsx-indent": 0,
        "jsx-a11y/click-events-have-key-events": 0,
        "jsx-a11y/no-noninteractive-element-interactions": 0,
        "max-len": 0,
        "arrow-body-style": 0,
        "react/jsx-indent-props": 0,
        "react/no-unescaped-entities": 0,
        "react/jsx-no-bind": 0,
        "arrow-parens": 0,
        "react/no-array-index-key": 0
    }
}
Run Code Online (Sandbox Code Playgroud)

npm install 和重启浏览器也不起作用。

浏览器错误

cha*_*ham 1

loveky 的答案修复了 VS 代码。

要修复构建,请在包含服务工作线程文件的 webpack.config 中添加排除属性:

  {
    test: /\.(js|jsx|mjs)$/,
    enforce: 'pre',
    use: [
      {
        options: {
          formatter: eslintFormatter,
          eslintPath: require.resolve('eslint'),
          emitError: false,
        },
        loader: require.resolve('eslint-loader'),
      },
    ],
    include: paths.appSrc,
    exclude: /(config|node_modules|registerServiceWorker.js|nodeTest.js|reactNotes.jsx|webpack.config)/,
  }
Run Code Online (Sandbox Code Playgroud)