自定义
概览 📋
内容 👹
基准方案
安装
基础包
pnpm add -D prettier eslint eslint-config-prettier eslint-plugin-prettier husky lint-staged eslint-config-airbnb-base eslint-plugin-import
1
2
3
自动化
- 配置命名,执行 parepare 生成 husky
{
"scripts": {
"prepare": "husky install"
}
}
1
2
3
4
5
- 执行以下命令, 生成 git hooks pre-commit
npx husky add .husky/pre-commit "npx lint-staged"
1
基础配置
lint-staged 配置具体平台
- 新增文件 lint-staged.config.js 进行配置
module.exports = {
"src/**": [
"eslint --fix",
"git add"
]
}
1
2
3
4
5
6
prettier 配置
module.exports = {
printWidth: 120,
tabWidth: 4,
useTabs: false,
semi: false,
singleQuote: true ,
quoteProps: 'as-needed',
jsxSingleQuote: true,
trailingComma: 'es5',
bracketSpacing: true,
bracketSameLine: false,
arrowParens: 'always',
rangeStart: 0,
rangeEnd: Infinity,
proseWrap: 'always',
htmlWhitespaceSensitivity: 'css',
vueIndentScriptAndStyle: false,
endOfLine: 'lf',
embeddedLanguageFormatting:'auto',
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
eslint 配置命名
module.exports = {
root: true,
parserOptions: {
sourceType: 'module'
},
env: {
browser: true,
node: true,
es6: true,
},
extends: ['airbnb-base', 'xxx', 'plugin:prettier/recommended'],
plugins: ["prettier"],
rules: {
"prettier/prettier": "error"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
eslint 忽略文件配置
Vue 或 React
"extends": [
"plugin:react/recommended",
],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react",
],
1
2
3
4
5
6
7
8
9
10
11
12
13
"extends": [
"plugin:vue/recommended",
],
"plugins": [
"vue",
],
1
2
3
4
5
6
eslint 参考方案
Javascript Style
方案1
方案2
eslint-config-airbnb-base
1
2
vue
方案1
eslint-config-airbnb-base + eslint-plugin-vue
1
2
react
方案1