优化前端工作流:二、使用lint-staged对将要提交的内容进

2020-05-28  本文已影响0人  高级程序狗

lint-staged介绍

lint-staged是一个只对将要通过git提交的文件(staged),执行自定义脚本的工具。初衷是配合lint使用,但是不一定仅限于lint。这里的说明有点绕,看官网介绍可能更清晰:

Linting makes more sense when run before committing your code. By doing so you can ensure no errors go into the repository and enforce code style. But running a lint process on a whole project is slow and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.
This project contains a script that will run arbitrary shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.

lint-staged使用

lint-staged通常都是配配合husky使用,非常简单:

//package.json
{
  "lint-staged": {
    "src/**/*.{js,jsx,ts,tsx}": [
      "eslint --max-warnings=0"
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  }
  //...
}

对于multi package monorepo的情况,可参考此项目配置:https://github.com/sudo-suhas/lint-staged-multi-pkg

上一篇 下一篇

猜你喜欢

热点阅读