【问题标题】:Ignore or prevent ESLint errors from breaking the build in React webpack project忽略或防止 ESLint 错误破坏 React webpack 项目中的构建
【发布时间】:2021-07-28 11:26:00
【问题描述】:

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

我设置了 emitWarning: true,但不起作用。 我的 React 项目是用 webpack、neutrino JS、esLint、airbnb 开发的。

运行构建时出现的错误

./src/index.jsx 中的错误 模块构建失败(来自 ./node_modules/eslint-loader/dist/cjs.js): 由于 eslint 错误,模块失败。

.neutrinorc.js 文件中的代码结构

const path = require("path");
const airbnb = require("@neutrinojs/airbnb");
const react = require("@neutrinojs/react");

module.exports = {
  options: {
    root: __dirname,
  },
  use: [
    (neutrino) => {
      neutrino.config.resolve.modules
        .add("node_modules")
        .add(path.resolve(__dirname, "src"));

      neutrino.config.resolve.extensions.add(".jsx");

      neutrino.config.resolve.alias.set("react-dom", "@hot-loader/react-dom");
    },
    airbnb({
      eslint: {
        emitWarning: true,
        baseConfig: {
          settings: {
            "import/resolver": "webpack",
          },
          rules: {
            radix: "off",
            "comma-dangle": "off",
            "react/no-danger": "off",
            "react/button-has-type": "off",
            "react/no-find-dom-node": "off",
            "react/jsx-filename-extension": "off",
            "no-console": [
              "error",
              {
                allow: ["warn", "error"],
              },
            ],
            "no-alert": "off",
            "no-plusplus": "off",
            "no-else-return": "off",
            "no-nested-ternary": "off",
            "no-underscore-dangle": "off",
            "no-restricted-syntax": "off",
            "max-len": 0,
            "quote-props": "off",
            "default-case": "off",
            "class-methods-use-this": "off",
            "import/prefer-default-export": "off",
            "react/no-array-index-key": "off",
            "jsx-a11y/click-events-have-key-events": "off",
            "jsx-a11y/label-has-for": [
              "error",
              {
                required: {
                  some: ["nesting", "id"],
                },
              },
            ],
          },
        },
      },
    }),

【问题讨论】:

    标签: reactjs eslint webpack-dev-server eslint-config-airbnb neutrino


    【解决方案1】:

    您需要将failOnWarning 添加为false

    emitWarning: true,
    failOnWarning: false,
    

    【讨论】:

    • 不工作,仍然显示 => 模块构建失败(来自 ./node_modules/eslint-loader/dist/cjs.js):模块因 eslint 错误而失败。
    • 运行时npm run build
    【解决方案2】:

    我不得不结合几个解决方案来解决这个问题。它不是最优雅的,但它确实有效。

    将 RUN_ESLINT=true 添加到 package.json 监视脚本:

    export RUN_ESLINT=true && webpack --config webpack.config.js --watch --mode development
    

    将 module.exports 重写为函数:

    module.exports = () => {
    ...
    return {}
    }
    

    使用webpack.EnvironmentPlugin 拉入环境变量。

    new webpack.EnvironmentPlugin(["NODE_ENV", "DEBUG", "RUN_ESLINT"]);
    

    有条件地添加 ESLintPlugin:

    const plugins = [...];
    
    if (process.env.RUN_ESLINT) {
      plugins.push(new ESLintPlugin());
    }
      
    

    这让我可以像添加 eslint 之前一样继续使用我的 npm run build 和 npm run watch 脚​​本。

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 2020-04-28
      • 2018-03-07
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 2014-07-21
      • 1970-01-01
      相关资源
      最近更新 更多