前端 Vue 16.eslint+stylelint+prettier全流程配置 墨颜丶 2023-04-03 2024-11-12 eslint配置 eslint中文官网:http://eslint.cn/
ESLint最初是由Nicholas C. Zakas 于2013年6月创建的开源项目。它的目标是提供一个插件化的javascript代码检测工具
首先安装eslint
生成配置文件:.eslint.cjs
.eslint.cjs配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 module.exports = { "env" : { "browser" : true , "es2021" : true , } , "extends" : [ "eslint:recommended" , "plugin:vue/vue3-essential" , "plugin:@typescript-eslint/recommended" ] , "overrides" : [ ] , "parser" : "@typescript-eslint/parser" , "parserOptions" : { "ecmaVersion" : "latest" , "sourceType" : "module" } , "plugins" : [ "vue" , "@typescript-eslint" ] , "rules" : { } }
vue3环境代码校验插件 1 2 3 4 5 6 7 8 9 10 # 让所有与prettier规则存在冲突的Eslint rules失效,并使用prettier进行代码检查 "eslint-config-prettier" : "^8.6.0" , "eslint-plugin-import" : "^2.27.5" , "eslint-plugin-node" : "^11.1.0" , # 运行更漂亮的Eslint,使prettier规则优先级更高,Eslint优先级低 "eslint-plugin-prettier" : "^4.2.1" , # vue.js的Eslint插件(查找vue语法错误,发现错误指令,查找违规风格指南 "eslint-plugin-vue" : "^9.9.0" , # 该解析器允许使用Eslint校验所有babel code "@babel/eslint-parser" : "^7.19.1" ,
安装指令
1 pnpm install -D eslint-plugin-import eslint-plugin-vue eslint-plugin-node eslint-plugin-prettier eslint-config-prettier eslint-plugin-node @babel/eslint-parser
修改.eslintrc.cjs
配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 module.exports = { env: { browser: true , es2021: true , node: true , jest: true , } , parser: 'vue-eslint-parser', parserOptions: { ecmaVersion: 'latest', sourceType: 'module', parser: '@typescript-eslint/parser', jsxPragma: 'React', ecmaFeatures: { jsx: true , } , } , extends: [ 'eslint: recommended', 'plugin: vue/vue3-essential', 'plugin: @typescript-eslint/recommended', 'plugin: prettier/recommended', ] , plugins: [ 'vue', '@typescript-eslint'] , rules: { 'no-var': 'error', 'no-multiple-empty-lines': [ 'warn', { max: 1 } ] , 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off', 'no-unexpected-multiline': 'error', 'no-useless-escape': 'off', '@typescript-eslint/no-unused-vars': 'error', '@typescript-eslint/prefer-ts-expect-error': 'error', '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-non-null -assertion': 'off', '@typescript-eslint/no-namespace': 'off', '@typescript-eslint/semi': 'off', 'vue/multi-word-component-names': 'off', 'vue/script-setup-uses-vars': 'error', 'vue/no-mutating-props': 'off', 'vue/attribute-hyphenation': 'off', } , }
.eslintignore
忽略文件运行脚本 package.json新增两个运行脚本
eslint src
校验src文件夹
--fix
不符合规则的 自动去修补
1 2 3 4 "scripts" : { "lint" : "eslint src" , "fix" : "eslint src --fix" , }
配置prettier 有了eslint,为什么还要有prettier?eslint针对的是javascript,他是一个检测工具,包含js语法以及少部分格式问题,在eslint看来,语法对了就能保证代码正常运行,格式问题属于其次;
而prettier属于格式化工具,它看不惯格式不统一,所以它就把eslint没干好的事接着干,另外,prettier支持
包含js在内的多种语言。
总结起来,eslint和prettier这俩兄弟一个保证js代码质量,一个保证代码美观。
安装依赖包 1 pnpm install -D eslint-plugin-prettier prettier eslint-config-prettier
.prettierrc.json
添加规则1 2 3 4 5 6 7 8 9 { "singleQuote" : true , "semi" : false , "bracketSpacing" : true , "htmlWhitespaceSensitivity" : "ignore" , "endOfLine" : "auto" , "trailingComma" : "all" , "tabWidth" : 2 }
.prettierignore
忽略文件1 2 3 4 5 6 7 /dist/* /html/* .local /node_modules/** **/*.svg **/*.sh /public/*
通过pnpm run lint去检测语法,如果出现不规范格式,通过pnpm run fix 修改
配置stylelint stylelint 为css的lint工具。可格式化css代码,检查css语法错误与不合理的写法,指定css书写顺序等。
我们的项目中使用scss作为预处理器,安装以下依赖:
1 pnpm add sass sass-loader stylelint postcss postcss-scss postcss-html stylelint-config-prettier stylelint-config-recess-order stylelint-config-recommended-scss stylelint-config-standard stylelint-config-standard-vue stylelint-scss stylelint-order stylelint-config-standard-scss -D
.stylelintrc.cjs
配置文件 官网:https://stylelint.bootcss.com/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 module.exports = { extends: [ 'stylelint-config-standard', 'stylelint-config-html/vue', 'stylelint-config-standard-scss', 'stylelint-config-recommended-vue/scss', 'stylelint-config-recess-order', 'stylelint-config-prettier', ] , overrides: [ { files: [ '** *.(html|vue)'] , customSyntax: 'postcss-html', } , ] , ignoreFiles: [ '** *.jsx', '** *.ts', '** *.md', '** rules: { 'value-keyword-case': null , 'no-descending-specificity': null , 'function-url-quotes': 'always', 'no-empty-source': null , 'selector-class-pattern': null , 'property-no-unknown': null , 'block-opening-brace-space-before': 'always', 'value-no-vendor-prefix': null , 'property-no-vendor-prefix': null , 'selector-pseudo-class-no-unknown': [ true , { ignorePseudoClasses: [ 'global', 'v-deep', 'deep'] , } , ] , } , }
.stylelintignore
忽略文件1 2 3 4 /node_modules/* /dist/* /html/* /public/*
运行脚本 1 2 3 "scripts" : { "lint:style" : "stylelint src/**/*.{css,scss,vue} --cache --fix" }
最后配置统一的prettier来格式化我们的js和css,html代码
1 2 3 4 5 6 7 8 9 10 "scripts" : { "dev" : "vite --open" , "build" : "vue-tsc && vite build" , "preview" : "vite preview" , "lint" : "eslint src" , "fix" : "eslint src --fix" , "format" : "prettier --write \"./**/*.{html,vue,ts,js,json,md}\"" , "lint:eslint" : "eslint src/**/*.{ts,vue} --cache --fix" , "lint:style" : "stylelint src/**/*.{css,scss,vue} --cache --fix" } ,
当我们运行pnpm run format
的时候,会把代码直接格式化
配置husky 在上面我们已经集成好了我们代码校验工具,但是需要每次手动的去执行命令才会格式化我们的代码。如果有人没有格式化就提交了远程仓库中,那这个规范就没什么用。所以我们需要强制让开发人员按照代码规范来提交。
要做到这件事情,就需要利用husky在代码提交之前触发git hook(git在客户端的钩子),然后执行pnpm run format
来自动的格式化我们的代码。
安装husky
执行
1 npx husky-init ## 需要先git init初始化哦
会在根目录下生成个一个.husky目录,在这个目录下面会有一个pre-commit文件,这个文件里面的命令在我们执行commit的时候就会执行
在.husky/pre-commit
文件添加如下命令:
1 2 3 # !/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" pnpm run format
当我们对代码进行commit操作的时候,就会执行命令,对代码进行格式化,然后再提交。
配置commitlint 对于我们的commit信息,也是有统一规范的,不能随便写,要让每个人都按照统一的标准来执行,我们可以利用commitlint 来实现。
安装包
1 pnpm add @commitlint/config-conventional @commitlint/cli -D
添加配置文件,新建commitlint.config.cjs
(注意是cjs),然后添加下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 module.exports = { extends: [ '@commitlint/config-conventional'] , rules: { 'type-enum': [ 2 , 'always', [ 'feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'chore', 'revert', 'build', ] , ] , '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 ] , } , }
在package.json
中配置scripts命令
1 2 3 4 5 6 # 在scrips中添加下面的代码 { "scripts" : { "commitlint" : "commitlint --config commitlint.config.cjs -e -V" } , }
配置结束,现在当我们填写commit
信息的时候,前面就需要带着下面的subject
1 2 3 4 5 6 7 8 9 10 'feat',//新特性、新功能 'fix',//修改bug 'docs',//文档修改 'style',//代码格式修改, 注意不是 css 修改 'refactor',//代码重构 'perf',//优化相关,比如提升性能、体验 'test',//测试用例修改 'chore',//其他修改, 比如改变构建流程、或者增加依赖库、工具等 'revert',//回滚到上一个版本 'build',//编译相关的修改,例如发布版本、对项目构建或者依赖的改动
配置husky
1 npx husky add .husky/commit-msg
在生成的commit-msg文件中添加下面的命令
1 2 3 #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" pnpm commitlint
当我们 commit 提交信息时,就不能再随意写了,必须是 git commit -m ‘fix: xxx’ 符合类型的才可以,需要注意的是类型的后面需要用英文的 :,并且冒号后面是需要空一格的,这个是不能省略的
强制使用pnpm包管理器工具 团队开发项目的时候,需要统一包管理器工具,因为不同包管理器工具下载同一个依赖,可能版本不一样,
导致项目出现bug问题,因此包管理器工具需要统一管理!!!
在根目录创建scritps/preinstall.js
文件,添加下面的内容
1 2 3 4 5 6 7 if (!/pnpm/ .test (process.env .npm_execpath || '' )) { console .warn ( `\u001b[33mThis repository must using pnpm as the package manager ` + ` for scripts to work properly.\u001b[39m\n` , ) process.exit (1 ) }
配置命令
1 2 3 "scripts" : { "preinstall" : "node ./scripts/preinstall.js" }
当我们使用npm或者yarn来安装包的时候,就会报错了。原理就是在install的时候会触发preinstall(npm提供的生命周期钩子)这个文件里面的代码。