[英]Ignore or prevent ESLint errors from breaking the build in a React project (create-react-project)
[英]Ignore or prevent ESLint errors from breaking the build in React webpack project
问题是,在我开发时,每次 ESLint 出现问题时,构建都会中断并且无法编译代码。
我设置了 emitWarning: true,但不起作用。 我的 React 项目是使用 webpack、neutrino JS、esLint、airbnb 开发的。
运行构建时出现错误
错误 in./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"],
},
},
],
},
},
},
}),
您需要将failOnWarning
添加为false
emitWarning: true,
failOnWarning: false,
我不得不结合几个解决方案来解决这个问题。 它不是最优雅的,但它确实有效。
将 RUN_ESLINT=true 添加到 package.json 观察脚本:
export RUN_ESLINT=true && webpack --config webpack.config.js --watch --mode development
将 module.exports 重写为 function:
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());
}
这使我可以继续使用我的npm run build
和npm run watch
脚本,就像我在添加 eslint 之前一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.