vite.config.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { getPluginsList } from "./build/plugins";
  2. import { include, exclude } from "./build/optimize";
  3. import { type UserConfigExport, type ConfigEnv, loadEnv } from "vite";
  4. import {
  5. root,
  6. alias,
  7. wrapperEnv,
  8. pathResolve,
  9. __APP_INFO__
  10. } from "./build/utils";
  11. export default ({ mode }: ConfigEnv): UserConfigExport => {
  12. const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
  13. wrapperEnv(loadEnv(mode, root));
  14. return {
  15. base: VITE_PUBLIC_PATH,
  16. root,
  17. resolve: {
  18. alias
  19. },
  20. // 服务端渲染
  21. server: {
  22. port: VITE_PORT,
  23. host: "0.0.0.0",
  24. proxy: {
  25. "/login": {
  26. target: "http://localhost:8080/admin",
  27. changeOrigin: true,
  28. rewrite: path => path.replace(/^\/login/, "/login")
  29. },
  30. "/user": {
  31. target: "http://localhost:8080/admin",
  32. changeOrigin: true,
  33. rewrite: path => path.replace(/^\/user/, "/user")
  34. },
  35. "/role": {
  36. target: "http://localhost:8080/admin",
  37. changeOrigin: true,
  38. rewrite: path => path.replace(/^\/role/, "/role")
  39. },
  40. "/permission": {
  41. target: "http://localhost:8080/admin",
  42. changeOrigin: true,
  43. rewrite: path => path.replace(/^\/permission/, "/permission")
  44. },
  45. "/users": {
  46. target: "http://localhost:8080/admin",
  47. changeOrigin: true,
  48. rewrite: path => path.replace(/^\/users/, "/users")
  49. },
  50. "/shops": {
  51. target: "http://localhost:8080/admin",
  52. changeOrigin: true,
  53. rewrite: path => path.replace(/^\/shops/, "/shops")
  54. },
  55. "/devices": {
  56. target: "http://localhost:8080/admin",
  57. changeOrigin: true,
  58. rewrite: path => path.replace(/^\/devices/, "/devices")
  59. },
  60. "/order": {
  61. target: "http://localhost:8080/admin",
  62. changeOrigin: true,
  63. rewrite: path => path.replace(/^\/order/, "/order")
  64. },
  65. "/products": {
  66. target: "http://localhost:8080/admin",
  67. changeOrigin: true,
  68. rewrite: path => path.replace(/^\/products/, "/products")
  69. },
  70. "/inventory": {
  71. target: "http://localhost:8080/admin",
  72. changeOrigin: true,
  73. rewrite: path => path.replace(/^\/inventory/, "/inventory")
  74. },
  75. "/marketing": {
  76. target: "http://localhost:8080/admin",
  77. changeOrigin: true,
  78. rewrite: path => path.replace(/^\/marketing/, "/marketing")
  79. },
  80. "/checkin": {
  81. target: "http://localhost:8080/admin",
  82. changeOrigin: true,
  83. rewrite: path => path.replace(/^\/checkin/, "/checkin")
  84. },
  85. "/announcement": {
  86. target: "http://localhost:8080/admin",
  87. changeOrigin: true,
  88. rewrite: path => path.replace(/^\/announcement/, "/announcement")
  89. },
  90. "/operation-log": {
  91. target: "http://localhost:8080/admin",
  92. changeOrigin: true,
  93. rewrite: path => path.replace(/^\/operation-log/, "/operation-log")
  94. },
  95. "/sync": {
  96. target: "http://localhost:8080/admin",
  97. changeOrigin: true,
  98. rewrite: path => path.replace(/^\/sync/, "/sync")
  99. },
  100. "/new-product-apply": {
  101. target: "http://localhost:8080/admin",
  102. changeOrigin: true,
  103. rewrite: path => path.replace(/^\/new-product-apply/, "/new-product-apply")
  104. },
  105. "/dashboard": {
  106. target: "http://localhost:8080/admin",
  107. changeOrigin: true,
  108. rewrite: path => path.replace(/^\/dashboard/, "/dashboard")
  109. },
  110. "/dict": {
  111. target: "http://localhost:8080/admin",
  112. changeOrigin: true,
  113. rewrite: path => path.replace(/^\/dict/, "/dict")
  114. },
  115. "/timed-discount": {
  116. target: "http://localhost:8080/admin",
  117. changeOrigin: true,
  118. rewrite: path => path.replace(/^\/timed-discount/, "/timed-discount")
  119. }
  120. },
  121. // 预热文件以提前转换和缓存结果,降低启动期间的初始页面加载时长并防止转换瀑布
  122. warmup: {
  123. clientFiles: ["./index.html", "./src/{views,components}/*"]
  124. }
  125. },
  126. plugins: getPluginsList(VITE_CDN, VITE_COMPRESSION),
  127. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  128. optimizeDeps: {
  129. include,
  130. exclude
  131. },
  132. build: {
  133. // https://cn.vitejs.dev/guide/build.html#browser-compatibility
  134. target: "es2015",
  135. sourcemap: false,
  136. // 消除打包大小超过500kb警告
  137. chunkSizeWarningLimit: 4000,
  138. rollupOptions: {
  139. input: {
  140. index: pathResolve("./index.html", import.meta.url)
  141. },
  142. // 静态资源分类打包
  143. output: {
  144. chunkFileNames: "static/js/[name]-[hash].js",
  145. entryFileNames: "static/js/[name]-[hash].js",
  146. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  147. }
  148. }
  149. },
  150. define: {
  151. __INTLIFY_PROD_DEVTOOLS__: false,
  152. __APP_INFO__: JSON.stringify(__APP_INFO__)
  153. }
  154. };
  155. };