plugins.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import path from 'node:path'
  2. import process from 'node:process'
  3. import type { PluginOption } from 'vite'
  4. import VueRouter from 'unplugin-vue-router/vite'
  5. import vue from '@vitejs/plugin-vue'
  6. import vueJsx from '@vitejs/plugin-vue-jsx'
  7. import vueLegacy from '@vitejs/plugin-legacy'
  8. import VueDevTools from 'vite-plugin-vue-devtools'
  9. import autoImport from 'unplugin-auto-import/vite'
  10. import { VueRouterAutoImports } from 'unplugin-vue-router'
  11. import components from 'unplugin-vue-components/vite'
  12. import Unocss from 'unocss/vite'
  13. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  14. import { vitePluginFakeServer } from 'vite-plugin-fake-server'
  15. import { compression } from 'vite-plugin-compression2'
  16. import Archiver from 'vite-plugin-archiver'
  17. import TurboConsole from 'unplugin-turbo-console/vite'
  18. import banner from 'vite-plugin-banner'
  19. import boxen from 'boxen'
  20. import picocolors from 'picocolors'
  21. export default function createVitePlugins(viteEnv, isBuild = false) {
  22. const vitePlugins: (PluginOption | PluginOption[])[] = [
  23. VueRouter({
  24. routesFolder: './src/views',
  25. dts: './src/types/typed-router.d.ts',
  26. exclude: ['**/components', '**/_*/**', '**/_*'],
  27. }),
  28. vue(),
  29. vueJsx(),
  30. vueLegacy({
  31. renderLegacyChunks: false,
  32. modernPolyfills: [
  33. 'es.array.at',
  34. ],
  35. }),
  36. // https://github.com/vuejs/devtools-next
  37. viteEnv.VITE_OPEN_DEVTOOLS === 'true' && VueDevTools(),
  38. // https://github.com/unplugin/unplugin-auto-import
  39. autoImport({
  40. imports: [
  41. 'vue',
  42. 'pinia',
  43. VueRouterAutoImports,
  44. {
  45. 'vue-router/auto': ['useLink'],
  46. },
  47. ],
  48. dts: './src/types/auto-imports.d.ts',
  49. dirs: [
  50. './src/utils/composables/**',
  51. ],
  52. }),
  53. // https://github.com/unplugin/unplugin-vue-components
  54. components({
  55. dirs: [
  56. 'src/components/*',
  57. 'src/ui-kit',
  58. ],
  59. deep: false,
  60. include: [/\.vue$/, /\.vue\?vue/, /\.tsx$/],
  61. dts: './src/types/components.d.ts',
  62. }),
  63. Unocss(),
  64. // https://github.com/vbenjs/vite-plugin-svg-icons
  65. createSvgIconsPlugin({
  66. iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/')],
  67. symbolId: 'icon-[dir]-[name]',
  68. svgoOptions: isBuild,
  69. }),
  70. // https://github.com/condorheroblog/vite-plugin-fake-server
  71. vitePluginFakeServer({
  72. logger: !isBuild,
  73. include: 'src/mock',
  74. infixName: false,
  75. enableProd: isBuild && viteEnv.VITE_BUILD_MOCK === 'true',
  76. }),
  77. // https://github.com/nonzzz/vite-plugin-compression
  78. viteEnv.VITE_BUILD_COMPRESS?.split(',').includes('gzip') && compression(),
  79. viteEnv.VITE_BUILD_COMPRESS?.split(',').includes('brotli') && compression({
  80. exclude: [/\.(br)$/, /\.(gz)$/],
  81. algorithm: 'brotliCompress',
  82. }),
  83. viteEnv.VITE_BUILD_ARCHIVE && Archiver({
  84. archiveType: viteEnv.VITE_BUILD_ARCHIVE,
  85. }),
  86. // https://github.com/unplugin/unplugin-turbo-console
  87. TurboConsole(),
  88. // https://github.com/chengpeiquan/vite-plugin-banner
  89. banner(`
  90. /**
  91. * 由 Fantastic-mobile 提供技术支持
  92. * Powered by Fantastic-mobile
  93. * https://fantastic-mobile.hurui.me/
  94. */
  95. `),
  96. {
  97. name: 'vite-plugin-debug-plugin',
  98. transform: (code, id) => {
  99. if (/src\/main.ts$/.test(id)) {
  100. if (viteEnv.VITE_APP_DEBUG_TOOL === 'eruda') {
  101. code = code.concat(`
  102. import eruda from 'eruda'
  103. eruda.init()
  104. `)
  105. }
  106. else if (viteEnv.VITE_APP_DEBUG_TOOL === 'vconsole') {
  107. code = code.concat(`
  108. import VConsole from 'vconsole'
  109. new VConsole()
  110. `)
  111. }
  112. return {
  113. code,
  114. map: null,
  115. }
  116. }
  117. },
  118. },
  119. {
  120. name: 'appInfo',
  121. apply: 'serve',
  122. async buildStart() {
  123. const { bold, green, cyan, bgGreen, underline } = picocolors
  124. // eslint-disable-next-line no-console
  125. console.log(
  126. boxen(
  127. `${bold(green(`由 ${bgGreen('Fantastic-mobile')} 驱动`))}\n\n${underline('https://fantastic-mobile.hurui.me')}\n\n当前使用:${cyan('基础版')}`,
  128. {
  129. padding: 1,
  130. margin: 1,
  131. borderStyle: 'double',
  132. textAlignment: 'center',
  133. },
  134. ),
  135. )
  136. },
  137. },
  138. ]
  139. return vitePlugins
  140. }