| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import { defineConfig, loadEnv, type Plugin } from "vite";
- import react from "@vitejs/plugin-react";
- import fs from "node:fs";
- import path from "node:path";
- import cesium from "vite-plugin-cesium";
- const PUBLIC_EXCLUDE_DIRS = new Set(["b3dm"]);
- const copyPublicDir = (
- srcDir: string,
- destDir: string,
- excludeDirs = PUBLIC_EXCLUDE_DIRS,
- ) => {
- if (!fs.existsSync(srcDir)) return;
- for (const entry of fs.readdirSync(srcDir, { withFileTypes: true })) {
- if (excludeDirs.has(entry.name)) continue;
- const src = path.join(srcDir, entry.name);
- const dest = path.join(destDir, entry.name);
- if (entry.isDirectory()) {
- fs.mkdirSync(dest, { recursive: true });
- copyPublicDir(src, dest, excludeDirs);
- continue;
- }
- fs.mkdirSync(path.dirname(dest), { recursive: true });
- fs.copyFileSync(src, dest);
- }
- };
- /** 打包时复制 public,但排除 b3dm 等大体积静态资源 */
- const copyPublicExcludePlugin = (): Plugin => ({
- name: "copy-public-exclude",
- apply: "build",
- closeBundle() {
- const publicDir = path.resolve(process.cwd(), "public");
- const outDir = path.resolve(process.cwd(), "dist");
- copyPublicDir(publicDir, outDir);
- },
- });
- // https://vite.dev/config/
- export default defineConfig(({ mode }) => {
- const env = loadEnv(mode, process.cwd(), "");
- return {
- base: "./",
- build: {
- copyPublicDir: false,
- },
- server: {
- port: 9020,
- open: true,
- host: "0.0.0.0",
- headers: {
- "Access-Control-Allow-Origin": "*", // 允许所有来源,生产环境建议指定具体来源
- "Access-Control-Allow-Methods":
- "GET, POST, PUT, DELETE, PATCH, OPTIONS",
- "Access-Control-Allow-Headers":
- "X-Requested-With, content-type, Authorization",
- },
- proxy: {
- "/api": {
- target: env.VITE_API_BASE_URL,
- changeOrigin: true,
- },
- "/tdt": {
- target: "https://t0.tianditu.gov.cn",
- changeOrigin: true,
- rewrite: (path) => path.replace(/^\/tdt/, ""),
- },
- "/gxq-map": {
- target:
- env.VITE_INTERNAL_IMAGERY_PROXY_TARGET ??
- "http://19.50.107.245:8090",
- changeOrigin: true,
- rewrite: (path) => path.replace(/^\/gxq-map/, ""),
- },
- },
- },
- plugins: [
- react({ babel: { plugins: ["babel-plugin-react-compiler"] } }),
- cesium(),
- copyPublicExcludePlugin(),
- ],
- resolve: {
- alias: {
- "@/": `${path.resolve(__dirname, "src")}/`,
- react: path.resolve(__dirname, "node_modules/react"),
- "react-dom": path.resolve(__dirname, "node_modules/react-dom"),
- },
- dedupe: ["react", "react-dom", "react-router", "react-redux"],
- },
- optimizeDeps: {
- include: [
- "react",
- "react-dom",
- "react-dom/client",
- "react/jsx-runtime",
- "react/jsx-dev-runtime",
- "react/compiler-runtime",
- "react-router",
- "react-redux",
- "cesium",
- ],
- },
- };
- });
|