vite.config.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 async ({ mode }: ConfigEnv): Promise<UserConfigExport> => {
  12. const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
  13. wrapperEnv(loadEnv(mode, root));
  14. // 统一的后端服务地址
  15. const API_BASE_URL = "http://localhost:7070/admin";
  16. // 需要代理的 API 路径列表
  17. const apiPaths = [
  18. "/login",
  19. "/user",
  20. "/role",
  21. "/permission",
  22. "/users",
  23. "/shops",
  24. "/devices",
  25. "/order",
  26. "/products",
  27. "/inventory",
  28. "/marketing",
  29. "/checkin",
  30. "/announcement",
  31. "/operation-log",
  32. "/sync",
  33. "/new-product-apply",
  34. "/dashboard",
  35. "/dict",
  36. "/timed-discount",
  37. "/layer-templates",
  38. "/distribution",
  39. "/refund",
  40. "/replenishers",
  41. "/menu",
  42. "/upload",
  43. "/replenishment-orders",
  44. "/erp-goods",
  45. "/erp-shop"
  46. ];
  47. // 动态生成代理配置
  48. const proxyConfig: Record<string, any> = {};
  49. apiPaths.forEach(path => {
  50. proxyConfig[path] = {
  51. target: API_BASE_URL,
  52. changeOrigin: true,
  53. rewrite: (p: string) => p.replace(new RegExp(`^${path}`), path),
  54. bypass: (req: any) => {
  55. if (req.headers?.accept?.includes('text/html')) {
  56. return '/index.html';
  57. }
  58. }
  59. };
  60. });
  61. return {
  62. base: VITE_PUBLIC_PATH,
  63. root,
  64. resolve: {
  65. alias
  66. },
  67. server: {
  68. port: VITE_PORT,
  69. host: "0.0.0.0",
  70. proxy: proxyConfig,
  71. warmup: {
  72. clientFiles: ["./index.html", "./src/{views,components}/*"]
  73. }
  74. },
  75. plugins: await getPluginsList(VITE_CDN, VITE_COMPRESSION),
  76. optimizeDeps: {
  77. include,
  78. exclude
  79. },
  80. build: {
  81. target: "es2015",
  82. sourcemap: false,
  83. chunkSizeWarningLimit: 4000,
  84. rolldownOptions: {
  85. input: {
  86. index: pathResolve("./index.html", import.meta.url)
  87. },
  88. output: {
  89. chunkFileNames: "static/js/[name]-[hash].js",
  90. entryFileNames: "static/js/[name]-[hash].js",
  91. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  92. },
  93. checks: {
  94. pluginTimings: false,
  95. toleratedTransform: false
  96. }
  97. }
  98. },
  99. define: {
  100. __INTLIFY_PROD_DEVTOOLS__: false,
  101. __APP_INFO__: JSON.stringify(__APP_INFO__)
  102. }
  103. };
  104. };