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

时间:2018-01-14 12:26:06

标签: reactjs webpack webpack-2 eslint eslintrc

我最近使用create-react-project创建了一个项目。

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

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

4 个答案:

答案 0 :(得分:24)

如果您想强制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
        },
    }],
},

As stated in the docs

  

错误和警告

     

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

     
      
  • emitError(默认:false

         

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

  •   
  • emitWarning(默认:false

         

    如果选项设置为true,则加载程序将始终返回警告。如果您正在使用热模块更换,您可能希望在开发中启用此功能,否则在遇到包装错误时将跳过更新。

  •   
  • ...

  •   

答案 1 :(得分:1)

好的,我刚刚从我的webpack配置中评论了这一行

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

答案 2 :(得分:1)

由于eslint-loader现在已被弃用,eslint-webpack-plugin现在在create-react-app检查docs中使用了eslint-webpack-plugin,因此我能够通过在{ {1}}

弹出您的应用程序后,将以下选项添加到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'
            })
          }
        }
      })

答案 3 :(得分:0)

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

干杯!