import path from "node:path"; import { fileURLToPath } from "node:url"; import tailwindcss from "@tailwindcss/vite"; import react from "@vitejs/plugin-react"; import { defineConfig, loadEnv, type Plugin } from "vite"; const DEFAULT_AGENTS24_BASE_URL = "https://api.agents24.dev"; export function agents24ConnectOrigin(value: string | undefined): string { const candidate = String(value || DEFAULT_AGENTS24_BASE_URL).trim(); let url: URL; try { url = new URL(candidate); } catch { throw new Error("VITE_AGENTS24_BASE_URL must be a valid absolute HTTP(S) URL."); } if (!["http:", "https:"].includes(url.protocol)) { throw new Error("VITE_AGENTS24_BASE_URL must be a valid absolute HTTP(S) URL."); } return url.origin; } function agents24CspPlugin(connectOrigin: string): Plugin { return { name: "agents24-csp", transformIndexHtml(html) { return html.replaceAll("__AGENTS24_CONNECT_SOURCE__", connectOrigin); }, }; } export default defineConfig(({ mode }) => { const environment = loadEnv(mode, process.cwd(), ""); const connectOrigin = agents24ConnectOrigin(environment.VITE_AGENTS24_BASE_URL); return { plugins: [agents24CspPlugin(connectOrigin), react(), tailwindcss()], resolve: { alias: { "@": path.resolve(fileURLToPath(new URL(".", import.meta.url)), "./src") } }, server: { host: "127.0.0.1" }, }; });