网站搜索

javascript - 从 Create React App 扩展 TypeScript ESLint 配置


我已经按照这个指南来扩展我的 ESLint 配置。

https://create-react-app.dev/docs/setting-up-your-editor/#experimental-extending-the-eslint-config

文档中说明 请注意,任何设置为 "error" 的规则都会阻止项目构建。我在与我的 package.json 文件相同的文件夹中添加了一个 .env 文件,并写入了 EXTEND_ESLINT=true。

https://create-react-app.dev/docs/advanced-configuration/

一旦添加了此文件,我就不再收到来自命令 npm run build 的警告,即使使用 package.json 中的默认值时也是如此:

"eslintConfig": {
  "extends": "react-app"
},

如果我将 .env 文件设置为 EXTEND_ESLINT=false,警告就会再次出现。

最初之所以要这样做,是因为我想像这样添加 no-multiple-empty-lines 规则:

"eslintConfig": {
  "extends": [ "react-app" ],
  "rules": {
    "no-multiple-empty-lines": [
      "error",
      {
        "max": 1,
        "maxEOF": 0,
        "maxBOF": 0
      }
    ]
  }
},

https://eslint.org/docs/rules/no-multiple-empty-lines

我认为配置是正确的,因为当我添加这条规则时,下面的命令会显示预期的结果:

npx eslint . --ext .js,.jsx,.ts,.tsx

配置更改前:

配置更改后:

阅读关于.env文件的资料时,它说明npm run build会按以下顺序读取文件:.env.production.local, .env.production, .env.local, .env。由于我只有.env,它应该会被读取。文档中还在EXTEND_ESLINT上方说明了这一点——对于下面的变量,你不需要像自定义环境变量那样声明 REACT_APP_ 前缀。

https://create-react-app.dev/docs/adding-custom-environment-variables/#adding-development-environment-variables-in-env

我也读过这个帖子,但我看不出我哪里错了:

ESLint 如何集成到 Create React App 中?

如果我在 package.json 中编辑 scripts 部分为 "build": "set EXTEND_ESLINT=true && react-scripts build",我会收到正常的警告,但我添加的规则无论如何都不会被执行。

相关文章