简体   繁体   English

忽略或防止 ESLint 错误破坏 React webpack 项目中的构建

[英]Ignore or prevent ESLint errors from breaking the build in React webpack project

The problem is that, while I'm developing, every time there's a problem with ESLint, the build breaks and doesn't compile the code.问题是,在我开发时,每次 ESLint 出现问题时,构建都会中断并且无法编译代码。

I set emitWarning: true, but not working.我设置了 emitWarning: true,但不起作用。 My React project is develop with webpack, neutrino JS, esLint, airbnb.我的 React 项目是使用 webpack、neutrino JS、esLint、airbnb 开发的。

Errors looks like while running build运行构建时出现错误

ERROR in./src/index.jsx Module build failed (from./node_modules/eslint-loader/dist/cjs.js): Module failed because of a eslint error.错误 in./src/index.jsx 模块构建失败(来自./node_modules/eslint-loader/dist/cjs.js):由于 eslint 错误,模块失败。

code structure in.neutrinorc.js file .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"],
                },
              },
            ],
          },
        },
      },
    }),

You need to add failOnWarning as false您需要将failOnWarning添加为false

emitWarning: true,
failOnWarning: false,

I had to combine a couple solutions to fix this problem.我不得不结合几个解决方案来解决这个问题。 It's not the most elegant but it works.它不是最优雅的,但它确实有效。

Add RUN_ESLINT=true to package.json watch script:将 RUN_ESLINT=true 添加到 package.json 观察脚本:

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

Rewrite module.exports as a function:将 module.exports 重写为 function:

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

Use webpack.EnvironmentPlugin to pull in env variables.使用webpack.EnvironmentPlugin拉入环境变量。

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

Conditionally add ESLintPlugin:有条件地添加 ESLintPlugin:

const plugins = [...];

if (process.env.RUN_ESLINT) {
  plugins.push(new ESLintPlugin());
}
  

That allows me to keep using my npm run build and npm run watch scripts as I had before I added eslint.这使我可以继续使用我的npm run buildnpm run watch脚本,就像我在添加 eslint 之前一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM