版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 2 години
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. <div align="center">
  2. <img width="200px" height="200px" src="https://gitee.com/dishait/tov-template/raw/main/public/logo.png" />
  3. <h1>tov-template</h1>
  4. <p>vite + vue3 + ts 开箱即用现代开发模板</p>
  5. </div>
  6. <br />
  7. <br />
  8. ## 特点 🐳
  9. 1. Vite 的
  10. 2. Vue3 的
  11. 3. 文件路由
  12. 4. 布局系统
  13. 5. Mock 支持
  14. 6. Api 自动引入
  15. 7. 组件自动引入
  16. 8. 图标自动引入
  17. 9. VueUse 支持
  18. 10. TypeScript 的
  19. 11. Windi CSS 的
  20. 12. 暗黑模式支持
  21. 13. SWR 请求支持
  22. 14. pinia 状态管理
  23. 15. pnpm 包管理器
  24. 16. 跳转进度条支持
  25. 17. Inspect 调试支持
  26. 18. 插件自动加载支持
  27. 19. Vitest 单元测试支持
  28. 20. 支持 Markdown 渲染
  29. 21. 路径别名 `~` 支持
  30. 22. 命令行自动创建与删除
  31. 23. i18n 国际化支持
  32. 24. 漂亮的 404 页 支持
  33. 25. tsx 支持
  34. 26. gzip 资源压缩支持
  35. 27. 环境变量配置支持
  36. 28. 统一的代码规范与风格支持
  37. 29. 生产环境自动移除开发日志
  38. 30. defineOptions 支持
  39. <br />
  40. <br />
  41. ## 克隆模板 🦕
  42. 1. Github
  43. ```shell
  44. git clone git@github.com:dishait/tov-template.git
  45. ```
  46. 2. Gitee
  47. ```shell
  48. git clone git@gitee.com:dishait/tov-template.git
  49. ```
  50. 3. degit
  51. ```shell
  52. npx degit https://github.com/dishait/tov-template#main
  53. ```
  54. 4. 新的 `Github` 仓库 👉 [Use this template](https://github.com/dishait/tov-template/generate)
  55. <br />
  56. <br />
  57. <br />
  58. ## `node` 版本推荐 🐎
  59. 因为该模板完全面向现代,所以推荐大家使用 `node` 当前的长期维护版本 `v16`, 大于 `v16.13.1` 即可。
  60. <br />
  61. <br />
  62. <br />
  63. ## 使用 🐂
  64. > 该模板不限定大家使用某一特定的包管理器,npm,yarn 和 pnpm 都行。同时注意 npm 的版本应该尽量的新。
  65. 强烈推荐大家使用更快更合理的 `pnpm` 包管理器 👉 [安装教程](https://pnpm.io/zh/installation)
  66. 1. 安装依赖
  67. ```shell
  68. pnpm install
  69. # 或者 npm install
  70. # 或者 yarn
  71. ```
  72. 2. 开发
  73. ```shell
  74. pnpm dev
  75. # 或者 npm run dev
  76. # 或者 yarn dev
  77. # 开启 host
  78. pnpm dev:host
  79. # 或者 npm run dev:host
  80. # 或者 yarn dev:host
  81. # 自动打开浏览器
  82. pnpm dev:open
  83. # 或者 npm run dev:open
  84. # 或者 yarn dev:open
  85. ```
  86. 3. 预览
  87. ```shell
  88. pnpm preview
  89. # 或者 npm run preview
  90. # 或者 yarn preview
  91. # 开启 host
  92. pnpm preview:host
  93. # 或者 npm run preview:host
  94. # 或者 yarn preview:host
  95. # 自动打开浏览器
  96. pnpm preview:open
  97. # 或者 npm run preview:open
  98. # 或者 yarn preview:open
  99. ```
  100. 4. 打包
  101. ```shell
  102. pnpm build
  103. # 或者 npm run build
  104. # 或者 yarn build
  105. ```
  106. 5. 单元测试
  107. ```shell
  108. pnpm test
  109. # 或者 npm run test
  110. # 或者 yarn test
  111. ```
  112. 6. 单元测试报告生成
  113. ```shell
  114. pnpm coverage
  115. # 或者 npm run coverage
  116. # 或者 yarn coverage
  117. ```
  118. 7. 样式报告预览
  119. ```shell
  120. pnpm analysis
  121. # 或者 npm run analysis
  122. # 或者 yarn analysis
  123. ```
  124. 8. 样式报告打包
  125. ```shell
  126. pnpm analysis:build
  127. # 或者 npm run analysis:build
  128. # 或者 yarn analysis:build
  129. ```
  130. 9. 类型检查
  131. ```shell
  132. pnpm typecheck
  133. # 或者 npm run typecheck
  134. # 或者 yarn typecheck
  135. ```
  136. 10. 自动创建
  137. ```shell
  138. pnpm auto:create
  139. # 或者 npm run auto:create
  140. # 或者 yarn auto:create
  141. ```
  142. 11. 自动移除
  143. ```shell
  144. pnpm auto:remove
  145. # 或者 npm run auto:remove
  146. # 或者 yarn auto:remove
  147. ```
  148. 12. 依赖更新
  149. ```shell
  150. # 安全版本更新
  151. pnpm deps:fresh
  152. # 或者 npm run deps:fresh
  153. # 或者 yarn deps:fresh
  154. # 主版本更新,可能是破坏性更新,谨慎使用,做好测试
  155. pnpm deps:fresh:major
  156. # 或者 npm run deps:fresh:major
  157. # 或者 yarn deps:fresh:major
  158. # 次版本更新,可能是破坏性更新,谨慎使用,做好测试
  159. pnpm deps:fresh:minor
  160. # 或者 npm run deps:fresh:minor
  161. # 或者 yarn deps:fresh:minor
  162. # 补丁版本更新
  163. pnpm deps:fresh:patch
  164. # 或者 npm run deps:fresh:patch
  165. # 或者 yarn deps:fresh:patch
  166. ```
  167. ```shell
  168. # 以上命令仅对包信息 package.json 进行写入,需要重新执行包安装命令
  169. pnpm i
  170. # 或者 npm i
  171. # 或者 yarn
  172. ```
  173. 13. 代码规范校验
  174. ```shell
  175. pnpm lint
  176. # 或者 npm run lint
  177. # 或者 yarn lint
  178. # 校验时修复
  179. pnpm lint:fix
  180. # 或者 npm run lint:fix
  181. # 或者 yarn lint:fix
  182. ```
  183. <br />
  184. <br />
  185. ## 动机 🐗
  186. 为什么要做这个 **模板** 呢?
  187. 1. 为下次开发节省浪费在配置上的时间
  188. 2. 结合主流插件整合现代开发架构,提高开发效率
  189. <br />
  190. <br />
  191. ## 使用场景 🐻
  192. 什么时候你应该用?
  193. 1. 不想浪费时间在项目配置上
  194. 2. 希望尝试用更现代的方式开发 `web` 应用,提高开发效率
  195. <br />
  196. <br />
  197. ## 启发 🐃
  198. 该模板受 **[vitesse](https://github.com/antfu/vitesse)** 启发,如果你有 `SSG` 的场景,那么推荐你始终使用 **[vitesse](https://github.com/antfu/vitesse)**。
  199. <br />
  200. <br />
  201. ## 组织 🦔
  202. 欢迎关注 **帝莎编程**
  203. - [官网](http://dishaxy.dishait.cn/)
  204. - [Gitee](https://gitee.com/dishait)
  205. - [Github](https://github.com/dishait)
  206. - [网易云课堂](https://study.163.com/provider/480000001892585/index.htm?share=2&shareId=480000001892585)
  207. <br />
  208. <br />
  209. ## 详情 🐳
  210. ### [1. Vite 的](https://cn.vitejs.dev/)
  211. 该模板采用 **[vite](https://cn.vitejs.dev/)** 作为构建工具,你可以在根目录下的 `vite.config.ts` 对项目的构建进行配置。
  212. 对于众多主流插件的引入和繁杂配置已经整合到根目录下的预设 `presets` 中,大多数情况下你是不需要重新对它们进行配置的。
  213. <br />
  214. ### [2. Vue3 的](https://v3.cn.vuejs.org/)
  215. <br />
  216. ### [3. 文件路由](https://github.com/posva/unplugin-vue-router)
  217. 目录结构即路由。
  218. eg:
  219. - `src/pages/index.vue` => `/`
  220. - `src/pages/about.vue` => `/about`
  221. - `src/pages/users/index.vue` => `/users`
  222. - `src/pages/users/profile.vue` => `/users/profile`
  223. - `src/pages/users/[id].vue` => `/users/:id`
  224. - `src/pages/[user]/settings.vue` => `/:user/settings`
  225. - `src/pages/[...notFound].vue` => 404 路由
  226. 具体可见 👉 [unplugin-vue-router](https://github.com/posva/unplugin-vue-router)
  227. <br />
  228. ### [4. 布局系统](https://github.com/dishait/vite-plugin-vue-meta-layouts)
  229. #### 默认布局
  230. `src/layouts/default.vue` 将作为默认布局。
  231. ```html
  232. <!-- src/layouts/default.vue -->
  233. <template>
  234. 我是默认布局
  235. <router-view />
  236. <!-- 页面视图出口 -->
  237. </template>
  238. ```
  239. 此时 `src/pages/index.vue`
  240. ```html
  241. <!-- src/pages/index.vue -->
  242. <template>
  243. <div>我是首页</div>
  244. </template>
  245. ```
  246. 路由到 `/`时,页面将渲染
  247. ```html
  248. 我是默认布局 我是首页
  249. ```
  250. 此时 `src/pages/about.vue`
  251. ```html
  252. <!-- src/pages/about.vue -->
  253. <template>
  254. <div>我是关于页</div>
  255. </template>
  256. ```
  257. 路由到 `/about` 时,页面将渲染
  258. ```html
  259. 我是默认布局 我是关于页
  260. ```
  261. <br />
  262. #### 非默认布局
  263. 随便创建一个 `src/layouts/custom.vue`
  264. ```html
  265. <!-- src/layouts/custom.vue -->
  266. <template>
  267. 我是非默认布局custom
  268. <router-view />
  269. <!-- 页面视图出口 -->
  270. </template>
  271. ```
  272. 此时 `src/pages/index.vue` 内
  273. ```html
  274. <!-- src/pages/index.vue -->
  275. <template>
  276. <div>我是首页</div>
  277. </template>
  278. <!-- 添加自定义块 👇 -->
  279. <route lang="yaml"> meta: layout: custom </route>
  280. ```
  281. 此时路由到 `/`, 页面将渲染
  282. ```html
  283. 我是非默认布局custom 我是首页
  284. ```
  285. 具体可见 👉 [vite-plugin-vue-meta-layouts](https://github.com/dishait/vite-plugin-vue-meta-layouts)
  286. <br />
  287. ### [5. Mock 支持](https://github.com/vbenjs/vite-plugin-mock)
  288. 在根目录下的 `mock` 目录下,可以在模块中导出默认的 `api` 资源。
  289. 例如 `mock/test.ts` 内导出
  290. ```ts
  291. import { MockMethod } from 'vite-plugin-mock'
  292. export default [
  293. {
  294. url: '/api/get',
  295. method: 'get',
  296. response: () => {
  297. return {
  298. code: 0,
  299. data: {
  300. name: 'vben',
  301. },
  302. }
  303. },
  304. },
  305. ] as MockMethod[]
  306. ```
  307. 在 `src` 中就可以进行模拟请求。
  308. ```html
  309. <script setup lang="ts">
  310. import { useRequest } from 'vue-request'
  311. // 请求接口 /api/get
  312. const { data, loading, error } = useRequest('/api/get')
  313. </script>
  314. <template>
  315. <div>data: {{data}}</div>
  316. <div>loading: {{loading}}</div>
  317. <div>error: {{error}}</div>
  318. </template>
  319. ```
  320. 这里用到 [vue-request](https://cn.attojs.org/) 去做请求,不过因为该 `mock` 拦截的是一整个接口,所以换成 `axios` 等请求库也是可以的。
  321. 更多 `mock` 设置可见 👉 [vite-plugin-mock](https://github.com/vbenjs/vite-plugin-mock)
  322. <br />
  323. ### [6. Api 自动引入](https://github.com/antfu/unplugin-auto-import)
  324. 原本 `vue` 的 `api` 需要自行 `import`。
  325. ```ts
  326. import { ref, computed } from 'vue'
  327. const count = ref(0)
  328. const doubled = computed(() => count.value * 2)
  329. ```
  330. 现在可以直接使用。
  331. ```ts
  332. const count = ref(0)
  333. const doubled = computed(() => count.value * 2)
  334. ```
  335. 而且上边的 `api` 是按需自动引入的。
  336. 目前模板支持自动引入 `api` 的库列表包括:
  337. 1. vue
  338. 2. pinia
  339. 3. vueuse
  340. 4. vue-i18n
  341. 5. vue-router
  342. 当然还有项目中的自动引入,只需要满足以下规范即可。
  343. 1. `src/composables` 的导出将被按需自动引入。
  344. 例如有个 `src/composables/foo.ts`
  345. ```ts
  346. // default 导出
  347. export default 1000
  348. ```
  349. 此时就不再需要 `import`了
  350. ```html
  351. <script setup lang="ts">
  352. console.log(foo) // 输出 1000
  353. </script>
  354. <template>
  355. <div @click="store.inc()">{{store.counter}}</div>
  356. </template>
  357. ```
  358. 1. `src/stores` 的导出将被按需自动引入。
  359. 例如有个 `src/stores/counterStore.ts`
  360. ```ts
  361. // default 导出
  362. export default defineStore('counter', {
  363. state() {
  364. return {
  365. counter: 1,
  366. }
  367. },
  368. actions: {
  369. inc() {
  370. this.counter++
  371. },
  372. },
  373. })
  374. ```
  375. 此时就不再需要 `import`了
  376. ```html
  377. <script setup lang="ts">
  378. const store = counterStore()
  379. </script>
  380. <template>
  381. <div @click="store.inc()">{{store.counter}}</div>
  382. </template>
  383. ```
  384. 具体可见 👉 [unplugin-auto-import](https://github.com/antfu/unplugin-auto-import)。
  385. <br />
  386. ### [7. 组件自动引入](https://github.com/antfu/unplugin-auto-import)
  387. 原来需要 `import`
  388. ```html
  389. <!-- src/pages/index.vue -->
  390. <script setup lang="ts">
  391. import Hello from '../components/Hello.vue'
  392. </script>
  393. <template>
  394. <Hello />
  395. </template>
  396. ```
  397. 现在只要在 `src/components` 下定义的组件都将会按需引入,即 `import` 是不需要的。
  398. ```html
  399. <!-- src/pages/index.vue -->
  400. <template>
  401. <Hello />
  402. </template>
  403. ```
  404. 同时流行组件库自动引入也是支持的,例如 `Naive ui`。
  405. 只需安装依赖。
  406. ```shell
  407. pnpm add naive-ui
  408. # 或者 npm i naive-ui
  409. # 或者 yarn add naive-ui
  410. ```
  411. 即可在模板中使用。
  412. ```html
  413. <!-- src/pages/index.vue -->
  414. <template>
  415. <n-button type="success">Success</n-button>
  416. </template>
  417. ```
  418. 目前支持的组件库有:
  419. [vant](https://github.com/youzan/vant)
  420. [idux](https://github.com/IDuxFE/idux)
  421. [devui](https://github.com/DevCloudFE/vue-devui)
  422. [quasar](https://github.com/quasarframework/quasar)
  423. [varlet](https://github.com/varletjs/varlet)
  424. [inkline](https://github.com/inkline/inkline)
  425. [vuetify](https://github.com/vuetifyjs/vuetify)
  426. [naive-ui](https://github.com/TuSimple/naive-ui)
  427. [primevue](https://github.com/primefaces/primevue)
  428. [layui-vue](https://gitee.com/layui/layui-vue)
  429. [view-design](https://iviewui.com/)
  430. [arco-design](https://github.com/arco-design/arco-design)
  431. [element-plus](https://github.com/element-plus/element-plus)
  432. [ant-design-vue](https://github.com/vueComponent/ant-design-vue)
  433. [@headlessui/vue](https://github.com/tailwindlabs/headlessui)
  434. [tdesign-vue-next](https://github.com/Tencent/tdesign-vue-next)
  435. [@vueuse/components](https://github.com/vueuse/vueuse/tree/main/packages/components)
  436. 具体可见 👉 [unplugin-vue-components](https://github.com/antfu/unplugin-vue-components)
  437. <br />
  438. ### [8. 图标自动引入](https://github.com/antfu/unplugin-auto-import)
  439. 可前往 👉 [icones](https://icones.netlify.app/),随意选择点击进入其中一个图标库
  440. ![icons-first-step](public/docs/icons-first-step.png)
  441. 再点击选择其中一个喜欢的图标
  442. ![icons-second-step](public/docs/icons-second-step.png)
  443. 复制其名称
  444. ![icons-second-step](public/docs/icons-third-step.png)
  445. 在模板中即可直接用组件的形式直接使用,注意加上前缀 `i-`。
  446. ```html
  447. <template>
  448. <i-mdi:account-box-multiple />
  449. </template>
  450. ```
  451. 保存后等待自动下载该图标库后,就可以在页面中看到对应图标。
  452. 注意自动下载图标需要 `pnpm` 包管理器支持,如果你是其他包管理器需要手动安装图标集 👇👇👇
  453. 图标集名称为 `@iconify/` 加 `:` 前缀,例如上边的图标是 `mdi:account-box-multiple`,图标集名称就为 `@iconify/mdi`。
  454. ```shell
  455. npm i @iconify/mdi -D
  456. # 或者 yarn add @iconify/mdi -D
  457. ```
  458. 同时推荐你使用 `vscode` 插件 [Iconify IntelliSense](https://marketplace.visualstudio.com/items?itemName=antfu.iconify)。
  459. 该插件会在模板中显示图标的预览。就像这样 👇
  460. ```html
  461. <!-- 模板中代码 -->
  462. <template>
  463. <i-mdi:account-box-multiple />
  464. </template>
  465. ```
  466. 将在 `vscode` 中得到图标预览
  467. ![icons-preview](public/docs/icons-preview.png)
  468. 具体可见 👉 [unplugin-icons](https://github.com/antfu/unplugin-icons)
  469. <br />
  470. ### [9. VueUse 支持](https://vueuse.org/)
  471. `VueUse` 是一个超级强的 `hooks` 库,例如你要获取鼠标位置,只需要这样 👇
  472. ```html
  473. <script setup lang="ts">
  474. // useMouse 被自动按需引入了,不需要import
  475. const { x, y } = useMouse()
  476. </script>
  477. <template>
  478. <div>x坐标 {{x}}</div>
  479. <div>y坐标 {{y}}</div>
  480. </template>
  481. ```
  482. 具体可见 👉 [VueUse](https://vueuse.org/)
  483. <br />
  484. ### [10. TypeScript 的](https://www.tslang.cn/)
  485. 不需要重新配置,直接用 `ts` 书写就行了。
  486. <br />
  487. ### [11. Windi CSS 的](https://cn.windicss.org/)
  488. `Windi CSS` 是一个开发中速度更快的 `原子css` 库。
  489. 直接在模板中用就行了,不需要配置。
  490. ```html
  491. <template>
  492. <div class="bg-red-500 text-white">我是红色背景的白色文本</div>
  493. <template></template
  494. ></template>
  495. ```
  496. 上述模板将渲染红色背景白色的字。
  497. 同时支持 [属性化模式](https://cn.windicss.org/features/attributify.html#attributify-mode),即可以用简写。该功能默认关闭,可在 `windi.config.ts` 中 设置 `attributify` 为 `true` 开启。
  498. ```html
  499. <template>
  500. <div text="white" bg="red-500">我是红色背景的白色文本</div>
  501. <template></template
  502. ></template>
  503. ```
  504. 这在调整边距尺寸以及等方面可以减少代码量。
  505. 同时预设 [排版](https://cn.windicss.org/plugins/official/typography.html) 插件,解决简单的布局困难问题。
  506. 当然也支持 [可视化分析器](https://cn.windicss.org/features/analyzer.html) 生成 `原子css` 报告.
  507. 在终端中输入命令,即可看到报告
  508. ```shell
  509. pnpm analysis
  510. # 或者 npm run analysis
  511. # 或者 yarn analysis
  512. ```
  513. 或者打包输出该报告
  514. ```shell
  515. pnpm analysis:build
  516. # 或者 npm run analysis:build
  517. # 或者 yarn analysis:build
  518. ```
  519. 具体可见 👉 [Windi CSS](https://cn.windicss.org/)
  520. <br />
  521. ### [12. 暗黑模式支持](https://cn.windicss.org/features/dark-mode.html#dark-mode)
  522. 暗黑模式由 `Windi CSS` 的 `暗黑模式` 和 `VueUse` 实现。
  523. `src/composables` 目录用来存储 `composition-api` 模块。
  524. 该目录下预设了`useDarks` 模块,该模块导出 `isDark` 和 `toggleDark` 用来显示和切换暗黑模式。
  525. ```ts
  526. // src/composables/useDarks.ts
  527. // vueuse的 api 会自动按需引入,无需import
  528. export const isDark = useDark()
  529. export const toggleDark = useToggle(isDark)
  530. export const useDarks = () => ({ isDark, toggleDark })
  531. ```
  532. 模板中即可直接用
  533. ```html
  534. <script setup lang="ts">
  535. import { useDarks } from '../composables/useDarks'
  536. const { isDark, toggleDark } = useDarks()
  537. </script>
  538. <template>
  539. <div m="6">Hello,This is the tov template!!</div>
  540. <div m="6" cursor="pointer" @click="toggleDark()">
  541. light: {{ isDark }} click me!!
  542. </div>
  543. </template>
  544. ```
  545. 具体可见 👉 [暗黑模式](https://cn.windicss.org/features/dark-mode.html#dark-mode)
  546. <br />
  547. ### [13. SWR 请求支持](https://cn.attojs.org/)
  548. SWR 是更现代的请求方式,具体可见文章 👉 [SWR](https://zhuanlan.zhihu.com/p/89570321)。
  549. 而 [vue-request](https://cn.attojs.org/) 是一个 `SWR` 的 `Vue` 版本请求库。
  550. 你可以这样用,例如请求 `/api/test`
  551. ```html
  552. <script setup lang="ts">
  553. import { useRequest } from 'vue-request'
  554. const { data, loading, error } = useRequest('/api/test')
  555. </script>
  556. <template>
  557. <div>data: {{data}}</div>
  558. <div>error: {{error}}</div>
  559. <div>loading: {{loading}}</div>
  560. </template>
  561. ```
  562. 所有基本的数据,状态和缓存都帮你搞定了,不需要重新封装。
  563. 具体可见 👉 [vue-request](https://cn.attojs.org/)
  564. <br />
  565. ### [14. pinia 状态管理](https://pinia.vuejs.org/)
  566. `pinia` 是下一代的状态管理库,比 `vuex` 更简单,`ts` 支持更好。
  567. 你可以在 `src/stores` 中进行状态的定义。
  568. 例如创建 `src/stores/counter.ts` 👇
  569. ```ts
  570. // src/stores/counter.ts
  571. import { defineStore } from 'pinia'
  572. export const useCounterStore = defineStore('counter', {
  573. state: () => {
  574. return { count: 0 }
  575. },
  576. actions: {
  577. inc() {
  578. this.count++
  579. },
  580. },
  581. })
  582. ```
  583. 定义完后在 `setup` 中直接使用即可
  584. ```html
  585. <!-- src/pages/index.vue -->
  586. <script setup lang="ts">
  587. import { useCounterStore } from "../stores/counter"
  588. const Counter = useCounterStore()
  589. <script>
  590. <template>
  591. <div @click="Counter.inc">{{Counter.count}}</div>
  592. </template>
  593. ```
  594. 更多具体使用可见 👉 [pinia](https://pinia.vuejs.org/)
  595. <br />
  596. ### [15. pnpm 包管理器](https://pnpm.io/zh/)
  597. `pnpm` 是非常优秀的包管理器,更快、更节省空间、更合理。
  598. 具体可见 👉 [pnpm](https://pnpm.io/zh/)
  599. <br />
  600. ### [16. 跳转进度条支持](https://github.com/rstacruz/nprogress)
  601. 跳转进度条由 `nprogress` 实现,可在`src/styles/main.css` 中调整配色。
  602. ```css
  603. /** src/styles/main.css **/
  604. /** 省略其他样式 **/
  605. #nprogress .bar {
  606. @apply bg-purple-700 bg-opacity-75; /** 配色 **/
  607. position: fixed;
  608. z-index: 1031;
  609. top: 0;
  610. left: 0;
  611. width: 100%;
  612. height: 2px;
  613. }
  614. ```
  615. 关于 `@apply` 由 [Windi Css apply 指令](https://cn.windicss.org/features/directives.html#apply) 实现。
  616. 具体可见 👉 [nprogress](https://github.com/rstacruz/nprogress)
  617. <br />
  618. ### [17. Inspect 调试支持](https://github.com/antfu/vite-plugin-inspect)
  619. 启动项目
  620. ```shell
  621. pnpm dev
  622. # 或者 npm run dev
  623. # 或者 yarn dev
  624. ```
  625. 可以看到一个 `url`
  626. ![inspect](public/docs/inspect.png)
  627. 先进入 `http://localhost:3000`,再进入 `http://localhost:3000/__inspect/` 即可获得依赖图谱。
  628. ![inspect-atlas](public/docs/inspect-atlas.png)
  629. 通过顶部的一些按钮还可以调整依赖呈现。
  630. 具体可见 👉 [vite-plugin-inspect](https://github.com/antfu/vite-plugin-inspect)
  631. <br />
  632. ### [18. 插件自动加载支持](https://github.com/dishait/vite-plugin-use-modules)
  633. 只需要在 `src/modules` 中的模块里导出默认函数即可。
  634. 例如 `pinia`,只需要这样做。
  635. ```ts
  636. // src/modules/pinia.ts
  637. import { App } from 'vue'
  638. // 导出 default 接口
  639. export default (app: App) => app.use(createPinia())
  640. ```
  641. 或者 `vue-router`
  642. ```ts
  643. // src/modules/router.ts
  644. import { App } from 'vue'
  645. // 省略各种配置
  646. // 导出 default 接口
  647. export default (app: App) => app.use(router)
  648. ```
  649. 当然 `pinia` 和 `vue-router` 已经预设好了,你不需要重新关注他们。
  650. 具体可见 👉 [vite-plugin-use-modules](https://github.com/dishait/vite-plugin-use-modules)
  651. <br />
  652. ### [19. Vitest 单元测试支持](https://vitest.dev/guide/#configuring-vitest)
  653. 在 `src/test` 目录中可以书写单元测试。
  654. ```ts
  655. import { it, describe, expect, assert } from 'vitest'
  656. describe('suite name', () => {
  657. it('foo', () => {
  658. expect(1 + 1).toEqual(2)
  659. expect(true).to.be.true
  660. })
  661. it('bar', () => {
  662. assert.equal(Math.sqrt(4), 2)
  663. })
  664. it('snapshot', () => {
  665. expect({ foo: 'bar' }).toMatchSnapshot()
  666. })
  667. })
  668. ```
  669. 然后在终端中输入命令即可测试
  670. ```shell
  671. pnpm test
  672. # 或者 npm run test
  673. # 或者 yarn test
  674. ```
  675. 或者生成报告
  676. ```shell
  677. pnpm coverage
  678. # 或者 npm run coverage
  679. # 或者 yarn coverage
  680. ```
  681. 具体可见 👉 [Vitest](https://vitest.dev/guide/#configuring-vitest)
  682. <br />
  683. ### [20. 支持 Markdown 渲染](https://github.com/antfu/vite-plugin-md)
  684. `markdown` 渲染可以用来书写一些简单的说明。
  685. 只需要把 `src/pages` 目录下的页面后缀由 `.vue` 改为 `.md`,然后再改为 `markdown` 语法即可。
  686. 例如 `src/pages/about.md`
  687. ```md
  688. ## About Page
  689. > The page is markdown file
  690. ```
  691. 当你路由到 `/about` 后即可看到对应的 `markdown` 渲染。
  692. 具体可见 👉 [vite-plugin-md](https://github.com/antfu/vite-plugin-md)
  693. <br />
  694. <br />
  695. ### 21. 路径别名 `~` 支持
  696. `~` 路径将被导向项目的 `src` 目录。
  697. ```html
  698. <!-- src/pages/index.vue -->
  699. <script lang="ts" setup>
  700. import { useDarks } from '~/composables/dark'
  701. // 等价于
  702. // import { useDarks } from "../composables/dark"
  703. </script>
  704. ```
  705. <br />
  706. <br />
  707. ### 22. 命令行自动创建与删除
  708. 只要输入 👇,即可创建一个标准的页面或组件
  709. ```shell
  710. pnpm auto:create
  711. # 或者 npm run auto:create
  712. # 或者 yarn auto:create
  713. ```
  714. 当然也可以进行删除 👇
  715. ```shell
  716. pnpm auto:remove
  717. # 或者 npm run auto:remove
  718. # 或者 yarn auto:remove
  719. ```
  720. <br />
  721. <br />
  722. ### [23. i18n 国际化支持](https://github.com/intlify/bundle-tools/tree/main/packages/vite-plugin-vue-i18n)
  723. 在日常的业务当中,可能会存在一些需要国际化的场景。那么只需要在根目录下的 `locales` 中定义不同语言的 `yml` 即可在项目中做到开箱即用的国际化支持。
  724. 比如 `locales/en.yml` 中用来定义需要国际化支持的英文内容。
  725. ```yml
  726. # locales/en.yml
  727. # English
  728. index: index
  729. about: about
  730. not-found: Notfound
  731. ```
  732. 又如 `locales/zh-CN.yml` 中用来定义需要国际化支持的中文内容。
  733. ```yml
  734. # locales/zh-CN.yml
  735. # 中文
  736. index: 主页
  737. about: 关于
  738. not-found: 未找到页面
  739. ```
  740. 此时在组件中即可这样用 👇
  741. ```html
  742. <script setup>
  743. // 该api是全局按需引入的,所以可以直接用
  744. // t 用来绑定特定的语块
  745. const { t, locale } = useI18n()
  746. const toggleLocale = () => {
  747. // locale.value 用来表示当前所属语言,可修改进行语言切换
  748. locale.value = locale.value === 'zh-CN' ? 'en' : 'zh-CN'
  749. }
  750. </script>
  751. <template>
  752. <div m="6" cursor="pointer" @click="toggleLocale()">
  753. language: {{ t('index') }} click me!!
  754. </div>
  755. </template>
  756. ```
  757. 更详细的说明可见用到的 `vite 插件` 👉 [@intlify/vite-plugin-vue-i18n](https://github.com/intlify/bundle-tools/tree/main/packages/vite-plugin-vue-i18n) 与 `vue插件` [vue-i18n](https://github.com/intlify/vue-i18n-next)。
  758. 另外 `yml` 是目前前端中流行的配置文件格式,语法可见阮一峰先生的 👉 [YAML 语言教程](http://www.ruanyifeng.com/blog/2016/07/yaml.html)。
  759. `vscode` 插件推荐:
  760. - 多合一的 `i18n` 支持 [i18n Ally](https://marketplace.visualstudio.com/items?itemName=Lokalise.i18n-ally)
  761. <br />
  762. <br />
  763. ### 24. 漂亮的 `404` 页支持
  764. 在日常业务中,当用户访问不存在的页面时,应该给到我们的用户一个不存在的信息提示,而这个提示的页面就是 `404` 页。
  765. 你可以随便访问一个不存在的页面,例如 `/bucunzai`
  766. ![notFound](public/docs/notFound.png)
  767. 当然还有暗黑模式适应。
  768. ![notFound-dark](public/docs/notFound-dark.png)
  769. 也支持简单的响应式适应。例如移动端浏览器上会有正确的显示。
  770. 当然如果这个 `404` 的封面不符合你的口味,那么可以在 `pages/[...notFound].vue` 中修改 `img` 标签的 `src`。默认是 `32.svg`,支持 `1 ~ 33` 的 `svg`。
  771. 例如,默认 👇
  772. ```html
  773. <!-- 省略各种代码 -->
  774. <template>
  775. <img src="/notFound/32.svg" class="cover" alt="page not found" />
  776. </template>
  777. ```
  778. 修改 `/notFound/32.svg` 为 `/notFound/33.svg`
  779. ```html
  780. <!-- 省略各种代码 -->
  781. <template>
  782. <img src="/notFound/33.svg" class="cover" alt="page not found" />
  783. </template>
  784. ```
  785. 即可切换封面为 👇
  786. ![notFound-other](public/docs/notFound-other.png)
  787. <br />
  788. <br />
  789. ### 25. [`tsx` 支持](https://github.com/vitejs/vite/tree/main/packages/plugin-vue-jsx)
  790. 只需要 `tsx` 文件放在 `src/components` 下,即可直接在模板中使用。
  791. 例如你有一个 `src/components/foo.tsx` 文件,那么即可直接在模板中使用。
  792. ```tsx
  793. // src/components/foo.tsx
  794. export default defineComponent({
  795. render() {
  796. return <div>Test</div>
  797. },
  798. })
  799. ```
  800. ```html
  801. <template>
  802. <foo />
  803. </template>
  804. ```
  805. 具体可见 👉 [@vitejs/plugin-vue-jsx](https://github.com/vitejs/vite/tree/main/packages/plugin-vue-jsx)
  806. <br />
  807. <br />
  808. ### 26. [`gzip` 资源压缩支持](https://github.com/vbenjs/vite-plugin-compression)
  809. 生产环境下开箱即用的 `gzip` 资源压缩,无需配置。
  810. 具体可见 👉 [vite-plugin-compression](https://github.com/vbenjs/vite-plugin-compression)
  811. <br />
  812. <br />
  813. ### 27. [环境变量配置支持](https://cn.vitejs.dev/guide/env-and-mode.html)
  814. 根目录下有三个环境变量配置文件 `.env`,`.env.development` 和 `.env.production` 用来对项目进行配置。
  815. <br />
  816. <br />
  817. ### 28. 统一的代码规范与风格支持
  818. 由 [eslint](https://github.com/eslint/eslint) 提供的代码规范校验,使用 [prettier](https://github.com/prettier/prettier) 统一代码风格。
  819. 由 [husky](https://github.com/typicode/husky) + [lint-staged](https://github.com/okonet/lint-staged) 提供的 `commit` 时校验。
  820. <br />
  821. <br />
  822. ### 29. [生产环境自动移除开发日志](https://github.com/dishait/vite-plugin-removelog)
  823. 生产环境下 `console.log`,`console.warn` 与 `console.error` 等日志会被自动移除掉,以避免开发时日志的泄漏。
  824. 具体可见 👉 [vite-plugin-removelog](https://github.com/dishait/vite-plugin-removelog)
  825. <br />
  826. <br />
  827. ### 30. [defineOptions 支持](https://github.com/sxzz/unplugin-vue-macros/blob/main/README-zh-CN.md)
  828. ```html
  829. <script setup lang="ts">
  830. // 定义额外的 options
  831. defineOptions({
  832. name: 'Foo'
  833. })
  834. </script>
  835. ```
  836. 具体可见 👉 [unplugin-vue-macros](https://github.com/sxzz/unplugin-vue-macros/blob/main/README-zh-CN.md)
  837. <br />
  838. <br />
  839. <br />
  840. ## License
  841. Made with [markthree](./LICENSE)
  842. Published under [MIT License](./LICENSE).
  843. <br />