简体   繁体   English

覆盖未弹出的 create-react-app 中的 eslint 设置?

[英]Override eslint settings in non-ejected create-react-app?

I've found a ton of "solutions" for this, ranging from simple package.json additions to custom hack modules, but none worked for me.我为此找到了大量“解决方案”,从简单的 package.json 添加到自定义 hack 模块,但没有一个对我有用。

I simply want to override the eslint settings for an out-of-the-box, NON ejected create-react-app.我只是想覆盖开箱即用的非弹出 create-react-app 的 eslint 设置。

Namely, the rule "no-unused-vars".即,规则“no-unused-vars”。

I'm using Visual Studio Code.我正在使用 Visual Studio 代码。

I seem to have fixed this accidentally just trying combinations of things I found online.我似乎只是尝试了我在网上找到的东西的组合,意外地解决了这个问题。 This seems to have worked.这似乎奏效了。

1) I created a .env file in the project root (where the package.json file is). 1)我在项目根目录(package.json 文件所在的位置)创建了一个 .env 文件。 In it I have:其中我有:

// .env file

EXTEND_ESLINT = true

2) Then I created a .eslintrc file (no extension) in my project root and added this: 2)然后我在我的项目根目录中创建了一个 .eslintrc 文件(没有扩展名)并添加了这个:

// .eslintrc file (no extension)
{
  "extends": [
    "react-app"
  ],
  "rules": {
    "no-unused-vars": "off"
  }
}

In the create-react-app project package.json is ready, you just need to add the rules field.create-react-app项目package.json已经准备好了,只需要添加rules字段即可。

package.json

"eslintConfig": {
  "extends": [
    "react-app",
    "react-app/jest"
  ],
+ "rules": {
+    "react/self-closing-comp": 1
+  }
},

It's important to note that any rules that are set to "error" will stop the project from building. 请务必注意,任何设置为“错误”的规则都将停止项目的构建。

create-react-app uses eslint-config-react-app , which contains almost all popular eslint packages. create-react-app使用eslint-config-react-app ,它包含几乎所有流行的 eslint 包。

  • eslint-plugin-flowtype
  • eslint-plugin-import
  • eslint-plugin-jest
  • eslint-plugin-jsx-a11y
  • eslint-plugin-react
  • eslint-plugin-react-hooks
  • eslint-plugin-testing-library

eslint-config-react-app github https://github.com/facebook/create-react-app/tree/main/packages/eslint-config-react-app eslint-config-react-app github https://github.com/facebook/create-react-app/tree/main/packages/eslint-config-react-app

eslint-config-react-app npm https://www.npmjs.com/package/eslint-config-react-app eslint-config-react-app npm https://www.npmjs.com/package/eslint-config-react-app

The library now supports extending the pre-defined ESLint rules natively, see the relevant docs .该库现在支持本地扩展预定义的 ESLint 规则,请参阅相关文档


The gist of it is that you will have to set the EXTEND_ESLINT environment variable, and then add your own ESLint config to the project root, optionally extending create-react-app's:其要点是您必须设置EXTEND_ESLINT环境变量,然后将您自己的 ESLint 配置添加到项目根目录,可选择扩展 create-react-app 的:

{
  "eslintConfig": {
    "extends": ["react-app"],
    "overrides": [
      {
        "files": ["**/*.js"],
        "rules": {
          "no-unused-vars": "warn"
        }
      }
    ]
  }
}

its not hard, just follow these steps:这并不难,只需按照以下步骤操作:

  1. npm i -D eslint eslint-plugin-react
  2. npx eslint --init
  3. edit the generated config file, for example .eslintrc.json编辑生成的配置文件,例如.eslintrc.json
  4. put your rules in the "rules": { ... } section将您的规则放在"rules": { ... }部分

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

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