项目初始化
npm init vite@latest test-vue -- --template vue-ts
cd test-vue
npm install
npm run dev
editorconfig
作用:有助于为不同的IDE编辑器处理同一项目的多个开发人员维护一致的编码风格。
- 新建
.editorconfig
配置文件
# Editor配置项, 请参考 http://editorconfig.org
# 表示是最顶层的 EditorConfig 配置文件
root = true
[*] # 表示所有文件适用
charset = utf-8 # 设置文件字符集为 utf-8
indent_style = space # 缩进风格(tab | space)
indent_size = 2 # 缩进大小
end_of_line = lf # 控制换行类型(lf | cr | crlf)
trim_trailing_whitespace = true # 去除行首的任意空白字符
insert_final_newline = true # 始终在文件末尾插入一个新行
[*.md] # 表示仅 md 文件适用以下规则
max_line_length = off
trim_trailing_whitespace = false
ESLint
- 安装相关依赖
# ESLint核心库
npm install eslint -D
# ESLint的TS依赖
npm install @types/eslint -D
# node依赖
npm install @types/node -D
# ESLint结合Vue代码规范
npm install eslint-plugin-vue -D
# ESLint结合Airbnb代码规范(依赖plugin-import)
npm install eslint-config-airbnb-base eslint-plugin-import -D
# 由于ESLint默认使用Espree语法解析,是不能识别TypeScript某些语法,所以需安装@typescript-eslint/parser替换掉默认解析器
npm install @typescript-eslint/parser -D
# 补充额外得typescript语法的规则
npm install @typescript-eslint/eslint-plugin -D
# 在import时可以使用@
npm install eslint-import-resolver-alias -D
# 解决ts别名报错问题
npm install eslint-import-resolver-typescript -D
- 初始化
ESLint
npx eslint --init
- problems
- esm
- vue
- yes
- browser,node
- JavaScript
- yes
- npm
- 完整
.eslintrc.cjs
// .eslintrc.cjs
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
'vue/setup-compiler-macros': true,
},
extends: [
'plugin:vue/vue3-recommended',
'plugin:@typescript-eslint/recommended',
'airbnb-base',
'plugin:prettier/recommended',
'prettier',
'eslint:recommended',
],
overrides: [],
parser: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 'latest',
parser: '@typescript-eslint/parser',
sourceType: 'module',
ecmaFeatures: {
tsx: true,
jsx: true,
},
},
plugins: ['vue', '@typescript-eslint', 'prettier'],
settings: {
alias: {
map: [['@', './src']]
},
// 别名import配置
'import/extensions': ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs'],
// 解决ts别名报错配置
'import/resolver':{
typescript: {
directory:'./tsconfig.json'
}
}
},
globals: {
defineProps: 'readonly',
defineEmits: 'readonly',
defineExpose: 'readonly',
withDefaults: 'readonly',
},
// 这里根据自己的项目进行配置
// 0 = off, 1 = warn, 2 = error
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'vue/no-parsing-error': [
2,
{
'x-invalid-end-tag': false,
'missing-semicolon-after-character-reference': false,
},
],
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
'no-unused-expressions': [
'error',
{ allowShortCircuit: true, allowTernary: true },
],
'no-param-reassign': ['error', { props: false }],
'vue/multi-word-component-names': 'off',
'no-new-object': 'off',
'no-explicit-any': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'vue/v-on-event-hyphenation': 'off',
camelcase: 0,
'no-unused-vars': 0,
'@typescript-eslint/no-unused-vars': 0,
'no-return-assign': 0,
'import/extensions': 0,
'vue/require-component-is': 0,
'no-use-before-define': 0,
'import/no-unresolved': 0,
'default-case': 0,
'@typescript-eslint/ban-types': 0,
'no-bitwise': 0,
'no-case-declarations': 0,
'import/prefer-default-export': 0,
'no-shadow': 0,
'@typescript-eslint/no-empty-function': 0,
},
}
- rules常见配置
'vue/no-multiple-template-root': 'off',
'array-bracket-spacing': ['error', 'never'], // 是否允许非空数组里面有多余的空格
'arrow-parens': 'off', // 箭头函数用小括号括起来
'block-spacing': ['error', 'always'], // =>的前/后括号
'accessor-pairs': 'off', // 在对象中使用getter/setter
'block-scoped-var': 'off', // 块语句中使用var
'brace-style': ['warn', '1tabs'], // 大括号风格
'callback-return': 'warn', // 避免多次调用回调什么的
'camelcase': 'error', // 强制驼峰法命名
'comma-dangle': ['error', 'never'], // 对象字面量项尾不能有逗号
'comma-spacing': 'off', // 逗号前后的空格
'comma-style': ['error', 'last'], // 逗号风格,换行时在行首还是行尾
'complexity': ['off', 11], // 循环复杂度
'computed-property-spacing': ['off', 'never'], // 是否允许计算后的键名什么的
'consistent-return': 'off', // return 后面是否允许省略
'consistent-this': ['error', 'that'], // this别名
'constructor-super': 'off', // 非派生类不能调用super,派生类必须调用super
'curly': ['error', 'all'], // 必须使用 if(){} 中的{}
'default-case': 'error', // switch语句最后必须有default
'dot-location': 'off', // 对象访问符的位置,换行的时候在行首还是行尾
'dot-notation': ['off', { 'allowKeywords': true }], // 避免不必要的方括号
'eol-last': 'off', // 文件以单一的换行符结束
'eqeqeq': 'error', // 要求使用 === 和 !==
'func-names': 'off', // 函数表达式必须有名字
'func-style': ['off', 'declaration'], // 函数风格,规定只能使用函数声明/函数表达式
'generator-star-spacing': 'off', // 生成器函数*的前后空格
'guard-for-in': 'off', // for in循环要用if语句过滤
'handle-callback-err': 'off', // nodejs 处理错误
'id-length': 'off', // 变量名长度
'indent': ['error', 4], // 缩进风格
'init-declarations': 'off', // 声明时必须赋初始值
'key-spacing': ['off', { 'beforeColon': false, 'afterColon': true }], // 对象字面量中冒号的前后空格
'lines-around-comment': 'off', // 行前/行后备注
'max-depth': ['off', 4], // 嵌套块深度
'max-len': ['off', 80, 4], // 字符串最大长度
'max-nested-callbacks': ['off', 'error'], // 回调嵌套深度
'max-params': ['off', 3], // 函数最多只能有3个参数
'max-statements': ['off', 10], // 函数内最多有几个声明
'new-cap': 'error', // 函数名首行大写必须使用new方式调用,首行小写必须用不带new方式调用
'new-parens': 'error', // new时必须加小括号
'newline-after-var': 'error', // 变量声明后是否需要空一行
'no-alert': 'off', // 禁止使用alert confirm prompt
'no-array-constructor': 'error', // 禁止使用数组构造器
'no-bitwise': 'off', // 禁止使用按位运算符
'no-caller': 'warn', // 禁止使用arguments.caller或arguments.callee
'no-catch-shadow': 'error', // 禁止catch子句参数与外部作用域变量同名
'no-class-assign': 'error', // 禁止给类赋值
'no-cond-assign': 'error', // 禁止在条件表达式中使用赋值语句
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', // 是否使用 console
'no-const-assign': 'error', // 禁止修改const声明的变量
'no-constant-condition': 'error', // 禁止在条件中使用常量表达式 if(true) if('warn')
'no-continue': 'off', // 禁止使用continue
'no-control-regex': 'error', // 禁止在正则表达式中使用控制字符
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', // 是否使用 debugger
'no-delete-var': 'error', // 不能对var声明的变量使用delete操作符
'no-div-regex': 'warn', // 不能使用看起来像除法的正则表达式/=foo/
'no-dupe-keys': 'error', // 在创建对象字面量时不允许键重复 {a:'warn',a:'warn'}
'no-dupe-args': 'error', // 函数参数不能重复
'no-duplicate-case': 'error', // switch中的case标签不能重复
'no-else-return': 'error', // 如果if语句里面有return,后面不能跟else语句
'no-empty': 'error', // 块语句中的内容不能为空
'no-empty-character-class': 'error', // 正则表达式中的[]内容不能为空
'no-empty-label': 'error', // 禁止使用空label
'no-eq-null': 'error', // 禁止对null使用==或!=运算符
'no-eval': 'warn', // 禁止使用eval
'no-ex-assign': 'error', // 禁止给catch语句中的异常参数赋值
'no-extend-native': 'error', // 禁止扩展native对象
'no-extra-bind': 'error', // 禁止不必要的函数绑定
'no-extra-boolean-cast': 'error', // 禁止不必要的bool转换
'no-extra-parens': 'error', // 禁止非必要的括号
'no-extra-semi': 'error', // 禁止多余的冒号
'no-fallthrough': 'warn', // 禁止switch穿透
'no-floating-decimal': 'error', // 禁止省略浮点数中的'off' .5 3.
'no-func-assign': 'error', // 禁止重复的函数声明
'no-implicit-coercion': 'warn', // 禁止隐式转换
'no-implied-eval': 'error', // 禁止使用隐式eval
'no-inline-comments': 'off', // 禁止行内备注
'no-inner-declarations': ['error', 'functions'], // 禁止在块语句中使用声明(变量或函数)
'no-invalid-regexp': 'error', // 禁止无效的正则表达式
'no-invalid-this': 'error', // 禁止无效的this,只能用在构造器,类,对象字面量
'no-irregular-whitespace': 'error', // 不能有不规则的空格
'no-iterator': 'error', // 禁止使用__iterator__ 属性
'no-label-var': 'error', // label名不能与var声明的变量名相同
'no-labels': 'error', // 禁止标签声明
'no-lone-blocks': 'error', // 禁止不必要的嵌套块
'no-lonely-if': 'error', // 禁止else语句内只有if语句
'no-loop-func': 'warn', // 禁止在循环中使用函数(如果没有引用外部变量不形成闭包就可以)
'no-mixed-requires': ['off', false], // 声明时不能混用声明类型
'no-mixed-spaces-and-tabs': ['error', false], // 禁止混用tab和空格
'linebreak-style': ['off', 'windows'], // 换行风格
'no-multi-spaces': 'warn', // 不能用多余的空格
'no-multi-str': 'error', // 字符串不能用\换行
'no-multiple-empty-lines': ['warn', {'max': 'error'}], // 空行最多不能超过'error'行
'no-native-reassign': 'error', // 不能重写native对象
'no-negated-in-lhs': 'error', // in 操作符的左边不能有!
'no-nested-ternary': 'off', // 禁止使用嵌套的三目运算
'no-new': 'warn', // 禁止在使用new构造一个实例后不赋值
'no-new-func': 'warn', // 禁止使用new Function
'no-new-object': 'error', // 禁止使用new Object()
'no-new-require': 'error', // 禁止使用new require
'no-new-wrappers': 'error', // 禁止使用new创建包装实例,new String new Boolean new Number
'no-obj-calls': 'error', // 不能调用内置的全局对象,比如Math() JSON()
'no-octal': 'error', // 禁止使用八进制数字
'no-octal-escape': 'error', // 禁止使用八进制转义序列
'no-param-reassign': 'error', // 禁止给参数重新赋值
'no-path-concat': 'off', // node中不能使用__dirname或__filename做路径拼接
'no-plusplus': 'off', // 禁止使用++,--
'no-process-env': 'off', // 禁止使用process.env
'no-process-exit': 'off', // 禁止使用process.exit()
'no-proto': 'error', // 禁止使用__proto__属性
'no-redeclare': 'error', // 禁止重复声明变量
'no-regex-spaces': 'error', // 禁止在正则表达式字面量中使用多个空格 /foo bar/
'no-restricted-modules': 'off', // 如果禁用了指定模块,使用就会报错
'no-return-assign': 'warn', // return 语句中不能有赋值表达式
'no-script-url': 'off', // 禁止使用javascript:void('off')
'no-self-compare': 'error', // 不能比较自身
'no-sequences': 'off', // 禁止使用逗号运算符
'no-shadow': 'error', // 外部作用域中的变量不能与它所包含的作用域中的变量或参数同名
'no-shadow-restricted-names': 'error', // 严格模式中规定的限制标识符不能作为声明时的变量名使用
'no-spaced-func': 'error', // 函数调用时 函数名与()之间不能有空格
'no-sparse-arrays': 'error', // 禁止稀疏数组, ['warn',,'error']
'no-sync': 'off', // nodejs 禁止同步方法
'no-ternary': 'off', // 禁止使用三目运算符
'no-trailing-spaces': 'warn', // 一行结束后面不要有空格
'no-this-before-super': 'off', // 在调用super()之前不能使用this或super
'no-throw-literal': 'error', // 禁止抛出字面量错误 throw 'error';
'no-undef': 'warn', // 不能有未定义的变量
'no-undef-init': 'error', // 变量初始化时不能直接给它赋值为undefined
'no-undefined': 'error', // 不能使用undefined
'no-unexpected-multiline': 'error', // 避免多行表达式
'no-underscore-dangle': 'warn', // 标识符不能以_开头或结尾
'no-unneeded-ternary': 'error', // 禁止不必要的嵌套 var isYes = answer === 'warn' ? true : false;
'no-unreachable': 'error', // 不能有无法执行的代码
'no-unused-expressions': 'error', // 禁止无用的表达式
'no-unused-vars': ['error', {'vars': 'all', 'args': 'after-used'}], // 不能有声明后未被使用的变量或参数
'no-use-before-define': 'error', // 未定义前不能使用
'no-useless-call': 'error', // 禁止不必要的call和apply
'no-void': 'error', // 禁用void操作符
'no-var': 'off', // 禁用var,用let和const代替
'no-warning-comments': ['warn', { 'terms': ['todo', 'fixme', 'xxx'], 'location': 'start' }], // 不能有警告备注
'no-with': 'error', // 禁用with
'object-curly-spacing': ['off', 'never'], // 大括号内是否允许不必要的空格
'object-shorthand': 'off', // 强制对象字面量缩写语法
'one-var': 'warn', // 连续声明
'operator-assignment': ['off', 'always'], // 赋值运算符 += -=什么的
'operator-linebreak': ['error', 'after'], // 换行时运算符在行尾还是行首
'padded-blocks': 'off', // 块语句内行首行尾是否要空行
'prefer-const': 'off', // 首选const
'prefer-spread': 'off', // 首选展开运算
'prefer-reflect': 'off', // 首选Reflect的方法
'quotes': ['warn', 'single'], // 引号类型 `` '' ''
'quote-props':['error', 'always'], // 对象字面量中的属性名是否强制双引号
'radix': 'error', // parseInt必须指定第二个参数
'id-match': 'off', // 命名检测
'require-yield': 'off', // 生成器函数必须有yield
'semi': ['error', 'always'], // 语句强制分号结尾
'semi-spacing': ['off', {'before': false, 'after': true}], // 分号前后空格
'sort-vars': 'off', // 变量声明时排序
'space-after-keywords': ['off', 'always'], // 关键字后面是否要空一格
'space-before-blocks': ['off', 'always'], // 不以新行开始的块{前面要不要有空格
'space-before-function-paren': ['off', 'always'], // 函数定义时括号前面要不要有空格
'space-in-parens': ['off', 'never'], // 小括号里面要不要有空格
'space-infix-ops': 'off', // 中缀操作符周围要不要有空格
'space-return-throw-case': 'error', // return throw case后面要不要加空格
'space-unary-ops': ['off', { 'words': true, 'nonwords': false }], // 一元运算符的前/后要不要加空格
'spaced-comment': 'off', // 注释风格要不要有空格什么的
'strict': 'error', // 使用严格模式
'use-isnan': 'error', // 禁止比较时使用NaN,只能用isNaN()
'valid-jsdoc': 'off', // jsdoc规则
'valid-typeof': 'error', // 必须使用合法的typeof的值
'vars-on-top': 'error', // var必须放在作用域顶部
'vue/require-v-for-key': 'off', // vue的for循环是否必须有key
'wrap-iife': ['error', 'inside'], // 立即执行函数表达式的小括号风格
'wrap-regex': 'off', // 正则表达式字面量用小括号包起来
'yoda': ['error', 'never'] // 禁止尤达条件
- 修改
package.json
{
"scripts": {
"lint": "eslint . --ext .vue,.js,.ts,.jsx,.tsx --fix"
}
}
- 新建
.eslintignore
忽略文件
node_modules/*
index.html
dist/*
/public/*
/docs/*
/bin/*
.husky/*
.idea/*
.vscode/*
*.md
*.woff
*.ttf
*.d.ts
*.sh
*.log
Prettier
Prettier 是一款强大的代码格式化工具,支持 JavaScript、TypeScript、CSS、SCSS、Less、JSX、Angular、Vue、GraphQL、JSON、Markdown 等语言,基本上前端能用到的文件格式它都可以搞定,是当下最流行的代码格式化工具。Prettier支持 .json
、 .yaml
、 .yml
、 .js
等格式的配置文件。
- 安装相关依赖
npm install prettier -D
- 创建
.prettierrc.cjs
配置文件
module.exports = {
// 单行输出长度
printWidth: 80,
// 不使用缩进符,使用空格
useTabs: false,
// 缩进空格数
tabWidth: 2,
// 是否在语句末尾分号
semi: false,
// 是否使用单引号
singleQuote: true,
// 尽在需要时在对象属性周围添加引号 可选值"<as-needed|consistent|preserve>"
quoteProps: 'as-needed',
// 去除对象最末尾元素跟随的逗号 可选值"<none|es5|all>",默认none
trailingComma: 'all',
// 箭头函数,只有一个参数的时候,也需要括号
arrowParens: 'always',
// 每个文件格式化的范围是文件的全部内容
rangeStart: 0,
rangeEnd: Infinity,
// 默认折行标准 always\never\preserve
proseWrap: 'always',
// 指定要使用的解析器,不需要写文件开头的 @prettier
requirePragma: false,
// 不需在开头自动插入@prettier
insertPragma: false,
// 换行符使用 lf 可选值"<auto|lf|crlf|cr>"
endOfLine: 'auto',
// 是否在对象属性添加空格
bracketSpacing: true,
// 指定 HTML 文件的全局空白区域敏感度, "ignore" - 空格被认为是不敏感的
htmlWhitespaceSensitivity: 'css',
// jsx 不使用单引号,而使用双引号
jsxSingleQuote: false,
// Vue文件脚本和样式标签缩进
vueIndentScriptAndStyle: false,
}
- 创建
.prettierignore
忽略文件
/dist/*
.local
.output.js
/node_modules/**
**/*.svg
**/*.sh
public/*
*.d.ts
Vite 集成 ESLint
- 安装插件
npm install vite-plugin-eslint
- 修改
vite.config.ts
import eslintPlugin from 'vite-plugin-eslint'
plugins: [
eslintPlugin({
include: ['src/**/*.ts', 'src/**/*.vue', 'src/*.ts', 'src/*.vue'],
cache: false,
})
]
- 修改
package.json
{
"scripts": {
"prettier": "prettier --write ./**/*.{vue,ts,tsx,js,jsx,css,less,scss,json,md}"
}
}
解决 ESLint 与 Prettier 冲突
在使用 eslint
和 prettier
时会存在一些规则上的冲突,此时就需要用到 eslint-plugin-prettier
和 eslint-config-prettier
。以形成prettier规则>eslint规则
- 安装插件
# 关闭所有不必要或可能跟prettier产生冲突的规则
npm install eslint-config-prettier -D
# 可以让eslint使用prettier规则进行检查
npm install eslint-plugin-prettier -D
.eslintrc.cjs
添加依赖
module.exports = {
extends: [
//注意该插件必须放在最后
'plugin:prettier/recommended'
],
}
husky + lint-staged
husky , Git Hook 工具,可以设置在 git 各个阶段(pre-commit
、commit-msg
、pre-push
等)触发我们的命令。主要为了解决配置eslint后仍不能将eslint中的问题解决掉。我们需要提交的代码符合eslint代码规范,则需要执行git commit
命令时对其进行校验,如若不符合eslint规范就会自动进行修复。
lint-staged 这个工具一般结合 husky 来使用,它可以让 husky 的 hook 触发的命令只作用于 git add那些文件(即 git 暂存区的文件),而不会影响到其他文件。
- 安装依赖
npm install husky lint-staged -D
- 初始化
husky
npx husky-init
- 修改
.husky/pre-commit
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx lint-staged
- 修改
package.json
表示在保存提交代码时会按顺序执行lint-staged下的命令
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,vue,ts,jsx,tsx}": [
"npm run lint",
"npm run prettier"
],
"*.{html,css,less,scss,md}": [
"npm run prettier"
]
}
}
commitlint
用于再提交时检查是否满足规定的格式。
- 安装依赖
npm install @commitlint/config-conventional @commitlint/cli -D
- 添加
commit-msg
钩子
npx husky add .husky/commit-msg "npx --no -- commitlint --edit ${1}"
- 创建
commitlint.config.cjs
// commitlint.config.cjs
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'build', // 编译相关修改(新版本发布)
'feat', // 新功能
'fix', // 修复bug
'update', // 更新某功能
'refactor', // 重构
'docs', // 文档
'chore', // 增加依赖或库
'style', // 格式(不影响代码变动)
'revert', // 撤销commit 回滚上一版本
'perf', // 性能优化
'test', // 测试单元
],
],
'type-case': [0],
'type-empty': [0],
'scope-empty': [0],
'scope-case': [0],
'subject-full-stop': [0, 'never'],
'subject-case': [0, 'never'],
'header-max-length': [0, 'always', 72],
},
}