vite.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { defineConfig, loadEnv, type Plugin } from "vite";
  2. import react from "@vitejs/plugin-react";
  3. import fs from "node:fs";
  4. import path from "node:path";
  5. import cesium from "vite-plugin-cesium";
  6. const PUBLIC_EXCLUDE_DIRS = new Set(["b3dm"]);
  7. const copyPublicDir = (
  8. srcDir: string,
  9. destDir: string,
  10. excludeDirs = PUBLIC_EXCLUDE_DIRS,
  11. ) => {
  12. if (!fs.existsSync(srcDir)) return;
  13. for (const entry of fs.readdirSync(srcDir, { withFileTypes: true })) {
  14. if (excludeDirs.has(entry.name)) continue;
  15. const src = path.join(srcDir, entry.name);
  16. const dest = path.join(destDir, entry.name);
  17. if (entry.isDirectory()) {
  18. fs.mkdirSync(dest, { recursive: true });
  19. copyPublicDir(src, dest, excludeDirs);
  20. continue;
  21. }
  22. fs.mkdirSync(path.dirname(dest), { recursive: true });
  23. fs.copyFileSync(src, dest);
  24. }
  25. };
  26. /** 打包时复制 public,但排除 b3dm 等大体积静态资源 */
  27. const copyPublicExcludePlugin = (): Plugin => ({
  28. name: "copy-public-exclude",
  29. apply: "build",
  30. closeBundle() {
  31. const publicDir = path.resolve(process.cwd(), "public");
  32. const outDir = path.resolve(process.cwd(), "dist");
  33. copyPublicDir(publicDir, outDir);
  34. },
  35. });
  36. // https://vite.dev/config/
  37. export default defineConfig(({ mode }) => {
  38. const env = loadEnv(mode, process.cwd(), "");
  39. return {
  40. base: "./",
  41. build: {
  42. copyPublicDir: false,
  43. },
  44. server: {
  45. port: 9020,
  46. open: true,
  47. host: "0.0.0.0",
  48. headers: {
  49. "Access-Control-Allow-Origin": "*", // 允许所有来源,生产环境建议指定具体来源
  50. "Access-Control-Allow-Methods":
  51. "GET, POST, PUT, DELETE, PATCH, OPTIONS",
  52. "Access-Control-Allow-Headers":
  53. "X-Requested-With, content-type, Authorization",
  54. },
  55. proxy: {
  56. "/api": {
  57. target: env.VITE_API_BASE_URL,
  58. changeOrigin: true,
  59. },
  60. "/tdt": {
  61. target: "https://t0.tianditu.gov.cn",
  62. changeOrigin: true,
  63. rewrite: (path) => path.replace(/^\/tdt/, ""),
  64. },
  65. "/gxq-map": {
  66. target:
  67. env.VITE_INTERNAL_IMAGERY_PROXY_TARGET ??
  68. "http://19.50.107.245:8090",
  69. changeOrigin: true,
  70. rewrite: (path) => path.replace(/^\/gxq-map/, ""),
  71. },
  72. },
  73. },
  74. plugins: [
  75. react({ babel: { plugins: ["babel-plugin-react-compiler"] } }),
  76. cesium(),
  77. copyPublicExcludePlugin(),
  78. ],
  79. resolve: {
  80. alias: {
  81. "@/": `${path.resolve(__dirname, "src")}/`,
  82. react: path.resolve(__dirname, "node_modules/react"),
  83. "react-dom": path.resolve(__dirname, "node_modules/react-dom"),
  84. },
  85. dedupe: ["react", "react-dom", "react-router", "react-redux"],
  86. },
  87. optimizeDeps: {
  88. include: [
  89. "react",
  90. "react-dom",
  91. "react-dom/client",
  92. "react/jsx-runtime",
  93. "react/jsx-dev-runtime",
  94. "react/compiler-runtime",
  95. "react-router",
  96. "react-redux",
  97. "cesium",
  98. ],
  99. },
  100. };
  101. });