vitePlugins.config.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import type { PluginOption } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import vueJsx from '@vitejs/plugin-vue-jsx'
  4. import VueMacros from 'vue-macros/vite'
  5. import viteCompression from 'vite-plugin-compression'
  6. import VueDevTools from 'vite-plugin-vue-devtools'
  7. import UnoCSS from 'unocss/vite'
  8. import AutoImport from 'unplugin-auto-import/vite'
  9. import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
  10. import Components from 'unplugin-vue-components/vite'
  11. import type { ImportMetaEnv } from './env'
  12. /**
  13. * 创建 vite 插件
  14. * @param ImportMetaEnv
  15. */
  16. export const createVitePlugins = (viteEnv: ImportMetaEnv): (PluginOption | PluginOption[])[] => {
  17. const { VITE_GLOB_APP_TITLE, VITE_REPORT, VITE_DEVTOOLS, VITE_CODEINSPECTOR } = viteEnv
  18. return [
  19. VueMacros({
  20. plugins: {
  21. vue: vue(),
  22. // vue 可以使用 jsx/tsx 语法
  23. vueJsx: vueJsx(),
  24. },
  25. }),
  26. UnoCSS(),
  27. // NaiveUi 按需引入
  28. AutoImport({
  29. imports: [
  30. 'vue',
  31. {
  32. 'naive-ui': ['useDialog', 'useMessage', 'useNotification', 'useLoadingBar', 'useModal'],
  33. },
  34. ],
  35. dts: 'src/types/auto-imports.d.ts',
  36. }),
  37. Components({
  38. resolvers: [NaiveUiResolver()],
  39. // dts: 'src/types/components.d.ts',
  40. dts: false,
  41. }),
  42. // vue devTools
  43. VITE_DEVTOOLS === 'true' && VueDevTools({ launchEditor: 'code' }),
  44. // 创建打包压缩配置
  45. createCompression(viteEnv),
  46. // 查看配置信息
  47. // {
  48. // name: 'debug-config',
  49. // configResolved(config) {
  50. // console.log('🚀 Vite Final Config:', config)
  51. // },
  52. // },
  53. // 查看中间件信息
  54. {
  55. name: 'inspect-middleware',
  56. configureServer(server) {
  57. console.log('🔍 Middlewares:')
  58. server.middlewares.stack.forEach((layer, i) => {
  59. const route = layer.route || '(root)'
  60. const name = 'name' in layer.handle ? layer.handle['name'] : '(anonymous)'
  61. const handleType = typeof layer.handle
  62. console.log(`${i + 1}. [${route}] → ${name} (${handleType})`)
  63. })
  64. },
  65. },
  66. // 中间件监控
  67. // {
  68. // name: 'middleware-tracker',
  69. // configureServer(server) {
  70. // const stack = server.middlewares.stack
  71. // stack.forEach((layer, i) => {
  72. // const originalHandle = layer.handle
  73. // const name = originalHandle.name || `(anonymous-${i})`
  74. // const handleInfo = originalHandle.toString()
  75. // layer.handle = function wrappedHandle(req, res, next) {
  76. // console.log(`🔥 Middleware [${name}] triggered by: ${req.method} ${req.url}`)
  77. // // if (!originalHandle.name) console.log(`handleInfo: ${handleInfo}`)
  78. // return originalHandle(req, res, next)
  79. // }
  80. // })
  81. // },
  82. // },
  83. ]
  84. }
  85. /**
  86. * @description 根据 compress 配置,生成不同的压缩规则
  87. * @param ImportMetaEnv
  88. */
  89. const createCompression = (viteEnv: ImportMetaEnv): PluginOption | PluginOption[] => {
  90. const { VITE_BUILD_COMPRESS = 'none', VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE } = viteEnv
  91. const compressList = VITE_BUILD_COMPRESS.split(',')
  92. const plugins: PluginOption[] = []
  93. if (compressList.includes('gzip')) {
  94. plugins.push(
  95. viteCompression({
  96. ext: '.gz',
  97. algorithm: 'gzip',
  98. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE === 'true',
  99. }),
  100. )
  101. }
  102. if (compressList.includes('brotli')) {
  103. plugins.push(
  104. viteCompression({
  105. ext: '.br',
  106. algorithm: 'brotliCompress',
  107. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE === 'true',
  108. }),
  109. )
  110. }
  111. return plugins
  112. }