网站搜索

reactjs - 当 ESLint 有警告时,如何停止 npm 构建?


在处理一个非常不成熟的团队时,我希望当 ESLint 给出警告时,React-Typescript 构建失败。

src/modules/security/components/ForgotPasswordBox/index.tsx
  Line 8:18:  'FormikHelpers' is defined but never used  @typescript-eslint/no-unused-vars

src/modules/security/components/OTPVerificationBox/modalverficationcode.tsx
  Line 54:18:  Expected '!==' and instead saw '!='  eqeqeq
  Line 63:29:  Expected '===' and instead saw '=='  eqeqeq

但在网上我只找到相反的情况,人们想要关闭警告或即使有警告也要继续构建,这正好与我想要的相反。

我不使用构建管道或类似的东西。我们有一个使用 NPM 作为包管理器的 React-TypeScript 应用。

 "scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint . --max-warnings=0"
},

  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },

请告诉我如何才能实现这种行为?请注意,TypeScript 编译器设置中的 strict 已经设置为 true。

答案#1

与其让你的build脚本失败,你可以更新你的CI以运行npm run lint && npm run build,或者创建一个新的脚本供CI使用(例如"production": "npm run lint && npm run build")。

这样,CI 构建将在 lint 脚本上失败,甚至在开始 build 脚本之前。这会暴露逻辑,而不是将其隐藏在 build 脚本中。


将来你可以考虑并行运行这些,并让其中任何一个失败都会导致 CI 构建失败和/或要求通过 GitHub 状态检查(基于每个脚本的成功/失败)。