vite.config.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. name: 'mockjs',
  53. var: 'Mock',
  54. path: 'https://lib.baomitu.com/Mock.js/1.0.1-beta3/mock-min.js'
  55. }
  56. ]
  57. })
  58. ],
  59. resolve: {
  60. alias: {
  61. '@': fileURLToPath(new URL('./src', import.meta.url))
  62. },
  63. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  64. },
  65. build: {
  66. sourcemap: false,
  67. rollupOptions: {
  68. // https://rollupjs.org/guide/en/#outputmanualchunks
  69. output: {
  70. manualChunks(id: string, { getModuleInfo }: any) {
  71. const reg = /(.*)\/src\/components\/(.*)/
  72. if (reg.test(id)) {
  73. const importersLen = getModuleInfo(id)?.importers.length ?? 0
  74. // 被多处引用
  75. if (importersLen > 1) return 'common'
  76. }
  77. if (id.includes('node_modules')) return 'vendor'
  78. if (id.includes('/src/pages/home/Publish.vue')) return 'other'
  79. if (id.includes('/src/pages/home/Music.vue')) return 'other'
  80. if (id.includes('/src/pages/home/MusicRankList.vue')) return 'other'
  81. if (id.includes('/src/pages/home/LivePage.vue')) return 'other'
  82. if (id.includes('/src/pages/home/SearchPage.vue')) return 'other'
  83. if (id.includes('/src/pages/shop/Shop.vue')) return 'other'
  84. if (id.includes('/src/pages/shop/GoodsDetail.vue')) return 'other'
  85. if (id.includes('/src/pages/message/Message.vue')) return 'other'
  86. if (id.includes('/src/pages/message/Fans.vue')) return 'other'
  87. if (id.includes('/src/pages/message/AllMessage.vue')) return 'other'
  88. if (id.includes('/src/pages/message/notice/DouyinHelper.vue')) return 'other'
  89. if (id.includes('/src/pages/message/notice/SystemNotice.vue')) return 'other'
  90. if (id.includes('/src/pages/message/notice/TaskNotice.vue')) return 'other'
  91. if (id.includes('/src/pages/message/notice/LiveNotice.vue')) return 'other'
  92. if (id.includes('/src/pages/message/notice/MoneyNotice.vue')) return 'other'
  93. if (id.includes('/src/pages/me/Me.vue')) return 'other'
  94. if (id.includes('/src/pages/me/Visitors.vue')) return 'other'
  95. if (id.includes('/src/pages/me/RequestUpdate.vue')) return 'other'
  96. if (id.includes('/src/pages/me/userinfo/EditUserInfo.vue')) return 'other'
  97. if (id.includes('/src/pages/me/userinfo/EditUserInfoItem.vue')) return 'other'
  98. if (id.includes('/src/pages/me/MyMusic.vue')) return 'other'
  99. if (id.includes('/src/pages/other/VideoDetail.vue')) return 'other'
  100. if (id.includes('/src/pages/other/AlbumDetail.vue')) return 'other'
  101. if (id.includes('/src/pages/people/FindAcquaintance.vue')) return 'other'
  102. if (id.includes('/src/pages/people/FollowAndFans.vue')) return 'other'
  103. },
  104. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  105. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  106. assetFileNames: 'assets/[name]-[hash].[ext]' // 资源文件像 字体,图片等
  107. }
  108. },
  109. assetsInlineLimit: 2048
  110. },
  111. define: {
  112. LATEST_COMMIT_HASH: JSON.stringify(
  113. latestCommitHash + (process.env.NODE_ENV === 'production' ? '' : ' (dev)')
  114. )
  115. },
  116. esbuild: {
  117. // drop: ['console', 'debugger']
  118. },
  119. server: {
  120. port: 3000,
  121. open: true,
  122. host: '0.0.0.0',
  123. fs: {
  124. strict: false
  125. },
  126. proxy: {
  127. '/api': {
  128. target: 'http://23.248.196.186',
  129. changeOrigin: true,
  130. rewrite: (path) => path.replace(/^\/api/, '')
  131. }
  132. }
  133. },
  134. preview: {
  135. port: 5555
  136. }
  137. })
  138. })
  139. })
  140. })