vue3+vite+element-plus+husky+commitzen搭建项目

2023-10-30

1.1 编辑器统一编码规范

# http://editorconfig.org

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

1.2 prettier格式化

  1. 安装,后者是为了解决雨eslint得冲突
npm i prettier eslint-config-prettier -D
  1. 配置文件
// .prettierrc
{
  "useTabs": false,
  "tabWidth": 2,
  "printWidth": 80,
  "singleQuote": true,
  "trailingComma": "none",
  "semi": false
}
  1. 忽略文件
// .prettierignore
/dist/*
.local
.output.js
/node_modules/**

**/*.svg
**/*.sh

/public/*
  1. 配置脚本命令
// package.json
"scripts": {
	// ...
  "prettier": "prettier --write ."
},

1.3 eslint

  1. 安装
npm i -D  eslint-plugin-prettier eslint eslint-plugin-vue@latest @typescript-eslint/parser @typescript-eslint/eslint-plugin
  1. 配置文件
// .eslintrc.js
module.exports = {
  root: true,
  env: {
    browser: true,
    node: true,
    es2021: true,
  },
  parser: 'vue-eslint-parser',
  extends: [
    'eslint:recommended',
    'plugin:vue/vue3-recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:prettier/recommended',
    // eslint-config-prettier 的缩写
    'prettier',
  ],
  parserOptions: {
    ecmaVersion: 12,
    parser: '@typescript-eslint/parser',
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  // eslint-plugin-vue @typescript-eslint/eslint-plugin eslint-plugin-prettier的缩写
  plugins: ['vue', '@typescript-eslint', 'prettier'],
  rules: {
    '@typescript-eslint/ban-ts-ignore': 'off',
    '@typescript-eslint/no-unused-vars': 'off',
    '@typescript-eslint/explicit-function-return-type': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/no-var-requires': 'off',
    '@typescript-eslint/no-empty-function': 'off',
    '@typescript-eslint/no-use-before-define': 'off',
    '@typescript-eslint/ban-ts-comment': 'off',
    '@typescript-eslint/ban-types': 'off',
    '@typescript-eslint/no-non-null-assertion': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    'no-var': 'error',
    'prettier/prettier': 'error',
    // 禁止出现console
    'no-console': 'warn',
    // 禁用debugger
    'no-debugger': 'warn',
    // 禁止出现重复的 case 标签
    'no-duplicate-case': 'warn',
    // 禁止出现空语句块
    'no-empty': 'warn',
    // 禁止不必要的括号
    'no-extra-parens': 'off',
    // 禁止对 function 声明重新赋值
    'no-func-assign': 'warn',
    // 禁止在 return、throw、continue 和 break 语句之后出现不可达代码
    'no-unreachable': 'warn',
    // 强制所有控制语句使用一致的括号风格
    curly: 'warn',
    // 要求 switch 语句中有 default 分支
    'default-case': 'warn',
    // 强制尽可能地使用点号
    'dot-notation': 'warn',
    // 要求使用 === 和 !==
    eqeqeq: 'warn',
    // 禁止 if 语句中 return 语句之后有 else 块
    'no-else-return': 'warn',
    // 禁止出现空函数
    'no-empty-function': 'warn',
    // 禁用不必要的嵌套块
    'no-lone-blocks': 'warn',
    // 禁止使用多个空格
    'no-multi-spaces': 'warn',
    // 禁止多次声明同一变量
    'no-redeclare': 'warn',
    // 禁止在 return 语句中使用赋值语句
    'no-return-assign': 'warn',
    // 禁用不必要的 return await
    'no-return-await': 'warn',
    // 禁止自我赋值
    'no-self-assign': 'warn',
    // 禁止自身比较
    'no-self-compare': 'warn',
    // 禁止不必要的 catch 子句
    'no-useless-catch': 'warn',
    // 禁止多余的 return 语句
    'no-useless-return': 'warn',
    // 禁止变量声明与外层作用域的变量同名
    'no-shadow': 'off',
    // 允许delete变量
    'no-delete-var': 'off',
    // 强制数组方括号中使用一致的空格
    'array-bracket-spacing': 'warn',
    // 强制在代码块中使用一致的大括号风格
    'brace-style': 'warn',
    // 强制使用骆驼拼写法命名约定
    camelcase: 'warn',
    // 强制使用一致的缩进
    indent: 'off',
    // 强制在 JSX 属性中一致地使用双引号或单引号
    // 'jsx-quotes': 'warn',
    // 强制可嵌套的块的最大深度4
    'max-depth': 'warn',
    // 强制最大行数 300
    // "max-lines": ["warn", { "max": 1200 }],
    // 强制函数最大代码行数 50
    // 'max-lines-per-function': ['warn', { max: 70 }],
    // 强制函数块最多允许的的语句数量20
    'max-statements': ['warn', 100],
    // 强制回调函数最大嵌套深度
    'max-nested-callbacks': ['warn', 3],
    // 强制函数定义中最多允许的参数数量
    'max-params': ['warn', 3],
    // 强制每一行中所允许的最大语句数量
    'max-statements-per-line': ['warn', { max: 1 }],
    // 要求方法链中每个调用都有一个换行符
    'newline-per-chained-call': ['warn', { ignoreChainWithDepth: 3 }],
    // 禁止 if 作为唯一的语句出现在 else 语句中
    'no-lonely-if': 'warn',
    // 禁止空格和 tab 的混合缩进
    'no-mixed-spaces-and-tabs': 'warn',
    // 禁止出现多行空行
    'no-multiple-empty-lines': 'warn',
    // 禁止出现;
    semi: ['warn', 'never'],
    // 强制在块之前使用一致的空格
    'space-before-blocks': 'warn',
    // 强制在 function的左括号之前使用一致的空格
    // 'space-before-function-paren': ['warn', 'never'],
    // 强制在圆括号内使用一致的空格
    'space-in-parens': 'warn',
    // 要求操作符周围有空格
    'space-infix-ops': 'warn',
    // 强制在一元操作符前后使用一致的空格
    'space-unary-ops': 'warn',
    // 强制在注释中 // 或 /* 使用一致的空格
    // "spaced-comment": "warn",
    // 强制在 switch 的冒号左右有空格
    'switch-colon-spacing': 'warn',
    // 强制箭头函数的箭头前后使用一致的空格
    'arrow-spacing': 'warn',
    'no-var': 'warn',
    'prefer-const': 'warn',
    'prefer-rest-params': 'warn',
    'no-useless-escape': 'warn',
    'no-irregular-whitespace': 'warn',
    'no-prototype-builtins': 'warn',
    'no-fallthrough': 'warn',
    'no-extra-boolean-cast': 'warn',
    'no-case-declarations': 'warn',
    'no-async-promise-executor': 'warn',
  },
  globals: {
    defineProps: 'readonly',
    defineEmits: 'readonly',
    defineExpose: 'readonly',
    withDefaults: 'readonly',
  },
}
  1. 配置脚本命令
// package.json
"script": {
	"lint": "eslint --fix --ext .js,.vue,.ts src",
}

1.4 husky提交前修复

  1. 初始化及安装
npx husky-init && npm i
  1. 配置pre-commit命令,进行eslint修复
// .husky/pre-commit.sh
npm run lint
  1. 使用lint-stage修复暂存区
// package.json
"lint-staged": {
  "src/**/*.{jsx,txs,ts,js,json,css,md,vue}": [
    "prettier --write ."
  ]
},
// .husky/pre-commit.sh
npx lint-staged

1.5 commitzen提交内容规范

  1. 安装
npm i -D commitizen 
  1. 初始化及日志集成
npx commitizen init cz-conventional-changelog --save-dev --save-exact
  1. 初始化完成
// package.json
+  "config": {
+    "commitizen": {
+      "path": "./node_modules/cz-conventional-changelog"
+   }
+ }
  1. 提交使用
npx cz
  1. 脚本命令添加
  "scripts": {
		// ...
+    "commit":"cz"
  },

1.6 手动提交限制

  1. 安装
npm i @commitlint/config-conventional @commitlint/cli -D
  1. 创建配置文件
// commitlint.config.js
module.exports = {
  extends: ['@commitlint/config-conventional']
}
  1. 使用husky生成commit-msg
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"

1.7 配置路径别名

  1. vite配置
// vite.config.ts
import path from 'path'


export default defineConfig({
	// ... code
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  }
})
  1. ts配置
// tsconfig.json 
"compilerOptions": {
		// ....
+    "baseUrl": "./",
+    "paths": {
+      "@/*": ["src/*"]
    }
  },

1.8 导入集成

这样我们就不用每次都手动引入vue相关得包了

  1. 安装
npm i unplugin-auto-import -D
  1. 使用
import AutoImport from 'unplugin-auto-import/vite' //注意后面有个/vite

export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const env = loadEnv(mode, root)
  return {
    plugins: [
      AutoImport({
        imports: ['vue', 'vue-router'],
        // 可以选择auto-import.d.ts生成的位置,使用ts建议设置为'src/auto-import.d.ts'
        dts: 'src/auto-import.d.ts'
      }),
    ]
  }
})

1.9 添加scss

vite自动帮我们配置了在之前webpack所需要得配置,我们只需要安装就可以立即使用了

npm i sass 

1.10 安装路由

  1. 安装
npm i vue-router@next
  1. 配置路由
// router/index.ts
import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

import { loadView } from './view-import'
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: loadView('login/LoginIndex')
  },
  {
    path: '/main',
    component: loadView('main/MainIndex')
  }
]

const router = createRouter({
  routes,
  history: createWebHashHistory()
})

export default router


// router/view-import.ts
const modules = import.meta.glob('../views/**/**.vue')

export const loadView = (path: string) => {
  return modules['../views/' + path + '.vue']
}
  1. main.ts中注册
// main.ts
import router from '@/router'

const app = createApp(App)

app.use(router)

1.11 安装pinia

  1. 安装
npm i pinia
  1. 注册
// main.ts
import setupPinia from './store'
setupPinia(app)


// store/index.ts
import { createPinia } from 'pinia'
import type { App } from 'vue'

const setupPinia = (app: App) => {
  app.use(createPinia())
}

export default setupPinia

1.12 安装element-plus

  1. 安装
npm i element-plus
  1. 按需自动导入
npm install -D unplugin-vue-components unplugin-auto-import unplugin-element-plus
  1. 配置
// vite.config.ts
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'

export default {
  plugins: [
    // ...
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    ElementPlus()
  ],
}

1.13 安装axios

  1. 安装
npm i axios
  1. 创建环境变量
// 在根目录下创建env文件

// .env.test
VITE_AXIOS_BASE_URL = 'I am test base'
VITE_STR = 'test'

// .env.prod
VITE_AXIOS_BASE_URL = 'I am prod base'
VITE_STR = 'prod'
  1. 启动项目脚本修改
// package.json

"script": {
	"dev": "vite --mode test",
}
  1. ts类型推导添加
// env.d.ts
interface ImportMetaEnv {
  readonly VITE_APP_TITLE: string
  readonly VITE_AXIOS_BASE_URL: string
  readonly VITE_STR:string
}

interface ImportMeta {
  readonly env: ImportMetaEnv
}
  1. 验证变量
// app.vue
<script>
  console.log(import.meta.env.VITE_STR)  // 得到 test
</script>

1.14 ts配置

// tsconfig.json
{
  "compilerOptions": {
    // 目标代码(esnext-->es6/7/8/9)
    "target": "esnext",
    // 目标代码需要使用的模块化方案
    "module": "esnext",
    // 严格模式(打开严格检查)
    "strict": true,
    // 对jsx进行怎样的处理,preserve为默认不处理
    "jsx": "preserve",
    // 辅助导入功能,开启的话会将文件抽离出来采用导入的形式,否则就是直接将代码插入对应的位置
    "importHelpers": true,
    // 按照node方式解析模块
    "moduleResolution": "node",
    // 跳过对一些库(第三方库)的类型检测(axios)
    "skipLibCheck": true,
    // 开发中 允许 es module  和commonjs module 混合使用
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    // 生成映射文件
    "sourceMap": true,
    // 文件路径解析时,基本的url
    "baseUrl": ".",
    // 指定具体要解析使用的类型
    "types": ["webpack-env"],
    // 路径解析(编译阶段)
    "paths": {
      // 配置路径解析
      "@/*": ["src/*"],
      "components/*":["src/components/*"]
    },
    // 可以指定在项目中可以使用哪些库的类型
    "lib": ["esnext", "dom", "dom.iterable", "scripthost"]
  },
  // 当前哪些ts文件需要解析
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": ["node_modules"]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vue3+vite+element-plus+husky+commitzen搭建项目 的相关文章

随机推荐

  • C语言创建顺序表并插入元素 详细注释

    顺序表是用一组地址连续的存储单元依次存储数据元素的数据结构 顺序表是线性表的一种 线性表是最常用且最简单的一种数据结构 一个线性表是 n 个数据元素的有限序列 我们使用 c 语言来创建顺序表并插入元素 IDE Code Blocks 17
  • 使用 Open3D 生成空间直线点云

    使用 Open3D 生成空间直线点云 Open3D 是一个流行的开源库 被广泛应用于 3D 数据处理和可视化领域 本文将介绍如何使用 Open3D 生成空间直线点云 从而为后续的 3D 数据分析和可视化工作提供基础数据 首先 我们需要导入
  • ROS——Navigation功能包等你来查收!

    ROS Robot Operating System 机器人操作系统 我相信大家或多或少都听过 在如今机器人领域突飞猛进的时代 ROS也得到飞速发展 下图展示了其各版本的发展历程 ROS的原型源自斯坦福大学人工智能实验室的项目 其首要设计目
  • 最短路径(给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。)...

    给定一个包含非负整数的 m x n 网格 请找出一条从左上角到右下角的路径 使得路径上的数字总和为最小 说明 每次只能向下或者向右移动一步 例 输入 1 3 1 1 5 1 4 2 1 输出 7 解释 因为路径 1 3 1 1 1 的总和最
  • a 标签 图片下载

    一般 a 标签下载文件的方式都是加download属性 但是对于图片下载来说 加了download的属性有时起作用 有时不起作用 如果真想下载远程图片在url后同加 response content type application oct
  • win10安装docker教程、常见问题和原理总结

    文章目录 前言 一 Docker是什么 Docker通常应用场景 准备开始 为什么要引入docker docker基本概念 docker工作流程 docker原理 二 win10上安装docker 三 linux安装docker 前言 wi
  • HC-05学习笔记

    大家好 新手上路 请多多指教 网上有很多大佬也做了这个HC 05的文章 我这个文章只是个人学习笔记 如果有侵犯到那位大佬请与我联系谢谢 也是希望对一次的学习做一个记录 也能够希望帮助到其他的小伙伴们 HC 05蓝牙 大家好 新手上路 请多多
  • Python中Xpath一些研究,node与*的一些区别

    Python中Xpath一些研究 node与 的一些区别理解 详细看下文 主要是一些区别 主要是注意 child node 与child 的一些区别 node 表示的是节点 表示的是元素 元素 文本 注释都属于节点 而标签属于元素 同时 标
  • Python研究生组蓝桥杯(省二)参赛感受

    为什么参加蓝桥杯 今年是读研的第一年 看着我简历上的获奖经历 优秀学生干部 优秀志愿者 优秀毕业生 大学四年 我竟然没有一次竞赛类的经历 也没有拿得出手的项目 我陷入了深深的焦虑 听说蓝桥杯的门槛相对较低 对我这种小白比较友好 于是我报名了
  • 软件配置管理(二)配置管理角色与过程

    文章目录 一 配置管理角色及职责 项目经理 PM 配置控制委员会 CCB 配置管理员 CMO 系统集成员 SIO 开发人员 DEV 二 配置管理基本流程 计划阶段 开发和维护阶段 三 软件配置管理七项基本活动 1 制定配置管理计划 2 识别
  • ubuntu2004/1804安装编译RocksDB

    Linux Ubuntu下载依赖 Upgrade your gcc to version at least 4 8 to get C 11 support Install gflags First try sudo apt get inst
  • XSS十五关通关秘籍

    文章来源 MS08067 Web零基础1期作业 本文作者 ymsli Web零基础1期学员 第一关 url输入关键字 完成第一关 第二关 文本框内输入 nclick alert 123 gt 点击搜索 文本框已被添加onclick事件 点击
  • 面试总结:测试常见面试题汇总

    文章目录 理论 测试流程 各个测试阶段 单元测试 集成测试 系统测试区别 测试用例设计 什么是好的测试用例 方法 用户登录 实例 App测试和Web测试的区别 典型测试场景 聊天功能测试用例怎么设计 怎么测试微信朋友圈 TODO 怎么测试微
  • do_mmap解读

    1 unsigned long do mmap pgoff struct file file unsigned long addr 2 unsigned long len unsigned long prot 3 unsigned long
  • Chat Gpt 4.0 API接口技术对接

    GPT Generative Pre trained Transformer 是一种基于Transformer网络架构的自然语言处理模型 能够生成自然 连贯的语言文本 GPT API接口技术是指将GPT模型应用于API接口开发 使得通过AP
  • ML-机器学习基础

    目录 偏差与方差 导致偏差和方差的原因 深度学习中的偏差与方差 生成模型与判别模型 两者之间的联系 优缺点 常见模型 先验概率与后验概率 偏差与方差 偏差与方差分别是用于衡量一个模型泛化误差的两个方面 模型的偏差 指的是模型预测的期望值与真
  • 大数据单机学习环境搭建(5)Hive建表DDL详解

    专题 大数据单机学习环境搭建和使用 1 Hive建表简单示例 1 1 Hive建表语句 1 2 表详细信息 1 3 数据展示 2 Hive建表语法详解 3 拓展1 复杂数据分割 4 拓展2 事务表 大数据单机学习环境搭建 5 Hive建表D
  • Linux中使用ctrl+z停止任务后如何恢复任务

    ctrl z的作用是停止任务 要恢复停止的任务要使用fg命令 root localhost jobs 1 已停止 top root localhost fg 1 fg 任务序号 直接用fg命令 恢复的是最新停止的一条任务
  • 1流明等于多少lux_投影仪流明科普,别再被商家骗了

    一 简介 流明 英文 Lumen 简写 lm 是光通量的国际单位 光通量 luminous flux 代表了我们人眼对不同波长的光的变化铭感度 我们一般说的投影仪流明指的是 ANSI流明 这个是国际公认的标准单位 在不同位置对投影仪进行测量
  • vue3+vite+element-plus+husky+commitzen搭建项目

    1 1 编辑器统一编码规范 http editorconfig org root true 表示所有文件适用 charset utf 8 设置文件字符集为 utf 8 indent style space 缩进风格 tab space in