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

4 篇文章 0 订阅
3 篇文章 0 订阅

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"]
}
### 回答1: Vue3 是一种用于构建用户界面的开源JavaScript 框架。它提供了许多功能和工具,使用户能够更轻松地创建可交互、响应式的应用程序。Vue3 采用了一些新的语法和技术,使其在性能和开发效率方面有了明显的改进。 Vite4 是一个基于原生ES模块解析的构建工具。它具有快速冷启动和快速热更新的能力,使开发人员能够更快速地进行开发和调试。与传统的打包工具不同,Vite4 通过在浏览器运行时进行模块解析,避免了不必要的打包和编译步骤,提供了更加流畅的开发体验。 Element Plus 是一个基于Vue3 的组件库,提供了丰富的UI组件和交互式工具,可以帮助开发人员快速构建出现代化的Web界面。Element Plus 是Element UI 的升级版,针对Vue3 进行了优化和改进,提供了更好的性能和更丰富的功能。 TypeScript 是一种强类型的JavaScript 超集,允许开发人员在JavaScript 代码中添加静态类型注解。这样可以在开发过程中提早发现错误并提供更好的代码提示。Vue3 通过与TypeScript 的集成,使开发人员能够使用类、接口、泛型等高级特性来构建更健壮的应用程序。 综上所述,Vue3、Vite4、Element Plus 和 TypeScript 的结合,可以提供一个高效、灵活和强大的开发环境。开发人员可以利用Vite4 的快速构建能力和热更新功能,结合Element Plus 提供的丰富组件,通过TypeScript 的类型检查和提示,更加轻松地构建出现代化的Vue 应用程序。 ### 回答2: Vue 3 是一款非常流行的 JavaScript 框架,用于构建现代化的用户界面。它的核心思想是组件化开发,通过将界面拆分成独立的组件,使开发者能够更好地管理和重用代码。 Vite 4 是 Vue 3 的一个新型构建工具,用于快速构建现代化的前端应用。相比传统的打包工具,Vite 4 支持开箱即用的单文件组件,可以进行更快的热重载和快速启动,从而提高开发效率。 Element Plus 是一套基于 Vue 3 的UI组件库,它提供了大量的高质量的组件,可以用于构建漂亮、响应式的用户界面。Element Plus 的组件易于使用,灵活且高度可定制,适合开发各种类型的应用。它还提供了强大的工具和样式库,使开发者能够更轻松地定制和管理应用的外观和风格。 TypeScript 是一个强类型的 JavaScript 超集,它为开发者提供了更强大的类型检查和代码提示功能。与 JavaScript 不同,TypeScript 在编码过程中能够捕获一些常见的错误,帮助开发者提高代码的质量和可维护性。TypeScript 还支持最新的 JavaScript 特性,并且可以与 Vue 3、Vite 4 和 Element Plus 等工具和库完美集成。 综合上述所述,结合 Vue 3、Vite 4、Element Plus 和 TypeScript 可以实现一种高效、可维护和易于定制的开发方式。开发者可以使用 Vite 4 快速创建 Vue 3 项目,同时借助 Element Plus 的丰富组件库来构建出漂亮的用户界面。而 TypeScript 则提供了更强大的类型检查和代码提示,避免一些潜在的bug,提高开发效率。通过组合使用这些工具和库,可以实现更快速、更可靠的前端开发体验。 ### 回答3: Vue3是一种流行的JavaScript框架,它具有响应式和组件化的特性,使开发者可以轻松构建现代化的Web应用程序。Vite4是Vue3的一个新的构建工具,旨在提供更快的启动和热重载,以及更好的开发体验。 Element Plus是一个基于Vue3的UI库,它提供了一套美观、易用和高性能的组件,用于构建优雅的用户界面。它使用了TypeScript来提供类型检查和智能提示,这使得在使用Element Plus时能更加轻松地进行开发。 TypeScript是一种由微软开发的编程语言,它是JavaScript的超集,添加了静态类型支持。它允许开发者在编写代码时进行类型检查,并提供了更好的代码补全和智能感知功能。在Vue3和Element Plus使用TypeScript可以提高代码的可读性和可维护性,并减少错误和调试时间。 综上所述,Vue3、Vite4、Element Plus和TypeScript是一组强大的工具和库,可以帮助开发者更快速、高效地构建现代化的Web应用程序。它们的结合提供了优秀的开发体验,并能够轻松满足各种UI需求。无论是构建大型企业应用还是小型个人项目,它们都是很好的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值