vite.config.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { defineConfig, PluginOption, UserConfig } from 'vite'
  2. import Vue from '@vitejs/plugin-vue'
  3. import VueJsx from '@vitejs/plugin-vue-jsx'
  4. import { visualizer } from 'rollup-plugin-visualizer'
  5. import { Plugin as importToCDN } from 'vite-plugin-cdn-import'
  6. import { fileURLToPath, URL } from 'node:url'
  7. import { getLastCommit } from 'git-last-commit'
  8. import VueMacros from 'unplugin-vue-macros/vite'
  9. const lifecycle = process.env.npm_lifecycle_event
  10. export default defineConfig((): Promise<UserConfig> => {
  11. let latestCommitHash = ''
  12. return new Promise((resolve) => {
  13. getLastCommit((err, commit) => {
  14. if (!err) {
  15. latestCommitHash = commit.shortHash
  16. }
  17. resolve({
  18. base: './',
  19. envDir: 'env',
  20. plugins: [
  21. VueMacros({
  22. plugins: {
  23. vue: Vue(),
  24. vueJsx: VueJsx() // if needed
  25. }
  26. // betterDefine: true,
  27. // reactivityTransform: {
  28. // exclude: [/node_modules/, /jQuery\.js/]
  29. // }
  30. }),
  31. // Vue(),
  32. // VueJsx(),
  33. lifecycle === 'report' ? (visualizer({ open: false }) as any as PluginOption) : null,
  34. importToCDN({
  35. modules: [
  36. {
  37. name: 'vue',
  38. var: 'Vue',
  39. path: `https://lib.baomitu.com/vue/3.4.21/vue.runtime.global.prod.min.js`
  40. },
  41. {
  42. name: 'vue-router',
  43. var: 'VueRouter',
  44. path: 'https://lib.baomitu.com/vue-router/4.3.0/vue-router.global.prod.min.js'
  45. },
  46. {
  47. name: 'vue-demi',
  48. var: 'VueDemi',
  49. path: 'https://lib.baomitu.com/vue-demi/0.14.7/index.iife.min.js'
  50. }
  51. ]
  52. })
  53. // viteCompression({
  54. // verbose: false,
  55. // disable: false,
  56. // threshold: 10240,
  57. // algorithm: 'brotliCompress',
  58. // }),
  59. // viteCompression({
  60. // verbose: false,
  61. // disable: false,
  62. // algorithm: 'gzip',
  63. // threshold: 10240,
  64. // }),
  65. // viteImagemin({
  66. // gifsicle: {
  67. // optimizationLevel: 7,
  68. // interlaced: false,
  69. // },
  70. // optipng: {
  71. // optimizationLevel: 7,
  72. // },
  73. // mozjpeg: {
  74. // quality: 20,
  75. // },
  76. // pngquant: {
  77. // quality: [0.8, 0.9],
  78. // speed: 4,
  79. // },
  80. // svgo: {
  81. // plugins: [
  82. // {
  83. // name: 'removeViewBox',
  84. // },
  85. // {
  86. // name: 'removeEmptyAttrs',
  87. // active: false,
  88. // },
  89. // ],
  90. // },
  91. // }),
  92. ],
  93. resolve: {
  94. alias: {
  95. '@': fileURLToPath(new URL('./src', import.meta.url))
  96. },
  97. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  98. },
  99. build: {
  100. sourcemap: false,
  101. rollupOptions: {
  102. // https://rollupjs.org/guide/en/#outputmanualchunks
  103. output: {
  104. manualChunks(id: string, { getModuleInfo }: any) {
  105. const reg = /(.*)\/src\/components\/(.*)/
  106. if (reg.test(id)) {
  107. const importersLen = getModuleInfo(id)?.importers.length ?? 0
  108. // 被多处引用
  109. if (importersLen > 1) return 'common'
  110. }
  111. if (id.includes('node_modules')) return 'vendor'
  112. if (id.includes('/src/pages/home/LivePage.vue')) return 'other'
  113. if (id.includes('/src/pages/search/PasswordLogin.vue')) return 'other'
  114. },
  115. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  116. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  117. assetFileNames: 'assets/[name]-[hash].[ext]' // 资源文件像 字体,图片等
  118. }
  119. },
  120. assetsInlineLimit: 2048
  121. },
  122. define: {
  123. LATEST_COMMIT_HASH: JSON.stringify(
  124. latestCommitHash + (process.env.NODE_ENV === 'production' ? '' : ' (dev)')
  125. )
  126. },
  127. esbuild: {
  128. // drop: ['console', 'debugger']
  129. },
  130. server: {
  131. port: 3000,
  132. open: false,
  133. host: '0.0.0.0',
  134. fs: {
  135. strict: false
  136. },
  137. proxy: {
  138. '/appapi': {
  139. target: 'http://admin.zbo458v5.top',
  140. changeOrigin: true,
  141. // rewrite: (path) => path.replace(/^\/api/, '')
  142. }
  143. }
  144. },
  145. preview: {
  146. port: 5555
  147. }
  148. })
  149. })
  150. })
  151. })