• (2) Java后端从0硬撸vite3+vue3+ts项目 | 规范


    源码地址

    本系列文章代码地址:https://github.com/xbmlz/venable-admin.git

    代码规范

    集成EditorConfig配置

    EditorConfig 有助于为不同 IDE 编辑器上处理同一项目的多个开发人员维护一致的编码风格。

    官网:editorconfig.org

    在项目根目录下增加 .editorconfig 文件:

    # @see: http://editorconfig.org
    
    root = true
    
    [*]
    charset = utf-8
    indent_style = space
    indent_size = 2
    end_of_line = lf
    insert_final_newline = true
    trim_trailing_whitespace = true
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    集成eslint-config

    以往我们都是采用prettierstylelinteslint组合的方式,这种方式的缺点是需要我们分别配置每个插件的各种属性,比较复杂。这里我们采用vue、vite核心成员antfu编写的一款插件@antfu/eslint-config来实现上述功能。

    插件地址:https://github.com/antfu/eslint-config

    插件特性:

    • 单引号,没有半引号
    • 自动修复格式(旨在在没有 Prettier 的情况下独立使用)
    • TypeScript、Vue、React 开箱即用
    • Lint 也适用于 json、yaml、markdown
    • 排序导入,悬空逗号以获得​​更清晰的提交差异
    • 合理的默认值,最佳实践,只有一行配置

    安装依赖

    pnpm add -D eslint @antfu/eslint-config
    
    
    • 1
    • 2

    在项目根目录创建.eslintrc

    {
      "extends": "@antfu"
    }
    
    • 1
    • 2
    • 3

    注意:.您通常不需要.eslintignore,因为它已由预设提供。

    package.json添加脚本

    {
      "scripts": {
        "lint": "eslint .",
        "lint:fix": "eslint . --fix"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    配置vscode代码自动修复

    在项目根目录创建.vscode/settings.json

    {
      "prettier.enable": false,
      "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    集成 husky 和 lint-staged

    为了简化在项目提交前执行代码规范命令以及遗忘执行,保证代码格式的统一规范,我们需要在git commit是增加git hook,在提交之前自动进行代码格式的校验与修复。

    • husky —— Git Hook 工具,可以设置在 git 各个阶段(pre-commitcommit-msgpre-push 等)触发我们的命令
    • lint-staged —— 在 git 暂存的文件上运行 linters。

    安装依赖

    pnpm add -D husky lint-staged
    
    • 1

    package.json添加脚本

    {
      "scripts": {
        "prepare": "husky install"
      },
      "lint-staged": {
        "*.{js,jsx,ts,tsx,vue}": [
          "eslint --fix"
        ]
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    初始化pre-commit

    pnpm run prepare
    npx husky add .husky/pre-commit "npx lint-staged"
    
    
    • 1
    • 2
    • 3

    配置完成,以后提交代码之前就会先使用eslint 修复js,jsx,ts,tsx,vue文件了

    提交规范

    集成commitlint

    在之前的开发中,每当提交代码时,git commit填写的都是乱七八糟的,为了后期维护和BUG处理时变得有据可查,我们可以依赖commitlint插件来辅助我们完成这项工作。

    在这里,我们使用Angular的代码提交规范

    类型描述
    build编译相关的修改,例如发布版本、对项目构建或者依赖的改动
    chore其他修改, 比如改变构建流程、或者增加依赖库、工具等
    ci持续集成修改
    docs文档修改
    feat新特性、新功能
    fix修改bug
    perf优化相关,比如提升性能、体验
    refactor代码重构
    revert回滚到上一个版本
    style代码格式修改, 注意不是 css 修改
    test测试用例修改

    官方文档

    安装插件

    pnpm add -D commitlint @commitlint/config-conventional cz-git czg
    
    • 1

    package.json添加脚本

    {
      "scripts": {
        "commit": "czg"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在项目根目录添加commitlint.config.js文件,用户自定义提交信息

    // @see: https://cz-git.qbenben.com/zh/guide
    /** @type {import('cz-git').UserConfig} */
    
    module.exports = {
      ignores: [commit => commit.includes('init')],
      extends: ['@commitlint/config-conventional'],
      rules: {
        // @see: https://commitlint.js.org/#/reference-rules
        'body-leading-blank': [2, 'always'],
        'footer-leading-blank': [1, 'always'],
        'header-max-length': [2, 'always', 108],
        'subject-empty': [2, 'never'],
        'type-empty': [2, 'never'],
        'subject-case': [0],
        'type-enum': [
          2,
          'always',
          [
            'feat',
            'fix',
            'docs',
            'style',
            'refactor',
            'perf',
            'test',
            'build',
            'ci',
            'chore',
            'revert',
            'wip',
            'workflow',
            'types',
            'release',
          ],
        ],
      },
      prompt: {
        messages: {
          // type: "Select the type of change that you're committing: ",
          // scope: 'Denote the SCOPE of this change (optional):',
          // customScope: 'Denote the SCOPE of this change:',
          // subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
          // body: 'Provide a LONGER description of the change (optional). Use ' | ' to break new line:\n',
          // breaking: 'List any BREAKING CHANGES (optional). Use ' | ' to break new line:\n',
          // footerPrefixsSelect: 'Select the ISSUES type of changeList by this change (optional):',
          // customFooterPrefixs: 'Input ISSUES prefix:',
          // footer: 'List any ISSUES by this change. E.g.: #31, #34:\n',
          // confirmCommit: 'Are you sure you want to proceed with the commit above?'
    
          // 中文版
          type: '选择你要提交的类型 :',
          scope: '选择一个提交范围(可选):',
          customScope: '请输入自定义的提交范围 :',
          subject: '填写简短精炼的变更描述 :\n',
          body: '填写更加详细的变更描述(可选)。使用 ' | ' 换行 :\n',
          breaking: '列举非兼容性重大的变更(可选)。使用 ' | ' 换行 :\n',
          footerPrefixsSelect: '选择关联issue前缀(可选):',
          customFooterPrefixs: '输入自定义issue前缀 :',
          footer: '列举关联issue (可选) 例如: #31, #I3244 :\n',
          confirmCommit: '是否提交或修改commit ?',
        },
        types: [
          {
            value: 'feat',
            name: 'feat:     🚀  A new feature',
            emoji: '🚀',
          },
          {
            value: 'fix',
            name: 'fix:      🧩  A bug fix',
            emoji: '🧩',
          },
          {
            value: 'docs',
            name: 'docs:     📚  Documentation only changes',
            emoji: '📚',
          },
          {
            value: 'style',
            name: 'style:    🎨  Changes that do not affect the meaning of the code',
            emoji: '🎨',
          },
          {
            value: 'refactor',
            name: 'refactor: ♻️   A code change that neither fixes a bug nor adds a feature',
            emoji: '♻️',
          },
          {
            value: 'perf',
            name: 'perf:     ⚡️  A code change that improves performance',
            emoji: '⚡️',
          },
          {
            value: 'test',
            name: 'test:     ✅  Adding missing tests or correcting existing tests',
            emoji: '✅',
          },
          {
            value: 'build',
            name: 'build:    📦️   Changes that affect the build system or external dependencies',
            emoji: '📦️',
          },
          {
            value: 'ci',
            name: 'ci:       🎡  Changes to our CI configuration files and scripts',
            emoji: '🎡',
          },
          {
            value: 'chore',
            name: 'chore:    🔨  Other changes that don\'t modify src or test files',
            emoji: '🔨',
          },
          {
            value: 'revert',
            name: 'revert:   ⏪️  Reverts a previous commit',
            emoji: '⏪️',
          },
          // 中文版
          // { value: '特性', name: '特性:   🚀  新增功能', emoji: '🚀' },
          // { value: '修复', name: '修复:   🧩  修复缺陷', emoji: '🧩' },
          // { value: '文档', name: '文档:   📚  文档变更', emoji: '📚' },
          // { value: '格式', name: '格式:   🎨  代码格式(不影响功能,例如空格、分号等格式修正)', emoji: '🎨' },
          // { value: '重构', name: '重构:   ♻️  代码重构(不包括 bug 修复、功能新增)', emoji: '♻️' },
          // { value: '性能', name: '性能:   ⚡️  性能优化', emoji: '⚡️' },
          // { value: '测试', name: '测试:   ✅  添加疏漏测试或已有测试改动', emoji: '✅' },
          // { value: '构建', name: '构建:   📦️  构建流程、外部依赖变更(如升级 npm 包、修改 webpack 配置等)', emoji: '📦️' },
          // { value: '集成', name: '集成:   🎡  修改 CI 配置、脚本', emoji: '🎡' },
          // { value: '回退', name: '回退:   ⏪️  回滚 commit', emoji: '⏪️' },
          // { value: '其他', name: '其他:   🔨  对构建过程或辅助工具和库的更改(不影响源文件、测试用例)', emoji: '🔨' }
        ],
        useEmoji: true,
        themeColorCode: '',
        scopes: [],
        allowCustomScopes: true,
        allowEmptyScopes: true,
        customScopesAlign: 'bottom',
        customScopesAlias: 'custom',
        emptyScopesAlias: 'empty',
        upperCaseSubject: false,
        allowBreakingChanges: ['feat', 'fix'],
        breaklineNumber: 100,
        breaklineChar: '|',
        skipQuestions: [],
        issuePrefixs: [{ value: 'closed', name: 'closed:   ISSUES has been processed' }],
        customIssuePrefixsAlign: 'top',
        emptyIssuePrefixsAlias: 'skip',
        customIssuePrefixsAlias: 'custom',
        allowCustomIssuePrefixs: true,
        allowEmptyIssuePrefixs: true,
        confirmColorize: true,
        maxHeaderLength: Infinity,
        maxSubjectLength: Infinity,
        minSubjectLength: 0,
        scopeOverrides: undefined,
        defaultBody: '',
        defaultIssues: '',
        defaultScope: '',
        defaultSubject: '',
      },
    }
    
    
    • 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
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161

    commitlint添加到husky

    npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
    
    • 1

    至此,commitlint已配置完,以后我们提交代码只需要执行pnpm run commit,插件就会自动按照我们在commitlint.config.js中配置的选项来辅助我们进行规范的代码提交。

  • 相关阅读:
    sql分析(查询截取分析做sql优化)
    u盘损坏如何恢复数据?u盘恢复数据,超好用的几个方法!
    01_大数据导论与Linux基础
    Springboot美国职业篮球联赛信息平台58sh4计算机毕业设计-课程设计-期末作业-毕设程序代做
    使用华为eNSP组网试验⑹-组建基于BGP的网络
    【初阶数据结构】——带头双向循环链表(C描述)
    如何正确部署一个测试环境
    【Spring】Spring的创建和使用手术刀剖析
    接口测试文档
    看SparkSql如何支撑企业数仓
  • 原文地址:https://blog.csdn.net/qq_35506960/article/details/126349388