vite.config.ts 2.4 KB

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