忽略或阻止ESLint错误破坏React项目中的构建(create-react-project)

R01*_*010 13 reactjs eslint webpack webpack-2 eslintrc

我最近创建了一个项目create-react-project.

问题在于,在我开发的过程中,每次ESLint出现问题时,构建都会中断并且不会编译代码.

我是否可以保持构建运行,同时仍然运行ESLint并报告我稍后会修复的错误?

Dan*_*ger 29

如果要强制ESLint始终发出警告(不会阻止您构建)而不是错误,则需要设置emitWarning: true:

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

如文档中所述:

错误和警告

默认情况下,加载程序将根据eslint错误/警告计数自动调整错误报告.您仍然可以使用emitErroremitWarning选项强制执行此操作:

  • emitError(默认值:false)

    如果此选项设置为true,则Loader将始终返回错误.

  • emitWarning(默认值:false)

    如果选项设置为,则Loader将始终返回警告true.如果您正在使用热模块替换,您可能希望在开发中启用此功能,否则当出现夹板错误时将跳过更新.

  • ...

  • 请问这个应该放在哪个文件里? (6认同)
  • [`webpack.config.js`](https://webpack.js.org/configuration/) (3认同)

小智 26

只需添加DISABLE_ESLINT_PLUGIN=true到您的.env文件中

干杯!

  • 或 `DISABLE_ESLINT_PLUGIN=true npm run build` (2认同)
  • ```'DISABLE_ESLINT_PLUGIN' 不被识别为内部或外部命令``` 我明白了,知道吗? (2认同)

ali*_*ein 5

由于eslint-loader现已弃用并且eslint-webpack-plugin现在用于create-react-app检查文档,因此我能够通过向eslint-webpack-plugin

弹出你的反应应用程序后,将这些选项添加到选项中ESLintPlugin

      new ESLintPlugin({
        // Plugin options
        extensions: ['js', 'mjs', 'jsx', 'ts', 'tsx'],
        formatter: require.resolve('react-dev-utils/eslintFormatter'),
        eslintPath: require.resolve('eslint'),
        context: paths.appSrc,
        failOnError: false,  <== `This one`
        emitWarning: true,  <== `And this one`
        // ESLint class options
        cwd: paths.appPath,
        resolvePluginsRelativeTo: __dirname,
        baseConfig: {
          extends: [require.resolve('eslint-config-react-app/base')],
          rules: {
            ...(!hasJsxRuntime && {
              'react/react-in-jsx-scope': 'error'
            })
          }
        }
      })
Run Code Online (Sandbox Code Playgroud)

  • 如果我不想弹出怎么办? (6认同)