【问题标题】:Override eslint settings in non-ejected create-react-app?在非弹出的 create-react-app 中覆盖 eslint 设置?
【发布时间】:2020-02-18 21:17:51
【问题描述】:

我为此找到了大量“解决方案”,从简单的 package.json 添加到自定义 hack 模块,但没有一个对我有用。

我只是想覆盖一个开箱即用的、非弹出的 create-react-app 的 eslint 设置。

即规则“no-unused-vars”。

我正在使用 Visual Studio Code。

【问题讨论】:

    标签: reactjs overriding create-react-app eslint


    【解决方案1】:

    我似乎只是尝试组合我在网上找到的东西时偶然解决了这个问题。这似乎奏效了。

    1) 我在项目根目录(package.json 文件所在的位置)中创建了一个 .env 文件。在里面我有:

    // .env file
    
    EXTEND_ESLINT = true
    

    2) 然后我在我的项目根目录中创建了一个 .eslintrc 文件(无扩展名)并添加了这个:

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

    【讨论】:

    • 另外,如果你还没有重启create-react-app。感谢您发布您的解决方案。
    • 踢球者似乎是.env 中的EXTEND_ESLINT = true。在我这样做之后,我可以在我的package.json 中使用eslinConfig。非常感谢。
    • 注意 EXTEND_ESLINT flag was removed in react-scripts v4 因为不再需要自定义 ESLint 配置
    【解决方案2】:

    该库现在支持原生扩展预定义的 ESLint 规则,请参阅 the relevant docs


    它的要点是你必须设置 EXTEND_ESLINT 环境变量,然后将你自己的 ESLint 配置添加到项目根目录,可选择扩展 create-react-app 的:

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

    【讨论】:

    • 我必须阅读文档。我不确定如何设置 .env 文件。
    【解决方案3】:

    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 使用 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 npm https://www.npmjs.com/package/eslint-config-react-app

    【讨论】:

      【解决方案4】:

      并不难,只需按照以下步骤操作:

      1. npm i -D eslint eslint-plugin-react
      2. npx eslint --init
      3. 编辑生成的配置文件,例如.eslintrc.json
      4. 将您的规则放在"rules": { ... } 部分

      【讨论】:

      猜你喜欢
      • 2017-12-04
      • 2019-10-14
      • 2020-07-23
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多