esLint 介绍
团队开发 ,统一代码风格
命令介绍 https://eslint.org/docs/user-guide/command-line-interface
安装 & 初始化配置文件
全局安装
1. npm install -g eslint
2. eslint –init
然后一系列的配置
本地项目
1. npm install eslint –save-dev
2. ./node_modules/.bin/eslint –init (需要当前项目路径)
然后一系列配置
配置
#### eslintrc文件配置
规则配置文件 , 通过上面 init 方式配置的都是在项目最外层生成一个配置文件, (当然你也可以在package.json)
官方文档 https://eslint.org/docs/user-guide/configuring
1 | { |
部分文件忽略检测
在 eslintrc同目录下创建 .eslintignore 文件
1 | # /node_modules/* and /bower_components/* 这个是默认忽略的 |
手动检测代码,自动修复格式化
1 | eslint --ext src //手动检测src文件夹下的.js --ext 默认 .js |
结合webpack使用
安装一个eslint-loader 的loader
1 | module:{ |
git 代码提交前检测
这个时候不得不提一下git hook了(https://git-scm.com/book/zh/v2/%E8%87%AA%E5%AE%9A%E4%B9%89-Git-Git-%E9%92%A9%E5%AD%90)
查看当前目录下的hook文件 cd .git/hooks && ls
然后看到 很多.sample 的文件,前面对应git 钩子一些操作, 而 .sample 后缀都是未启动状态,对应的钩子要生效,需要用shell脚本把.sample去掉,如何用js去掉,
这里才是重点 (⚠️必须在git仓库添加之后)
安装pre-commit
1
npm install pre-commit --save-dev
配置package.json
1 | 第一种//执行静态文件检查 |
制定自己的规则
https://eslint.org/docs/user-guide/command-line-interface
其他的配置
- standardjs:https://standardjs.com/readme-zhcn.html (集成度很好 ,包括git hook)
- prettier:https://zhuanlan.zhihu.com/p/34188596