vue3 使用 husky + commitlint 强制码提交规范

使用prettier + Eslint 格式化代码

在vscode下载 Prettier 和 Prettier ESLint 插件 :

在这里插入图片描述
在这里插入图片描述

在项目根目录中新建 .prettierrc 文件,该文件为 perttier 默认配置文件并写入:

{
  "semi": false,   // 末尾不加分号
  "singleQuote": true,  // 使用单引号
  "trailingComma": "none", // 多行逗号分割的语法中,最后一行不加逗号
  "tabWidth": 2  //制表符长度为2
}

设置自动保存

在这里插入图片描述
在设置中,搜索 save ,勾选 Format On Save

在这里插入图片描述
设置制表符长度长度为2

在这里插入图片描述

设置默认格式化工具:

打来项目中任意vue文件或者js文件右键,点击 使用...格式化工具在弹出的选择列表选择 配置默认格式化工具,选择 prettier
在这里插入图片描述

为了防止冲突,我们在.eslintrc.cjs 添加规则:

// “关闭”或0-关闭规则
// “警告”或1-将规则作为警告(不会影响退出代码)
// “错误”或2-将规则作为错误打开(退出代码为1   触发)
module.exports = {
  //...
  rules: {
  	//...
    // 解决eslint与prettier冲突
    indent: 0, // 强制使用一致的缩进
    'space-before-function-paren': 0 // 方法名和括号之间需要有一格空格
  }
}

注意: 这里配置好后需要重启 VScode 才能生效。

git代码提交规范

1.全局安装commitizen和在项目中安装cz-customizable


npm install -g commitizen

pnpm add cz-customizable -D

2.在根目录下新建 .cz-config.cjs 文件并写入配置 之后就可以用 git cz 来代替 git commit

module.exports = {
  // 可选类型
  types: [
    { value: 'feat', name: 'feat:     新功能' },
    { value: 'fix', name: 'fix:      修复' },
    { value: 'docs', name: 'docs:     文档变更' },
    { value: 'style', name: 'style:    代码格式(不影响代码运行的变动)' },
    {
      value: 'refactor',
      name: 'refactor: 重构(既不是增加feature,也不是修复bug)'
    },
    { value: 'perf', name: 'perf:     性能优化' },
    { value: 'test', name: 'test:     增加测试' },
    { value: 'chore', name: 'chore:    构建过程或辅助工具的变动' },
    { value: 'revert', name: 'revert:   回退' },
    { value: 'build', name: 'build:    打包' }
  ],
  // 消息步骤
  messages: {
    type: '请选择提交类型:',
    customScope: '请输入修改范围(可选):',
    subject: '请简要描述提交(必填):',
    body: '请输入详细描述(可选):',
    footer: '请输入要关闭的issue(可选):',
    confirmCommit: '确认使用以上信息提交?(y/n/e/h)'
  },
  // 跳过问题
  skipQuestions: ['body', 'footer'],
  // subject文字长度默认是72
  subjectLimit: 72
}

3.在package.json中进行新增

"config": {
    "commitizen": {
      "path": "node_modules/cz-customizable"
    },
    // 指定 .cz-config.cjs 文件路径
    "cz-customizable": {
      "config": ".cz-config.cjs"
    }
  }

运行一下git cz 试试:

image-20221221150849266

使用husky进行强制git代码提交规范

# 安装 commitlint 校验插件
pnpm add -D @commitlint/config-conventional @commitlint/cli


# 安装 husky 强制性使用规范
npm install husky -D

# 初始化 husky
npx husky install

此时我们可以看见根目录多了一个.husky文件:

image-20221221151529457

导入commitlint配置文件:

在根目录新增 commitlint.config.cjs 文件并写入:

module.exports = {
  // 继承的规则
  extends: ['@commitlint/config-conventional'],
  // 定义规则类型
  rules: {
    // type 类型定义,表示 git 提交的 type 必须在以下类型范围内
    'type-enum': [
      2,
      'always',
      [
        'feat', // 新功能 feature
        'fix', // 修复 bug
        'docs', // 文档注释
        'style', // 代码格式(不影响代码运行的变动)
        'refactor', // 重构(既不增加新功能,也不是修复bug)
        'perf', // 性能优化
        'test', // 增加测试
        'chore', // 构建过程或辅助工具的变动
        'revert', // 回退
        'build' // 打包
      ]
    ],
    // subject 大小写不做校验
    'subject-case': [0]
  }
}

5.在package.json中新增指令

"prepare": "husky install"

image-20221221151906972

6.执行命令

pnpm run prepare

7.执行命令

# 生成 husky commit时的配置文件
npx husky add .husky/commit-msg

8.把husky和commitlint进行关联, 在刚刚新增husky配置文件里写入

npx --no-install commitlint --edit

image-20221221152441568

测试一下,强制提交规范成功:

image-20221221152830641

强制提交时代码格式化

既然安装了husky,为了更好的开发体验,husky也支持在推送代码时强制代码格式化

1.我们先来执行命令创建配置文件

npx husky add .husky/pre-commit

2.同样的生成的文件中写入下面命令

npx lint-staged

8.把package.json文件的lint-staged修改为

"lint-staged": {
   "src/**/*.{js,vue}": [      //src目录下所有的js和vue文件
     "eslint --fix",           // 自动修复
     "git add"                 // 自动提交时修复
   ]
 }

至此,强制提交规范完成。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以尝试以下步骤来封装一个Vue 3和TypeScript下使用Element Plus的表单提交组件: 1. 安装必要的依赖: - Vue 3:`npm install vue@next` - TypeScript:`npm install -D typescript` - Element Plus:`npm install element-plus` 2. 创建一个新的Vue组件,并为其选择一个合适的名称,例如`FormSubmit.vue`。 3. 在`FormSubmit.vue`文件中,导入必要的模块和样式: ```vue <template> <!-- 表单内容 --> </template> <script lang="ts"> import { defineComponent } from 'vue'; import { ElButton, ElForm, ElFormItem } from 'element-plus'; export default defineComponent({ components: { ElButton, ElForm, ElFormItem, }, }); </script> <style scoped> /* Element Plus 样式 */ @import 'element-plus/packages/theme-chalk/src/index.scss'; /* 自定义样式 */ /* ... */ </style> ``` 4. 在模板中编写表单内容,并使用Element Plus的组件来构建表单: ```vue <template> <el-form ref="form" :model="formData" label-width="120px"> <el-form-item label="姓名" prop="name"> <el-input v-model="formData.name"></el-input> </el-form-item> <!-- 更多表单项 --> <el-form-item> <el-button type="primary" @click="submitForm">提交</el-button> </el-form-item> </el-form> </template> <script lang="ts"> // ... export default defineComponent({ // ... data() { return { formData: { name: '', // 更多表单字段 } }; }, methods: { submitForm() { // 表单提交逻辑 if (this.$refs.form.validate()) { // 表单验证通过,执行提交操作 // ... } } } }); </script> ``` 这样,你就可以使用封装好的表单提交组件来方便地处理表单提交了。你可以根据实际需求添加更多的表单项,并在`submitForm`方法中实现你的提交逻辑。希望这可以帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OooooYi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值