plugins.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import type { PluginOption } from 'vite'
  2. import path from 'node:path'
  3. import process from 'node:process'
  4. import vueLegacy from '@vitejs/plugin-legacy'
  5. import vue from '@vitejs/plugin-vue'
  6. import vueJsx from '@vitejs/plugin-vue-jsx'
  7. import boxen from 'boxen'
  8. import picocolors from 'picocolors'
  9. import Unocss from 'unocss/vite'
  10. import autoImport from 'unplugin-auto-import/vite'
  11. import TurboConsole from 'unplugin-turbo-console/vite'
  12. import components from 'unplugin-vue-components/vite'
  13. import { loadEnv } from 'vite'
  14. import AppLoading from 'vite-plugin-app-loading'
  15. import Archiver from 'vite-plugin-archiver'
  16. import banner from 'vite-plugin-banner'
  17. import { compression } from 'vite-plugin-compression2'
  18. import { envParse, parseLoadedEnv } from 'vite-plugin-env-parse'
  19. import { vitePluginFakeServer } from 'vite-plugin-fake-server'
  20. import Pages from 'vite-plugin-pages'
  21. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  22. import VueDevTools from 'vite-plugin-vue-devtools'
  23. import Layouts from 'vite-plugin-vue-meta-layouts'
  24. export default function createVitePlugins(mode: string, isBuild = false) {
  25. const viteEnv = parseLoadedEnv(loadEnv(mode, process.cwd()))
  26. const vitePlugins: (PluginOption | PluginOption[])[] = [
  27. vue(),
  28. vueJsx(),
  29. vueLegacy({
  30. renderLegacyChunks: false,
  31. modernPolyfills: [
  32. 'es.array.at',
  33. 'es.array.find-last',
  34. 'es.object.has-own',
  35. ],
  36. }),
  37. // https://github.com/vuejs/devtools
  38. viteEnv.VITE_OPEN_DEVTOOLS && VueDevTools({
  39. launchEditor: viteEnv.VITE_VUE_DEVTOOLS_LAUNCH_EDITOR ?? 'vscode',
  40. }),
  41. envParse({
  42. dtsPath: 'src/types/env.d.ts',
  43. }),
  44. // https://github.com/unplugin/unplugin-auto-import
  45. autoImport({
  46. imports: [
  47. 'vue',
  48. 'vue-router',
  49. 'pinia',
  50. ],
  51. dts: './src/types/auto-imports.d.ts',
  52. dirs: [
  53. './src/store/modules',
  54. './src/utils/composables',
  55. ],
  56. }),
  57. // https://github.com/unplugin/unplugin-vue-components
  58. components({
  59. globs: [
  60. 'src/ui/components/*/index.vue',
  61. 'src/components/*/index.vue',
  62. ],
  63. dts: './src/types/components.d.ts',
  64. }),
  65. Unocss(),
  66. // https://github.com/vbenjs/vite-plugin-svg-icons
  67. createSvgIconsPlugin({
  68. iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/')],
  69. symbolId: 'icon-[dir]-[name]',
  70. svgoOptions: isBuild,
  71. }),
  72. // https://github.com/condorheroblog/vite-plugin-fake-server
  73. vitePluginFakeServer({
  74. logger: !isBuild,
  75. include: 'src/mock',
  76. infixName: false,
  77. enableProd: isBuild && viteEnv.VITE_BUILD_MOCK,
  78. }),
  79. // https://github.com/dishait/vite-plugin-vue-meta-layouts
  80. Layouts({
  81. defaultLayout: 'index',
  82. }),
  83. // https://github.com/hannoeru/vite-plugin-pages
  84. Pages({
  85. dirs: 'src/views',
  86. exclude: [
  87. '**/components/**/*.vue',
  88. ],
  89. }),
  90. // https://github.com/nonzzz/vite-plugin-compression
  91. viteEnv.VITE_BUILD_COMPRESS && compression({
  92. exclude: [/\.(br)$/, /\.(gz)$/],
  93. algorithms: viteEnv.VITE_BUILD_COMPRESS.split(',').map((item: string) => ({
  94. gzip: 'gzip',
  95. brotli: 'brotliCompress',
  96. }[item])),
  97. }),
  98. viteEnv.VITE_BUILD_ARCHIVE && Archiver({
  99. archiveType: viteEnv.VITE_BUILD_ARCHIVE,
  100. }),
  101. AppLoading('loading.html'),
  102. // https://github.com/unplugin/unplugin-turbo-console
  103. TurboConsole(),
  104. // https://github.com/chengpeiquan/vite-plugin-banner
  105. banner(`
  106. /**
  107. * 由 Fantastic-admin 提供技术支持
  108. * Powered by Fantastic-admin
  109. * https://fantastic-admin.hurui.me
  110. */
  111. `),
  112. {
  113. name: 'vite-plugin-debug-plugin',
  114. enforce: 'pre',
  115. transform: (code, id) => {
  116. if (/src\/main.ts$/.test(id)) {
  117. if (viteEnv.VITE_APP_DEBUG_TOOL === 'eruda') {
  118. code = code.concat(`
  119. import eruda from 'eruda'
  120. eruda.init()
  121. `)
  122. }
  123. else if (viteEnv.VITE_APP_DEBUG_TOOL === 'vconsole') {
  124. code = code.concat(`
  125. import VConsole from 'vconsole'
  126. new VConsole()
  127. `)
  128. }
  129. return {
  130. code,
  131. map: null,
  132. }
  133. }
  134. },
  135. },
  136. {
  137. name: 'vite-plugin-disable-devtool',
  138. enforce: 'pre',
  139. transform: (code, id) => {
  140. if (/src\/main.ts$/.test(id)) {
  141. if (viteEnv.VITE_APP_DISABLE_DEVTOOL) {
  142. // ?ddtk=example
  143. code = code.concat(`
  144. import DisableDevtool from 'disable-devtool'
  145. DisableDevtool({
  146. md5: '1a79a4d60de6718e8e5b326e338ae533',
  147. })
  148. `)
  149. }
  150. return {
  151. code,
  152. map: null,
  153. }
  154. }
  155. },
  156. },
  157. {
  158. name: 'vite-plugin-terminal-info',
  159. apply: 'serve',
  160. async buildStart() {
  161. const { bold, green, cyan, bgGreen, underline } = picocolors
  162. // eslint-disable-next-line no-console
  163. console.log(
  164. boxen(
  165. `${bold(green(`由 ${bgGreen('Fantastic-admin')} 驱动`))}\n\n${underline('https://fantastic-admin.hurui.me')}\n\n当前使用:${cyan('基础版')}`,
  166. {
  167. padding: 1,
  168. margin: 1,
  169. borderStyle: 'double',
  170. textAlignment: 'center',
  171. },
  172. ),
  173. )
  174. },
  175. },
  176. ]
  177. return vitePlugins
  178. }