vite.config.ts 2.5 KB

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