vitePlugins.config.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }),
  41. // vue devTools
  42. VITE_DEVTOOLS === 'true' && VueDevTools({ launchEditor: 'code' }),
  43. // 创建打包压缩配置
  44. createCompression(viteEnv),
  45. // 查看配置信息
  46. // {
  47. // name: 'debug-config',
  48. // configResolved(config) {
  49. // console.log('🚀 Vite Final Config:', config)
  50. // },
  51. // },
  52. // 查看中间件信息
  53. {
  54. name: 'inspect-middleware',
  55. configureServer(server) {
  56. console.log('🔍 Middlewares:')
  57. server.middlewares.stack.forEach((layer, i) => {
  58. const route = layer.route || '(root)'
  59. const name = 'name' in layer.handle ? layer.handle['name'] : '(anonymous)'
  60. const handleType = typeof layer.handle
  61. console.log(`${i + 1}. [${route}] → ${name} (${handleType})`)
  62. })
  63. },
  64. },
  65. // 中间件监控
  66. // {
  67. // name: 'middleware-tracker',
  68. // configureServer(server) {
  69. // const stack = server.middlewares.stack
  70. // stack.forEach((layer, i) => {
  71. // const originalHandle = layer.handle
  72. // const name = originalHandle.name || `(anonymous-${i})`
  73. // const handleInfo = originalHandle.toString()
  74. // layer.handle = function wrappedHandle(req, res, next) {
  75. // console.log(`🔥 Middleware [${name}] triggered by: ${req.method} ${req.url}`)
  76. // // if (!originalHandle.name) console.log(`handleInfo: ${handleInfo}`)
  77. // return originalHandle(req, res, next)
  78. // }
  79. // })
  80. // },
  81. // },
  82. ]
  83. }
  84. /**
  85. * @description 根据 compress 配置,生成不同的压缩规则
  86. * @param ImportMetaEnv
  87. */
  88. const createCompression = (viteEnv: ImportMetaEnv): PluginOption | PluginOption[] => {
  89. const { VITE_BUILD_COMPRESS = 'none', VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE } = viteEnv
  90. const compressList = VITE_BUILD_COMPRESS.split(',')
  91. const plugins: PluginOption[] = []
  92. if (compressList.includes('gzip')) {
  93. plugins.push(
  94. viteCompression({
  95. ext: '.gz',
  96. algorithm: 'gzip',
  97. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE === 'true',
  98. }),
  99. )
  100. }
  101. if (compressList.includes('brotli')) {
  102. plugins.push(
  103. viteCompression({
  104. ext: '.br',
  105. algorithm: 'brotliCompress',
  106. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE === 'true',
  107. }),
  108. )
  109. }
  110. return plugins
  111. }