Vite+ts+springboot项目集成1

项目初始化

  1. 本项目使用vite进行构建,vite参考官网
  2. pnpm包管理:performant npm,意味"高性能的npm"。pnpm由npm/yarn衍生而来,解决了npmlyarn内部潜在的bug,极大的优化了性能,扩展了使用场景。被誉为"最先进的包管理工具”

0|1使用vite创建V3工程

是新一代前端构建工具

官网地址

image-20230921011659815

pnpm安装指令

npm i -g pnpm

项目初始化

cmd进入命令行构建vite项目

image-20230921012142204

pnpm create vite

image-20230921012505315

进入项目根目录,也就是我们的waves-ts-runtine根目录下

image-20230921012544865

pnpm i

image-20230921012709766

运行demo

image-20230921012750599

访问5173端口

image-20230921012816276

0|2项目配置eslint

image-20230921080218682

设置项目运行时,浏览器自动打开

image-20230921080516802

安装eslint

pnpm i eslint -D

image-20230921080810821

生成配置文件:eslint --lint

目的:告诉eslint,你应该如何工作

npx eslint --init

image-20230921080928088

image-20230921081004714

image-20230921081017730

image-20230921081044594

image-20230921081100640

image-20230921081125611

image-20230921081152018

image-20230921081424542

项目当中出现了这个文件夹image-20230921081517126

eslintrc.cjs参数解析

  1. env:工作环境:浏览器,校验的是es语法
  2. extends:规则的继承
  3. ......

1|0Vue3环境代码校验插件

image-20230921082111349

安装代码

pnpm install -D eslint-plugin-import eslint-plugin-vue eslint-plugin-node eslint-plugin-prettier eslint-config-prettier eslint-plugin-node @babel/eslint-parser

image-20230921082625039

修改eslint配置文件

// @see https://eslint.bootcss.com/docs/rules/ module.exports = { env: { browser: true, es2021: true, node: true, jest: true, }, /* 指定如何解析语法 */ parser: 'vue-eslint-parser', /** 优先级低于 parse 的语法解析配置 */ parserOptions: { ecmaVersion: 'latest', sourceType: 'module', parser: '@typescript-eslint/parser', jsxPragma: 'React', ecmaFeatures: { jsx: true, }, }, /* 继承已有的规则 */ extends: [ 'eslint:recommended', 'plugin:vue/vue3-essential', 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', ], plugins: ['vue', '@typescript-eslint'], /* * "off" 或 0 ==> 关闭规则 * "warn" 或 1 ==> 打开的规则作为警告(不影响代码执行) * "error" 或 2 ==> 规则作为一个错误(代码不能执行,界面报错) */ rules: { // eslint(https://eslint.bootcss.com/docs/rules/) 'no-var': 'error', // 要求使用 let 或 const 而不是 var 'no-multiple-empty-lines': ['warn', { max: 1 }], // 不允许多个空行 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off', 'no-unexpected-multiline': 'error', // 禁止空余的多行 'no-useless-escape': 'off', // 禁止不必要的转义字符 // typeScript (https://typescript-eslint.io/rules) '@typescript-eslint/no-unused-vars': 'error', // 禁止定义未使用的变量 '@typescript-eslint/prefer-ts-expect-error': 'error', // 禁止使用 @ts-ignore '@typescript-eslint/no-explicit-any': 'off', // 禁止使用 any 类型 '@typescript-eslint/no-non-null-assertion': 'off', '@typescript-eslint/no-namespace': 'off', // 禁止使用自定义 TypeScript 模块和命名空间。 '@typescript-eslint/semi': 'off', // eslint-plugin-vue (https://eslint.vuejs.org/rules/) 'vue/multi-word-component-names': 'off', // 要求组件名称始终为 “-” 链接的单词 'vue/script-setup-uses-vars': 'error', // 防止<script setup>使用的变量<template>被标记为未使用 'vue/no-mutating-props': 'off', // 不允许组件 prop的改变 'vue/attribute-hyphenation': 'off', // 对模板中的自定义组件强制执行属性命名样式 }, }

生成忽略文件夹,让eslint去忽略,不扫描

创建.eslintignore文件夹

image-20230921084106958

package.json文件中新增两个运行脚本

"scripts": { "dev": "vite --open", "build": "vue-tsc && vite build", "preview": "vite preview", // 新增的两个 "lint": "eslint src", "fix": "eslint src --fix" },

0|3配置prettier

image-20230921084426713

安装

pnpm install -D eslint-plugin-prettier prettier eslint-config-prettier

image-20230921084613622

.prettierrc.json添加规则

创建该文件,添加规则

{ "singleQuote": true, "semi": false, "bracketSpacing": true, "htmlWhitespaceSensitivity": "ignore", "endOfLine": "auto", "trailingComma": "all", "tabWidth": 2 }

.prettierignore忽略文件

项目根目录创建·.prettierignore

/dist/* /html/* .local /node_modules/** **/*.svg **/*.sh /public/*

通过pnpm run lint去检测语法,如果出现不规范格式,通过pnpm run fix 修改

他帮你格式化代码意思就是

0|4配置stylelint

stylelint为css的lint工具。可格式化css代码,检查css语法错误与不合理的写法,指定css书写顺序等。

我们的项目中使用scss作为预处理器,安装以下依赖:

pnpm add sass sass-loader stylelint postcss postcss-scss postcss-html stylelint-config-prettier stylelint-config-recess-order stylelint-config-recommended-scss stylelint-config-standard stylelint-config-standard-vue stylelint-scss stylelint-order stylelint-config-standard-scss -D

.stylelintrc.cjs配置文件

官网:https://stylelint.bootcss.com/

// @see https://stylelint.bootcss.com/ module.exports = { extends: [ 'stylelint-config-standard', // 配置stylelint拓展插件 'stylelint-config-html/vue', // 配置 vue 中 template 样式格式化 'stylelint-config-standard-scss', // 配置stylelint scss插件 'stylelint-config-recommended-vue/scss', // 配置 vue 中 scss 样式格式化 'stylelint-config-recess-order', // 配置stylelint css属性书写顺序插件, 'stylelint-config-prettier', // 配置stylelint和prettier兼容 ], overrides: [ { files: ['**/*.(scss|css|vue|html)'], customSyntax: 'postcss-scss', }, { files: ['**/*.(html|vue)'], customSyntax: 'postcss-html', }, ], ignoreFiles: [ '**/*.js', '**/*.jsx', '**/*.tsx', '**/*.ts', '**/*.json', '**/*.md', '**/*.yaml', ], /** * null => 关闭该规则 * always => 必须 */ rules: { 'value-keyword-case': null, // 在 css 中使用 v-bind,不报错 'no-descending-specificity': null, // 禁止在具有较高优先级的选择器后出现被其覆盖的较低优先级的选择器 'function-url-quotes': 'always', // 要求或禁止 URL 的引号 "always(必须加上引号)"|"never(没有引号)" 'no-empty-source': null, // 关闭禁止空源码 'selector-class-pattern': null, // 关闭强制选择器类名的格式 'property-no-unknown': null, // 禁止未知的属性(true 为不允许) 'block-opening-brace-space-before': 'always', //大括号之前必须有一个空格或不能有空白符 'value-no-vendor-prefix': null, // 关闭 属性值前缀 --webkit-box 'property-no-vendor-prefix': null, // 关闭 属性前缀 -webkit-mask 'selector-pseudo-class-no-unknown': [ // 不允许未知的选择器 true, { ignorePseudoClasses: ['global', 'v-deep', 'deep'], // 忽略属性,修改element默认样式的时候能使用到 }, ], }, }

.stylelintignore设置忽略文件

/node_modules/* /dist/* /html/* /public/*

1|0补充运行脚本到package.json

"scripts": { "lint:style": "stylelint src/**/*.{css,scss,vue} --cache --fix" }

最后配置统一的prettier来格式化我们的js和css,html代码

"scripts": { "dev": "vite --open", "build": "vue-tsc && vite build", "preview": "vite preview", "lint": "eslint src", "fix": "eslint src --fix", "format": "prettier --write \"./**/*.{html,vue,ts,js,json,md}\"", "lint:eslint": "eslint src/**/*.{ts,vue} --cache --fix", "lint:style": "stylelint src/**/*.{css,scss,vue} --cache --fix" },

1|0如何使用?

在cmd当中使用 pnpm run format 即可

0|5配置husky

在上面我们已经集成好了我们代码校验工具,但是需要每次手动的去执行命令才会格式化我们的代码。如果有人没有格式化就提交了远程仓库中,那这个规范就没什么用。所以我们需要强制让开发人员按照代码规范来提交

要做到这件事情,就需要利用husky在代码提交之前触发git hook(git在客户端的钩子),然后执行pnpm run format来自动的格式化我们的代码。

安装husky

pnpm install -D husky

执行

npx husky-init

会在根目录下生成个一个.husky目录,在这个目录下面会有一个pre-commit文件,这个文件里面的命令在我们执行commit的时候就会执行,执行init时出错了,我自己在根目录下新建文件夹.husky

image-20230921090934599

手动创建文件夹后,在pre-commit当中写入

#!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" pnpm run format

image-20230921091238075

0|6配置commitlint仓库提交

对于我们的commit信息,也是有统一规范的,不能随便写,要让每个人都按照统一的标准来执行,我们可以利用commitlint来实现。

安装包

pnpm add @commitlint/config-conventional @commitlint/cli -D

添加配置文件,新建commitlint.config.cjs(注意是cjs),然后添加下面的代码:

module.exports = { extends: ['@commitlint/config-conventional'], // 校验规则 rules: { 'type-enum': [ 2, 'always', [ 'feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'chore', 'revert', 'build', ], ], 'type-case': [0], 'type-empty': [0], 'scope-empty': [0], 'scope-case': [0], 'subject-full-stop': [0, 'never'], 'subject-case': [0, 'never'], 'header-max-length': [0, 'always', 72], }, }

package.json中配置scripts命令

# 在scrips中添加下面的代码 { "scripts": { "commitlint": "commitlint --config commitlint.config.cjs -e -V" }, }

配置结束,现在当我们填写commit信息的时候,前面就需要带着下面的subject == 关键字

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

配置husky

npx husky add .husky/commit-msg

在生成的commit-msg文件中添加下面的命令

#!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" pnpm commitlint

image-20230921092117844

0|7强制使用pnpm包管理工具

团队开发项目的时候,需要统一包管理器工具,因为不同包管理器工具下载同一个依赖,可能版本不一样,

导致项目出现bug问题,因此包管理器工具需要统一管理!!!

在根目录src下创建scritps/preinstall.js文件,添加下面的内容

if (!/pnpm/.test(process.env.npm_execpath || '')) { console.warn( `\u001b[33mThis repository must using pnpm as the package manager ` + ` for scripts to work properly.\u001b[39m\n`, ) process.exit(1) }

package.json当中添加配置命令

"scripts": { "preinstall": "node ./scripts/preinstall.js" }

当我们使用npm或者yarn来安装包的时候,就会报错了。原理就是在install的时候会触发preinstall(npm提供的生命周期钩子)这个文件里面的代码。

1|0


__EOF__

本文作者muzlei
本文链接https://www.cnblogs.com/wavesbright/p/17720598.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   澜璨  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示