# tov-template **Repository Path**: ggbhack/tov-template ## Basic Information - **Project Name**: tov-template - **Description**: vite + vue3 + ts 开箱即用现代开发模板 - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 127 - **Created**: 2022-03-14 - **Last Updated**: 2023-01-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # tov-template 一个 ` vite + vue3 + ts ` 开箱即用现代开发模板

## 特点 🐳 1. Vite 的 2. Vue3 的 3. 文件路由 4. 布局系统 5. Mock 支持 6. Api 自动引入 7. 组件自动引入 8. 图标自动引入 9. VueUse 支持 10. TypeScript 的 11. Windi CSS 的 12. 暗黑模式支持 13. SWR 请求支持 14. pinia 状态管理 15. pnpm 包管理器 16. 跳转进度条支持 17. Inspect 调试支持 18. 插件自动加载支持 19. Vitest 单元测试支持 20. 支持 Markdown 渲染 21. 路径别名 `~` 支持 22. 命令行自动创建与删除 23. i18n 国际化支持 24. 漂亮的 404页 支持 25. tsx 支持 26. gzip 资源压缩支持 27. 环境变量配置支持

## 克隆模板 🦕 1. Github ```shell git clone git@github.com:dishait/tov-template.git ``` 2. Gitee ```shell git clone git@gitee.com:dishait/tov-template.git ``` 3. degit ```shell npx degit https://github.com/dishait/tov-template#main ``` 4. 新的 `Github` 仓库 👉 [Use this template](https://github.com/dishait/tov-template/generate)


## `node` 版本推荐 🐎 因为该模板完全面向现代,所以推荐大家使用 `node` 当前的长期维护版本 `v16`, 大于 `v16.13.1` 即可。


## 使用 🐂 > 该模板不限定大家使用某一特定的包管理器,npm,yarn 和 pnpm 都行。同时注意 npm 的版本应该尽量的新。 强烈推荐大家使用更快更合理的 `pnpm` 包管理器 👉 [安装教程](https://pnpm.io/zh/installation) 1. 安装依赖 ```shell pnpm install # 或者 npm install # 或者 yarn ``` 2. 开发 ```shell pnpm dev # 或者 npm run dev # 或者 yarn dev # 开启 host pnpm dev:host # 或者 npm run dev:host # 或者 yarn dev:host # 自动打开浏览器 pnpm dev:open # 或者 npm run dev:open # 或者 yarn dev:open ``` 3. 预览 ```shell pnpm preview # 或者 npm run preview # 或者 yarn preview # 开启 host pnpm preview:host # 或者 npm run preview:host # 或者 yarn preview:host # 自动打开浏览器 pnpm preview:open # 或者 npm run preview:open # 或者 yarn preview:open ``` 4. 打包 ```shell pnpm build # 或者 npm run build # 或者 yarn build ``` 5. 单元测试 ```shell pnpm test # 或者 npm run test # 或者 yarn test ``` 6. 单元测试报告生成 ```shell pnpm coverage # 或者 npm run coverage # 或者 yarn coverage ``` 7. 样式报告预览 ```shell pnpm analysis # 或者 npm run analysis # 或者 yarn analysis ``` 8. 样式报告打包 ```shell pnpm analysis:build # 或者 npm run analysis:build # 或者 yarn analysis:build ``` 9. 类型检查 ```shell pnpm typecheck # 或者 npm run typecheck # 或者 yarn typecheck ``` 10. 自动创建 ```shell pnpm auto:create # 或者 npm run auto:create # 或者 yarn auto:create ``` 11. 自动移除 ```shell pnpm auto:remove # 或者 npm run auto:remove # 或者 yarn auto:remove ``` 12. 依赖更新 ```shell # 安全版本更新 pnpm deps:fresh # 或者 npm run deps:fresh # 或者 yarn deps:fresh # 主版本更新,可能是破坏性更新,谨慎使用,做好测试 pnpm deps:fresh:major # 或者 npm run deps:fresh:major # 或者 yarn deps:fresh:major # 次版本更新,可能是破坏性更新,谨慎使用,做好测试 pnpm deps:fresh:minor # 或者 npm run deps:fresh:minor # 或者 yarn deps:fresh:minor # 补丁版本更新 pnpm deps:fresh:patch # 或者 npm run deps:fresh:patch # 或者 yarn deps:fresh:patch ``` ```shell # 以上命令仅对包信息 package.json 进行写入,需要重新执行包安装命令 pnpm i # 或者 npm i # 或者 yarn ```

## 动机 🐗 为什么要做这个 **模板** 呢? 1. 为下次开发节省浪费在配置上的时间 2. 结合主流插件整合现代开发架构,提高开发效率

## 使用场景 🐻 什么时候你应该用? 1. 不想浪费时间在项目配置上 2. 希望尝试用更现代的方式开发 `web` 应用,提高开发效率

## 启发 🐃 该模板受 **[vitesse](https://github.com/antfu/vitesse)** 启发,如果你有 SSG 的场景,那么推荐你始终使用 **[vitesse](https://github.com/antfu/vitesse)**。

## 组织 🦔 欢迎关注 **帝莎编程** - [官网](http://dishaxy.dishait.cn/) - [Gitee](https://gitee.com/dishait) - [Github](https://github.com/dishait) - [网易云课堂](https://study.163.com/provider/480000001892585/index.htm?share=2&shareId=480000001892585)

## 详情 🐳 ### [1. Vite 的](https://cn.vitejs.dev/) 该模板采用 **[vite](https://cn.vitejs.dev/)** 作为构建工具,你可以在根目录下的 `vite.config.ts` 对项目的构建进行配置。 对于众多主流插件的引入和繁杂配置已经整合到根目录下的预设 `presets` 中,大多数情况下你是不需要重新对它们进行配置的。
### [2. Vue3 的](https://v3.cn.vuejs.org/)
### [3. 文件路由](https://github.com/hannoeru/vite-plugin-pages) 目录结构即路由。 eg: - `src/pages/index.vue` => `/` - `src/pages/about.vue` => `/about` - `src/pages/users/index.vue` => `/users` - `src/pages/users/profile.vue` => `/users/profile` - `src/pages/users/[id].vue` => `/users/:id` - `src/pages/[user]/settings.vue` => `/:user/settings` - `src/pages/[...notFound].vue` => 404 路由 具体可见 👉 [vite-plugin-pages](https://github.com/hannoeru/vite-plugin-pages#file-system-routing)
### [4. 布局系统](https://github.com/JohnCampionJr/vite-plugin-vue-layouts) #### 默认布局 `src/layouts/default.vue` 将作为默认布局。 ```html ``` 此时 `src/pages/index.vue` ```html ``` 路由到 `/`时,页面将渲染 ```html 我是默认布局 我是首页 ``` 此时 `src/pages/about.vue` ```html ``` 路由到 `/about` 时,页面将渲染 ```html 我是默认布局 我是关于页 ```
#### 非默认布局 随便创建一个 `src/layouts/custom.vue` ```html ``` 此时 `src/pages/index.vue` 内 ```html meta: layout: custom ``` 此时路由到 `/`, 页面将渲染 ```html 我是非默认布局custom 我是首页 ``` 具体可见 👉 [vite-plugin-vue-layouts](https://github.com/JohnCampionJr/vite-plugin-vue-layouts#overview)
### [5. Mock 支持](https://github.com/vbenjs/vite-plugin-mock) 在根目录下的 `mock` 目录下,可以在模块中导出默认的 `api` 资源。 例如 `mock/test.ts` 内导出 ```ts import { MockMethod } from 'vite-plugin-mock' export default [ { url: '/api/get', method: 'get', response: () => { return { code: 0, data: { name: 'vben' } } } } ] as MockMethod[] ``` 在 `src` 中就可以进行模拟请求。 ```html ``` 这里用到 [vue-request](https://cn.attojs.org/) 去做请求,不过因为该 `mock` 拦截的是一整个接口,所以换成 `axios` 等请求库也是可以的。 更多 `mock` 设置可见 👉 [vite-plugin-mock](https://github.com/vbenjs/vite-plugin-mock)
### [6. Api 自动引入](https://github.com/antfu/unplugin-auto-import) 原本 `vue` 的 `api` 需要自行 `import`。 ```ts import { ref, computed } from 'vue' const count = ref(0) const doubled = computed(() => count.value * 2) ``` 现在可以直接使用。 ```ts const count = ref(0) const doubled = computed(() => count.value * 2) ``` 而且上边的 `api` 是按需自动引入的。 目前模板支持自动引入 `api` 的库列表包括: 1. vue 2. pinia 3. vueuse 4. vue-i18n 5. vue-router 当然还有项目中的自动引入,只需要满足以下规范即可。 1. `src/composables` 下凡是 `use` 开头的模块,同时里边有 `default` 导出,该导出就可以被按需自动引入。 例如有个 `src/composables/foo.ts` ```ts // default 导出 export default 1000 ``` 此时就不再需要 `import`了 ```html ``` 2. `src/stores` 下凡是 `Store` 结尾的模块,同时里边有 `default` 导出,该导出就可以按需自动引入。 例如有个 `src/stores/counterStore.ts` ```ts // default 导出 export default defineStore('counter', { state() { return { counter: 1 } }, actions: { inc() { this.counter++ } } }) ``` 此时就不再需要 `import`了 ```html ``` 具体可见 👉 [unplugin-auto-import](https://github.com/antfu/unplugin-auto-import) 与 [vite-auto-import-resolvers](https://github.com/dishait/vite-auto-import-resolvers)
### [7. 组件自动引入](https://github.com/antfu/unplugin-auto-import) 原来需要 `import` ```html ``` 现在只要在 `src/components` 下定义的组件都将会按需引入,即 `import` 是不需要的。 ```html ``` 同时流行组件库自动引入也是支持的,例如 `Naive ui`。 只需安装依赖。 ```shell pnpm add naive-ui # 或者 npm i naive-ui # 或者 yarn add naive-ui ``` 即可在模板中使用。 ```html ``` 目前支持的组件库有: - [naive ui](https://www.naiveui.com/zh-CN/os-theme) - [arco.design](https://arco.design/) - [element-plus](https://element-plus.gitee.io/zh-CN/) - [ant design of vue](https://next.antdv.com/docs/vue/introduce-cn/) - [@vueuse/components](https://vueuse.org/guide/components.html) 具体可见 👉 [unplugin-vue-components](https://github.com/antfu/unplugin-vue-components)
### [8. 图标自动引入](https://github.com/antfu/unplugin-auto-import) 可前往 👉 [icones](https://icones.netlify.app/),随意选择点击进入其中一个图标库 ![icons-first-step](public/docs/icons-first-step.png) 再点击选择其中一个喜欢的图标 ![icons-second-step](public/docs/icons-second-step.png) 复制其名称 ![icons-second-step](public/docs/icons-third-step.png) 在模板中即可直接用组件的形式直接使用,注意加上前缀 `i-`。 ```html ``` 保存后等待自动下载该图标库后,就可以在页面中看到对应图标。 注意自动下载图标需要 `pnpm` 包管理器支持,如果你是其他包管理器需要手动安装图标集 👇👇👇 图标集名称为 `@iconify/` 加 `:` 前缀,例如上边的图标是 `mdi:account-box-multiple`,图标集名称就为 `@iconify/mdi`。 ```shell npm i @iconify/mdi -D # 或者 yarn add @iconify/mdi -D ``` 同时推荐你使用 `vscode` 插件 [Iconify IntelliSense](https://marketplace.visualstudio.com/items?itemName=antfu.iconify)。 该插件会在模板中显示图标的预览。就像这样 👇 ```html ``` 将在 `vscode` 中得到图标预览 ![icons-preview](public/docs/icons-preview.png) 具体可见 👉 [unplugin-icons](https://github.com/antfu/unplugin-icons)
### [9. VueUse 支持](https://vueuse.org/) `VueUse` 是一个超级强的 `hooks` 库,例如你要获取鼠标位置,只需要这样 👇 ```html ``` 具体可见 👉 [VueUse](https://vueuse.org/)
### [10. TypeScript 的](https://www.tslang.cn/) 不需要重新配置,直接用 `ts` 书写就行了。
### [11. Windi CSS 的](https://cn.windicss.org/) `Windi CSS` 是一个开发中速度更快的 `原子css` 库。 直接在模板中用就行了,不需要配置。 ```html