vite.config_20260429203922.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { defineConfig, loadEnv } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import path from 'path'
  4. import { fileURLToPath } from 'url'
  5. import vueDevTools from 'vite-plugin-vue-devtools'
  6. import viteCompression from 'vite-plugin-compression'
  7. import Components from 'unplugin-vue-components/vite'
  8. import AutoImport from 'unplugin-auto-import/vite'
  9. import ElementPlus from 'unplugin-element-plus/vite'
  10. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  11. import tailwindcss from '@tailwindcss/vite'
  12. // import { visualizer } from 'rollup-plugin-visualizer'
  13. export default ({ mode }: { mode: string }) => {
  14. const root = process.cwd()
  15. const env = loadEnv(mode, root)
  16. const { VITE_VERSION, VITE_PORT, VITE_BASE_URL, VITE_API_URL, VITE_API_PROXY_URL } = env
  17. console.log(`🚀 API_URL = ${VITE_API_URL}`)
  18. console.log(`🚀 VERSION = ${VITE_VERSION}`)
  19. return defineConfig({
  20. define: {
  21. __APP_VERSION__: JSON.stringify(VITE_VERSION)
  22. },
  23. base: VITE_BASE_URL,
  24. server: {
  25. port: Number(VITE_PORT),
  26. proxy: {
  27. '/api': {
  28. target: VITE_API_PROXY_URL,
  29. changeOrigin: true
  30. },
  31. // Web端接口代理 - 转发到后端 /api 前缀
  32. '/web': {
  33. target: 'http://192.168.2.178:8000/api',
  34. changeOrigin: true,
  35. rewrite: (path) => `/api${path}`
  36. },
  37. // 小程序端接口代理
  38. '/mini': {
  39. target: 'http://192.168.2.178:8000/api',
  40. changeOrigin: true,
  41. rewrite: (path) => `/api${path}`
  42. }
  43. },
  44. host: true
  45. },
  46. // 路径别名
  47. resolve: {
  48. alias: {
  49. '@': fileURLToPath(new URL('./src', import.meta.url)),
  50. '@views': resolvePath('src/views'),
  51. '@imgs': resolvePath('src/assets/images'),
  52. '@icons': resolvePath('src/assets/icons'),
  53. '@utils': resolvePath('src/utils'),
  54. '@stores': resolvePath('src/store'),
  55. '@styles': resolvePath('src/assets/styles')
  56. }
  57. },
  58. build: {
  59. target: 'es2015',
  60. outDir: 'dist',
  61. chunkSizeWarningLimit: 2000,
  62. minify: 'terser',
  63. terserOptions: {
  64. compress: {
  65. // 生产环境去除 console
  66. drop_console: true,
  67. // 生产环境去除 debugger
  68. drop_debugger: true
  69. }
  70. },
  71. dynamicImportVarsOptions: {
  72. warnOnError: true,
  73. exclude: [],
  74. include: ['src/views/**/*.vue']
  75. }
  76. },
  77. plugins: [
  78. vue(),
  79. tailwindcss(),
  80. // 自动按需导入 API
  81. AutoImport({
  82. imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
  83. dts: 'src/types/import/auto-imports.d.ts',
  84. resolvers: [ElementPlusResolver()],
  85. eslintrc: {
  86. enabled: true,
  87. filepath: './.auto-import.json',
  88. globalsPropValue: true
  89. }
  90. }),
  91. // 自动按需导入组件
  92. Components({
  93. dts: 'src/types/import/components.d.ts',
  94. resolvers: [ElementPlusResolver()]
  95. }),
  96. // 按需定制主题配置
  97. ElementPlus({
  98. useSource: true
  99. }),
  100. // 压缩
  101. viteCompression({
  102. verbose: false, // 是否在控制台输出压缩结果
  103. disable: false, // 是否禁用
  104. algorithm: 'gzip', // 压缩算法
  105. ext: '.gz', // 压缩后的文件名后缀
  106. threshold: 10240, // 只有大小大于该值的资源会被处理 10240B = 10KB
  107. deleteOriginFile: false // 压缩后是否删除原文件
  108. }),
  109. vueDevTools()
  110. // 打包分析
  111. // visualizer({
  112. // open: true,
  113. // gzipSize: true,
  114. // brotliSize: true,
  115. // filename: 'dist/stats.html' // 分析图生成的文件名及路径
  116. // }),
  117. ],
  118. // 依赖预构建:避免运行时重复请求与转换,提升首次加载速度
  119. optimizeDeps: {
  120. include: [
  121. 'echarts/core',
  122. 'echarts/charts',
  123. 'echarts/components',
  124. 'echarts/renderers',
  125. 'xlsx',
  126. 'xgplayer',
  127. 'crypto-js',
  128. 'file-saver',
  129. 'vue-img-cutter',
  130. 'element-plus/es',
  131. 'element-plus/es/components/*/style/css',
  132. 'element-plus/es/components/*/style/index'
  133. ]
  134. },
  135. css: {
  136. preprocessorOptions: {
  137. // sass variable and mixin
  138. scss: {
  139. additionalData: `
  140. @use "@styles/core/el-light.scss" as *;
  141. @use "@styles/core/mixin.scss" as *;
  142. `
  143. }
  144. },
  145. postcss: {
  146. plugins: [
  147. {
  148. postcssPlugin: 'internal:charset-removal',
  149. AtRule: {
  150. charset: (atRule) => {
  151. if (atRule.name === 'charset') {
  152. atRule.remove()
  153. }
  154. }
  155. }
  156. }
  157. ]
  158. }
  159. }
  160. })
  161. }
  162. function resolvePath(paths: string) {
  163. return path.resolve(__dirname, paths)
  164. }