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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。