- 创建项目和配置ESLint校验规范

项目初始化

创建项目

  1. 首先确定项目是vite+vue+TS完成的。
  2. 检查node.js版本
  1. node -v
  1. 安装vite
  1. npm init vite@latest
  2. //我并没安装 于是按y进行安装
  3. //安装完成后,选择项目名字 我选择了shop-admin
  4. //选择框架 我选择了vue
  5. //选择vue or vue-ts 我这个项目想用ts,于是选择vue-ts

1659573196752.png

项目就创建成功啦!

  1. 安装依赖。
  1. npm i
  2. //中途我网络太差了,报错
  3. npm ERR! code ERR_SOCKET_TIMEOUT
  4. npm ERR! network Socket timeout
  5. npm ERR! network This is a problem related to network connectivity.
  6. npm ERR! network In most cases you are behind a proxy or have bad network settings.
  7. npm ERR! network
  8. npm ERR! network If you are behind a proxy, please make sure that the
  9. npm ERR! network 'proxy' config is set properly. See: 'npm help config'
  10. npm ERR! A complete log of this run can be found in:
  11. npm ERR! C:\Users\77518\AppData\Local\npm-cache\_logs\2022-08-03T00_58_03_570Z-debug-0.log
  12. //问题不大,重复安装,再来一次
  13. npm i
  14. added 1 package, removed 22 packages, and changed 8 packages in 3m
  15. 4 packages are looking for funding
  16. run `npm fund` for details
  17. //完事
  1. 打开项目
  1. npm run dev
  2. //以下就是完成信息
  3. VITE v3.0.4 ready in 4154 ms
  4. Local: http://127.0.0.1:5173/
  5. Network: use --host to expose
  6. //点击local的地址就是我们项目的地址了

初始项目目录

分别是node_modules, public, src文件夹

  • node_module 第三方包的存储目录
  • public 用来放置不需要编译构建的纯静态资源
  • src包含了基本所有需要构建的资源
    • src的main.ts就是项目启动入口
    • App.vue项目的根组件
    • shims-vue.d.ts/vite-env.d.ts ts相关的文件
    • assets文件夹主要也是放置纯静态资源
    • components就组件库
  • gitignore主要是放置不需要git版本管理的
  • index.html单页面文件的主页
  • package-lock.json package.json与npm相关的文件

    比如,一些命令和依赖相关信息

  1. "scripts": {//npm脚本
  2. //输入比较麻烦的一些命令配置
  3. "dev": "vite",
  4. //发布上线的build命令 把源码构建成可以浏览器直接运行的结果,校验ts类型,类型通过则vite build,否则就不会进行构建了
  5. "build": "vue-tsc --noEmit && vite build",
  6. //vite preview预览打包的结果
  7. "preview": "vite preview"
  8. },
  9. //依赖的相关信息
  10. "dependencies": {
  11. "vue": "^3.2.37"
  12. },
  13. "devDependencies": {
  14. "@vitejs/plugin-vue": "^3.0.0",
  15. "typescript": "^4.6.4",
  16. "vite": "^3.0.0",
  17. "vue-tsc": "^0.38.4"
  18. }
  • tsconfig.json 一些基本的ts配置给我们弄好了
  • vite.config.ts 这是vite的配置文件。所有vite的配置,我们也可以自定义配置

我们额外在src里添加:

  1. api,用来存放接口封装的这些模块
  2. style
  3. untils 放置与工具相关的模块
  4. plugins 插件库
  5. views 存储路由页面,非路由页面放components里
  6. router 路由器
  7. store 容器相关的模块
  8. layout 公共布局组件
  9. composable项目中封装的组合式API的模块

上传git备份

  1. //初始化 git init
  2. C:\code\shop-admin>git init
  3. Initialized empty Git repository in C:/code/shop-admin/.git/
  4. //看看状态
  5. git status
  6. //以下不受管理,我们在gitignore里设置过的
  7. On branch master
  8. No commits yet
  9. Untracked files:
  10. (use "git add <file>..." to include in what will be committed)
  11. .gitignore
  12. .vscode/
  13. README.md
  14. index.html
  15. package-lock.json
  16. package.json
  17. public/
  18. src/
  19. tsconfig.json
  20. tsconfig.node.json
  21. vite.config.ts
  22. nothing added to commit but untracked files present (use "git add" to track)
  23. //把变动的全放进去
  24. git add .
  25. //再次查看一下状态
  26. git status
  27. //确认没问题后,就能确认提交了。初次提交,因此就写为init
  28. git commit -m "init"
  29. //由于还没登陆,因此
  30. Author identity unknown
  31. *** Please tell me who you are.
  32. Run
  33. git config --global user.email "you@example.com"
  34. git config --global user.name "Your Name"
  35. to set your account's default identity.
  36. Omit --global to set the identity only in this repository.
  37. fatal: unable to auto-detect email address (got '77518@Lin.(none)')
  38. //于是我登陆信息
  39. git config --global user.email "you@example.com"
  40. git config --global user.name "Your Name"
  41. //登陆后,再次提交
  42. git commit -m "init"
  43. //本地备份完成后,备份到github里
  44. //把本地推到线上,复制我们项目的command line
  45. //起名叫origin
  46. git remote add origin https://github.com/jianqi11/Lin-admin.git
  47. //检查下远程仓库
  48. git remote -v
  49. origin https://github.com/jianqi11/Lin-admin.git (fetch)
  50. origin https://github.com/jianqi11/Lin-admin.git (push)
  51. //没啥问题,远程仓库叫origin,那么我们也比较方便,直接操作origin
  52. //注意push之前确保有最新的记录才能Push
  53. //然后把本地的master分支push到origin里。第一次提交所以用-u,下次就不用了,默认后面的
  54. git push -u origin master

代码规范和ESLint

基础配置

由于vite没有提供ESLint, 因此我们得手动配置一下ESLint.

问题不大,开始基础配置

  1. 安装ESLint到项目中
  1. npm install eslint --save-dev
  1. 初始化ESLint初始配置
  1. npx eslint --init
  2. How would you like to use ESLint? ...
  3. To check syntax only //仅检查语法
  4. > To check syntax and find problems //仅检查语法并发现问题
  5. To check syntax, find problems, and enforce code style
  6. //检查语法 发现问题 强调代码规范
  7. //选择第三个
  8. //下一步 让我们选择模块
  9. ? What type of modules does your project use? ...
  10. > JavaScript modules (import/export) //ES6的规范 选这个
  11. CommonJS (require/exports)
  12. None of these
  13. //下一步选择 React/Vue.js
  14. //下一步
  15. ? Does your project use TypeScript? » No / Yes
  16. //果断yes
  17. ? Where does your code run? ... (Press <space> to select, <a> to toggle all, <i> to invert selection)
  18. Browser
  19. Node
  20. //执行环境,选浏览器
  21. ? How would you like to define a style for your project? ...
  22. > Use a popular style guide
  23. Answer questions about your style
  24. //选择代码规范,第一个是流行的规范,选第一个就好了
  25. ? Which style guide do you want to follow? ...
  26. > Airbnb: https://github.com/airbnb/javascript
  27. Standard: https://github.com/standard/standard
  28. Google: https://github.com/google/eslint-config-google
  29. XO: https://github.com/xojs/eslint-config-xo
  30. //个人喜欢standard
  31. //下一步是选择语言,选JS
  32. //下一步选择是否安装 YES
  33. ? Would you like to install them now? » No / Yes
  34. //下一步选择NPM
  35. added 88 packages in 26s
  36. 4 packages are looking for funding
  37. run `npm fund` for details
  38. Successfully created .eslintrc.cjs file in C:\code\shop-admin
  39. //证明安装完毕

配置完成后,在项目的根目录下多了一个.eslintrc.cjs文件

  1. ESLint 配置文件,(小小的意见:如果之后ESLint可以选择这个规则就更好了,选择vue2还是vue3)
  1. //.eslintrc.cjs
  2. module.exports = {
  3. env: {
  4. browser: true,
  5. es2021: true
  6. },
  7. extends: [
  8. // 'plugin:vue/vue3-essential',修改这儿, 我们改成强烈推荐的版本
  9. //vue3主要有三个版本,vue3-essential, vue3-strongly-recommended等
  10. //使用 Vue 3 规则
  11. // https://eslint.vuejs.org/user-guide/#bundle-configurations
  12. 'plugin:vue/vue3-strongly-recommended',
  13. 'standard'
  14. ],
  15. parserOptions: {
  16. ecmaVersion: 'latest',
  17. parser: '@typescript-eslint/parser',
  18. sourceType: 'module'
  19. },
  20. plugins: [
  21. 'vue',
  22. '@typescript-eslint'
  23. ],
  24. rules: {
  25. }
  26. }
  1. npm scripts中添加验证脚本
  1. //package.json
  2. "scripts": {
  3. ...
  4. //这样以来,我们在cmd里可以直接执行npm lint,就不需要执行这么多代码了
  5. //作用是检查src目录下所有js,jsx,vue.ts,tsx文件模块 --fix代表不符合要求的自动格式化 处理不了的不会动
  6. "lint":"eslint ./src/**/*.{js,jsx,vue,ts,tsx} --fix"
  7. }

验证:

我们把src/main.ts里

  1. import { createApp } from 'vue'
  2. import './style.css'
  3. import App from './App.vue';
  4. //最后加一个分号,一个小错误
  1. npm run lint

结果自动给我们删除了;

vue-eslint-plugin

介绍了一些规则,以及如何安装,但是我们已经安装好了!

但是也有进行流程的介绍和使用。

编译器集成

如何看到不符合规范的错误提示?

如何按照项目中的ESLint规则要求进行格式化

  • 禁用Vetur
  • 安装eslint插件
    • 安装后,这个插件马上就会自动查找项目中的eslint配置规范并给出提示!
    • 如何格式化?
      • ESLint是提供了格式化工具的,但是需要我们手动去配置才有。点击vscode 首选项=> 设置 => 扩展 => ESLint => ESLint>format: Enable打勾✔
      • 如何使用?右键,选择格式化文档的方式,选择ESLint即可。
  • 安装volar插件

配置git commit hook:

保持代码规范是尤其重要的,保持洁癖性!

但是我们每次上传代码前,都得执行一次link,会不会有些麻烦,又或者有时候忘了怎么办?

于是我查找了下,可以进行配置lint-staged!自动执行。

安装:

  1. npx mrm@2 lint-staged

安装完之后,package.json里多了一个husky,lint-staged

husky的作用是拦截,是git脚本的钩子工具,挂载在git之前,在下一次git push或者什么之前。

lint-staged拿到暂存区的代码,比如git add .的文件

并且安装后,自动在script里添加了

  1. "prepare":"husky install"

确保其他人拿到仓库的代码时,若其他人的电脑上没有husky,岂不是尴尬了,就能自动安装了,确实很人性化哦!

配置:

  1. //将以下的代码全换掉
  2. "lint-staged": {
  3. // "*.js": "eslint --cache --fix"
  4. *.{js,jsx,vue,ts,tsx}":[
  5. "npm run lint",
  6. //若lint修复完后,再加到暂存区里,于是g
  7. "git add"
  8. ]
  9. }

安装和配置eslint后,及时验证下。

我故意在main.ts里改了代码,多加了一个后期不需要使用的变量a,试试能不能找出来

  1. git add .
  2. //备份完毕
  3. //验证
  4. git commit -m "config eslint"
  5. //只要有错误,就会报出来。
  6. C:\code\shop-admin\src\main.ts
  7. 4:7 error 'a' is assigned a value but never used no-unused-vars
  8. 1 problem (1 error, 0 warnings)

验证通过!没问题。配置的git commit hook是ok的!

在开发和构建中进行代码规范校验

主要问题:ESLint怎么在开发和构建中进行代码校验。

以上的配置只是存在于git commit时,有个hook钩子来帮我们进行eslint校验。

实际是需要把eslint集成到vite的整个构建环境中

官方并没有提供插件,在vite官网的官方插件里没能找到相关的插件。于是我们需要自己去配。

不过我找到了一个比较不错的插件

安装

  1. npm install eslint vite-plugin-eslint --save-dev

安装完毕后,需要手动配置一下。

在vite.config.ts

  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import eslintPlugin from 'vite-plugin-eslint'
  4. // https://vitejs.dev/config/
  5. export default defineConfig({
  6. plugins: [
  7. vue(),
  8. eslintPlugin({
  9. // 配置选项
  10. })
  11. ]
  12. })

插件配置完成后:

当我们开发时代码不规范,都会及时报错

比如我们特意把main.ts里,引入vue后面加个;
image.png

cmd会直接报错,浏览器里也会
image.png

插件原理:

创建eslint插件API,然后在插件里调用eslint进行代码格式验证。

可以修改的配置项:cacha, fix, include…

主要修改的是cache,把缓存关闭,以至于我们可以及时地更新。比如当我们修改错误后,页面的错误可以马上消失。如果不关闭,可能页面验证还没通过,因为是基于缓存的代码。

存在的好处是提高验证的效率。

fix默认false,指的是自动给开发者修复问题,不建议

include指的是验证哪些形式的代码,比如默认包括/.(jsx?|tsx?|vue|svelte)$/

exclude哪些不验证,默认node_modules不验证

formatter格式化

throwOnWarning默认true.抛出警告

throwOnErrot默认true. 抛出异常

但是,只会验证当前的文件,比如我们打开了main.ts,只会验证main.ts,不会验证其他的文件

尝试上线发布

  1. npm run build

image.png

没有任何错误,成功构建。

angular git commit规范

参考:阮一峰老师的博客

Git 每次提交代码,都要写 Commit message(提交说明),否则就不允许提交。

git commit就是提交日志,可以自己随意编写,但是,一般来说,commit message 应该清晰明了,说明本次提交的目的。

  1. git commit -m "xxx"

如果一行不够你写,那么

  1. git commit

就会直接跳出文本编辑器任你写

例子:

某一天发现了一个bug,修正后,要提交。可以在commit里写个说明,加上bug的解释

但是没有按照格式编写,开发者本身若查看,也不好查找相关的说明,因此需要定义格式,方便查看。

比如:修复了一个bug

  1. fix($animateCss):"only design can...."

commit的好处

参照阮一峰老师提出的:

  1. 提供更多的历史信息,方便快速浏览。

比如我们可以通过日志来查看上次发布的变动,而同时也会显示每次的时间。

  1. $ git log <last tag> HEAD --pretty=format:%s
  1. 过滤某些commit(比如文档改动),便于快速查找信息。

比如,写一段命令,仅仅只显示本次发布新增的功能。

  1. $ git log <last release> HEAD --grep feature
  1. 可以直接从commit生成Change log.

Change log是在开发者发布新版本时,用来说明与上一个版本差异的文档!

如果每次都写了commit,那么就不用手动写change log,直接可以生成。但是前提是得按照格式来,不能乱写。

image.png

commit message格式

每次提交commit message都包含三部分:Header、Body、Footer

  1. //本次提交的类型,影响的作用范围:提交的主题是什么
  2. <type>(<scope>):<subject>
  3. //空一行
  4. <body>
  5. //空一行
  6. <footer>

其中Header是必需的

2.1 Header

Header部分只有一行,包括了type(必需)、scope(可选)、subject(必需)

type用于说明commit类别,只允许使用下面7个标识

  • feat: 新功能(feature)
  • fix:修补bug
  • docs: 文档(documentation)
  • style: 格式(不影响代码运行的变动)
  • refactor: 重构(即不是新增功能,也不是修改bug的代码变动)
  • test: 新增加了测试或者改了测试
  • chore: 构建过程或辅助工具的变动

其中如果type是feat和fix,则该commit将肯定要出现在change log里!其他就由自己决定要不要放进change log里,建议不要。

scope则用于说明commit影响范围,比如数据层、控制层、视图层,视项目不同而不同。

subject是commit目的 的 简短描述,不超过50个字符。

  • 以动词开头,使用第一人称现在时,比如change,而不是changed/changes
  • 第一个字母小写
  • 结尾不加句号

2.2 Body

不是必需的。若单行就不需要Body了,若多行每次需要空一行。

  1. More detailed explanatory text, if necessary. Wrap it to
  2. about 72 characters or so.
  3. Further paragraphs come after blank lines.
  4. - Bullet points are okay, too
  5. - Use a hanging indent

注意:格式还是使用第一人称现在时。内容应该写清楚代码为啥变动以及变动与之前的对比。

2.3 Footer

Footer也不是必须的,只用于两种情况

  1. 版本变动

如果更新的代码与上个版本不兼容,则Footer部分以BREAKING CHANGE开头,后面是对变动的描述、以及变动理由和迁移方法。

  1. BREAKING CHANGE: isolate scope bindings definition has changed.
  2. To migrate the code follow the example below:
  3. Before:
  4. scope: {
  5. myAttr: 'attribute',
  6. }
  7. After:
  8. scope: {
  9. myAttr: '@',
  10. }
  11. The removed `inject` wasn't generaly useful for directives so there should be no code using it.
  1. 关闭Issue

github开源项目,其他开发者可以给你提bug,或者想让你添加新的功能,就是一个issue。而我们可能要对issue解决问题,解决完了后,就需要关闭issue

而关闭issue就可以手动关闭,也可以在footer里关闭。

  1. Closes #234, #245, #992

2.4 撤销Commit - Revert

如果当前的commit用于撤销以前的commit,则必须用revert:开头,后面跟着被撤销Commit的Header

  1. revert: feat(pencil): add 'graphiteWidth' option
  2. This revert commit 667ecc1654a317a13331b17617d973392f415f02.

Body部分的格式是固定的,必须写成This reverts commit &lt;hash>., 其中的hash是被撤销Commit的SHA标识符。

如果当前 commit 与被撤销的 commit,在同一个发布(release)里面,那么它们都不会出现在 Change log 里面。如果两者在不同的发布,那么当前 commit,会出现在 Change log 的Reverts小标题下面。

验证commit

需要配置一个辅助工具来验证commit是否符合规范,commitlint

安装和配置:我们是windows,选择下面那个

  1. # Install commitlint cli and conventional config
  2. npm install --save-dev @commitlint/{config-conventional,cli}
  3. # For Windows:
  4. npm install --save-dev @commitlint/config-conventional @commitlint/cli
  5. # Configure commitlint to use conventional config
  6. echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

配置完之后,继续配置commit-msg的钩子函数hooks:

Husky在上面eslint已经装过了,所以不需要安装了,并且也激活了,因此也不需要Activate hooks

  1. # Install Husky v6
  2. npm install husky --save-dev
  3. # or
  4. yarn add husky --dev
  5. # Activate hooks
  6. npx husky install
  7. # or
  8. yarn husky install

配置

  1. npx husky add .husky/commit-msg "npx --no-install commitlint -e $HUSKY_GIT_PARAMS"

按官网,完成了配置,那么进行验证

  1. git commit -m "chore: setCommitlint"
  2. //结果报错了

image.png

我在谷歌查找了答案,最后解决方案

将commitlint.config.js改成了cjs即可。

再次验证时,却显示我们没定义规则,好吧,我们在commitlint.config,cjs里直接手写规则

image.png

  1. module.exports = {
  2. extends: [
  3. '@commitlint/config-conventional'
  4. ],
  5. rules: {
  6. 'type-enum': [2, 'always', [
  7. 'build', 'ci', 'perf', 'feat', 'fix', 'refactor', 'docs', 'chore', 'style', 'revert', 'test'
  8. ]],
  9. 'type-case': [0],
  10. 'type-empty': [0],
  11. 'scope-empty': [0],
  12. 'scope-case': [0],
  13. 'subject-full-stop': [0],
  14. 'subject-case': [0, 'never'],
  15. 'header-max-length': [0, 'always', 72]
  16. }
  17. }

再次git add .

再次验证

终于好了
image.png