eslint生成配置
pnpx eslint --init
按照如下步骤执行
1. 选择第二项:只检测代码和找到不符合eslint的部分
npx: 88 安装成功,用时 20.076 秒 How would you like to use ESLint? … To check syntax only ❯ To check syntax and find problems To check syntax, find problems, and enforce code style
2. js标准 选择es6 module
What type of modules does your project use? …
❯ JavaScript modules (import/export)
CommonJS (require/exports)
None of these
3. 选择vue.js框架
? Which framework does your project use? …
React
❯ Vue.js
None of these
4. 选择是否使用typescript, 前提是typescirpt已经存在该依赖不然在第七步会报错
Does your project use TypeScript? · No / Yes
5. 选择代码运行环境
? Where does your code run? … (Press <space> to select, <a> to toggle all, <i> to invert selection)
✔ Browser
✔ Node
6. 选择配置eslint配置文件的格式, 这里建议使用javascirpt的格式
What format do you want your config file to be in? … ❯ JavaScript YAML JSON
7. 是否使用npm立即下载,这里我们选择是
? Would you like to install them now with npm? › No / Yes
8. 安装完成
9. 项目下会生成一个.eslintrc.js文件
module.exports = { "env": { "browser": true, "es2021": true, "node": true }, "extends": [ "eslint:recommended", "plugin:vue/essential", "plugin:@typescript-eslint/recommended" ], "parserOptions": { "ecmaVersion": 13, "parser": "@typescript-eslint/parser", "sourceType": "module" }, "plugins": [ "vue", "@typescript-eslint" ], "rules": { } }
10. package.json文件依赖
"devDependencies": { "@typescript-eslint/eslint-plugin": "^5.8.0", "@typescript-eslint/parser": "^5.8.0", "eslint": "^8.5.0", "eslint-plugin-vue": "^8.2.0", "typescript": "^4.5.4" }
11. 适配vue3, 看下eslint-plugin-vue 官网:https://eslint.vuejs.org/user-guide/#usage
module.exports = { "env": { "browser": true, "es2021": true }, "extends": [ "eslint:recommended", - "plugin:vue/essential", + 'plugin:vue/vue3-recommended', "plugin:@typescript-eslint/recommended" ], "parserOptions": { "ecmaVersion": 13, "parser": "@typescript-eslint/parser", "sourceType": "module" }, "plugins": [ - "vue", "@typescript-eslint" ], "rules": { } };
12. eslint配置好了,再配置prettier
pnpm install prettier eslint-config-prettier eslint-plugin-prettier -D
13. 修改eslint,在.eslintrc.js文件中的extends中加入
module.exports = { ..., extends: [ ..., 'prettier', ... ] }
14. 创建.prettierrc.js文件
module.exports = { // 一行最多 120 字符 printWidth: 120, // 使用 2 个空格缩进 tabWidth: 2, // 不使用缩进符,而使用空格 useTabs: false, // 行尾需要有分号 semi: false, // 使用单引号 singleQuote: true, // 对象的 key 仅在必要时用引号 quoteProps: 'as-needed', // jsx 不使用单引号,而使用双引号 jsxSingleQuote: false, // 末尾不需要逗号 trailingComma: 'none', // 大括号内的首尾需要空格 bracketSpacing: true, // jsx 标签的反尖括号需要换行 // jsxBracketSameLine: false, // 箭头函数,只有一个参数的时候,也需要括号 arrowParens: 'always', // 每个文件格式化的范围是文件的全部内容 rangeStart: 0, // 不需要写文件开头的 @prettier requirePragma: false, // 不需要自动在文件开头插入 @prettier insertPragma: false, // 使用默认的折行标准 proseWrap: 'preserve', // 根据显示样式决定 html 要不要折行 htmlWhitespaceSensitivity: 'css', // 换行符使用 lf endOfLine: 'lf' }
15. 在packages.json中添加scripts
{ ..., "scripts": { "prettier:lint": "prettier --write ." }, ... }
16. 添加husky lint-staged
pnpm add -D lint-staged husky pnpm set-script prepare "husky install" pnpm run prepare pnpx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"' // 给package.json里修改些相应的内容 { ..., "scripts": { ... "lint": "eslint "{src,packages}/**/*.{vue,js,ts,tsx}" --fix" ... }, "lint-staged": { "*.{js,ts,vue,jsx,tsx}": [ "prettier --write .", "pnpm run lint" ] } ... }
17. 完整的依赖项为
{ "name": "", "version": "0.0.1", "description": "", "main": "lib/index.js", "directories": { "example": "examples" }, "scripts": { "bootstrap": "pnpm install", "prepare": "husky install", "lint": "eslint "{src,packages}/**/*.{vue,js}" --fix" }, "repository": { "type": "git", "url": "https://gitee.com/sdjnduantong/xxxx.git" }, "keywords": [ ], "author": "duantong", "license": "MIT", "engines": { "node": ">= 12.20.0" }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^5.8.0", "@typescript-eslint/parser": "^5.8.0", "eslint": "^8.5.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^4.0.0", "eslint-plugin-vue": "^8.2.0", "husky": "^7.0.4", "lint-staged": "^12.1.3", "prettier": "^2.5.1", "typescript": "^4.5.4" }, "lint-staged": { "*.{js,vue,jsx}": [ "prettier --write .", "pnpm run lint" ] } }
18. git hooks介绍
git-hooks | 解释 |
---|---|
pre-commit | 该钩子在键入提交信息前运行。 它用于检查即将提交的快照。如果该钩子以非零值退出,Git 将放弃此次提交,你可以利用该钩子,来检查代码风格是否一致 |
prepare-commit-msg | 该钩子在启动提交信息编辑器之前,默认信息被创建之后运行。 它允许你编辑提交者所看到的默认信息 |
commit-msg | 该钩子接收一个参数,此参数存有当前提交信息的临时文件的路径。 如果该钩子脚本以非零值退出,Git 将放弃提交,因此,可以用来在提交通过前验证项目状态或提交信息 |
post-commit | 该钩子一般用于通知之类的事情 |