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

IT技术 reactjs webpack webpack-2 eslint eslintrc
2021-04-27 12:49:05

我最近用create-react-project.

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

我可以在 ESLint 继续运行并报告稍后修复的错误的同时保持构建运行吗?

4个回答

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

如文档中所述

错误和警告

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

  • emitError(默认值:false

    如果此选项设置为 true,加载程序将始终返回错误。

  • emitWarning(默认值:false

    如果选项设置为 ,加载程序将始终返回警告true如果您正在使用热module替换,您可能希望在开发中启用此功能,否则当出现 eslint 错误时将跳过更新。

  • ...

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

干杯!

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

弹出你的react应用程序后,将这些选项添加到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'
            })
          }
        }
      })

对于想要在 2021 年解决这个问题的人,您可以简单地在 .env.development 文件中设置以下内容以忽略 TypeScript 错误

TSC_COMPILE_ON_ERROR=true

来源:https : //create-react-app.dev/docs/advanced-configuration/# :~: text=TSC_COMPILE_ON_ERROR

另外,要忽略 ESLint 错误,请使用 craco 覆盖eslint-webpack-plugin默认配置以忽略开发环境中的错误。

const eslintWebpackPlugin = require("eslint-webpack-plugin");
const process = require("process");
module.exports = {
  webpack: {
    configure: (config) => {
      config.plugins.forEach((plugin) => {
        if (plugin instanceof eslintWebpackPlugin) {
          // Ignore ESLint Errors.
          plugin.options.failOnError = process.env.NODE_ENV === "production";
        }
      });
      return config;
    },
  },
};