feat: initialize TJWater WebGIS frontend
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import tailwindcss from "@tailwindcss/vite";
|
||||
import react from "@vitejs/plugin-react-swc";
|
||||
import { defineConfig, loadEnv, type Plugin, type PreviewServer, type ViteDevServer } from "vite";
|
||||
import { fileURLToPath, URL } from "node:url";
|
||||
|
||||
const RUNTIME_CONFIG_PATH = "/runtime-config.js";
|
||||
|
||||
function renderRuntimeConfig(values: Record<string, string>) {
|
||||
const config = {
|
||||
TJWATER_MAPBOX_ACCESS_TOKEN: values.TJWATER_MAPBOX_ACCESS_TOKEN || "",
|
||||
TJWATER_MAP_URL: values.TJWATER_MAP_URL || "https://geoserver.waternetwork.cn/geoserver",
|
||||
TJWATER_GEOSERVER_WORKSPACE: values.TJWATER_GEOSERVER_WORKSPACE || "tjwater",
|
||||
TJWATER_AGENT_API_BASE_URL: values.TJWATER_AGENT_API_BASE_URL || "http://127.0.0.1:8787",
|
||||
TJWATER_ENABLE_DEV_PANEL: values.TJWATER_ENABLE_DEV_PANEL || "false",
|
||||
TJWATER_ENABLE_MSW: values.TJWATER_ENABLE_MSW || "false"
|
||||
};
|
||||
|
||||
return `globalThis.__TJWATER_CONFIG__ = ${JSON.stringify(config)};\n`;
|
||||
}
|
||||
|
||||
function runtimeConfigPlugin(values: Record<string, string>): Plugin {
|
||||
const installMiddleware = (server: ViteDevServer | PreviewServer) => {
|
||||
server.middlewares.use((request, response, next) => {
|
||||
if (request.url?.split("?", 1)[0] !== RUNTIME_CONFIG_PATH) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
response.statusCode = 200;
|
||||
response.setHeader("Content-Type", "application/javascript; charset=utf-8");
|
||||
response.setHeader("Cache-Control", "no-store");
|
||||
response.end(renderRuntimeConfig(values));
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
name: "tjwater-runtime-config",
|
||||
configureServer: installMiddleware,
|
||||
configurePreviewServer: installMiddleware
|
||||
};
|
||||
}
|
||||
|
||||
export default defineConfig(({ mode }) => {
|
||||
const env = loadEnv(mode, process.cwd(), "");
|
||||
const agentBaseUrl =
|
||||
env.AGENT_API_INTERNAL_BASE_URL || env.TJWATER_AGENT_API_BASE_URL || "http://127.0.0.1:8787";
|
||||
|
||||
return {
|
||||
plugins: [runtimeConfigPlugin(env), react(), tailwindcss()],
|
||||
resolve: {
|
||||
alias: {
|
||||
"@": fileURLToPath(new URL("./src", import.meta.url))
|
||||
}
|
||||
},
|
||||
server: {
|
||||
proxy: {
|
||||
"/api/v1/agent/chat": {
|
||||
target: agentBaseUrl,
|
||||
changeOrigin: true
|
||||
}
|
||||
}
|
||||
},
|
||||
test: {
|
||||
environment: "jsdom",
|
||||
globals: true,
|
||||
setupFiles: ["./src/test/setup.ts"]
|
||||
}
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user