vite.config.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // 统一的后端服务地址
  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. ];
  42. // 动态生成代理配置
  43. const proxyConfig: Record<string, any> = {};
  44. apiPaths.forEach(path => {
  45. proxyConfig[path] = {
  46. target: API_BASE_URL,
  47. changeOrigin: true,
  48. rewrite: (p: string) => p.replace(new RegExp(`^${path}`), path)
  49. };
  50. });
  51. return {
  52. base: VITE_PUBLIC_PATH,
  53. root,
  54. resolve: {
  55. alias
  56. },
  57. // 服务端渲染
  58. server: {
  59. port: 8888,
  60. host: "0.0.0.0",
  61. proxy: proxyConfig,
  62. // 预热文件以提前转换和缓存结果,降低启动期间的初始页面加载时长并防止转换瀑布
  63. warmup: {
  64. clientFiles: ["./index.html", "./src/{views,components}/*"]
  65. }
  66. },
  67. plugins: getPluginsList(VITE_CDN, VITE_COMPRESSION),
  68. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  69. optimizeDeps: {
  70. include,
  71. exclude
  72. },
  73. build: {
  74. // https://cn.vitejs.dev/guide/build.html#browser-compatibility
  75. target: "es2015",
  76. sourcemap: false,
  77. // 消除打包大小超过500kb警告
  78. chunkSizeWarningLimit: 4000,
  79. rollupOptions: {
  80. input: {
  81. index: pathResolve("./index.html", import.meta.url)
  82. },
  83. // 静态资源分类打包
  84. output: {
  85. chunkFileNames: "static/js/[name]-[hash].js",
  86. entryFileNames: "static/js/[name]-[hash].js",
  87. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  88. }
  89. }
  90. },
  91. define: {
  92. __INTLIFY_PROD_DEVTOOLS__: false,
  93. __APP_INFO__: JSON.stringify(__APP_INFO__)
  94. }
  95. };
  96. };