import react from "@vitejs/plugin-react"; import { defineConfig } from "vite"; import { fileURLToPath } from "node:url"; import path from "node:path"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); /** * GWT slot apps (ECC reference sample **Agent_self_monitoring**) ship with: * - `index.html` at archive root * - **Relative** script URLs: `static/js/main..js` (no hard-coded `/si/...` prefix) * - `asset-manifest.json`: `{ "main.js": "static/js/main..js" }` * * So production **`base` defaults to `./`**, so bundles resolve next to `index.html` under any * host path (`/si/App/ver/`, `/ameyo40/si/.../`, etc.). * * Set **`VITE_GWT_BASE`** only if you intentionally need absolute URLs (e.g. `/si/MyApp/1.0.0/`). * * `vite dev` uses **`base: '/'`** so `/app/main.tsx` resolves on localhost. */ /** Collapse `/si//gwt//…` and avoid `…/ver//static/…` when Vite joins base + `static/js/…`. */ function normalizeVitePublicBase(raw: string): string { const s = raw.trim(); if (s.startsWith("/")) { return `/${s.split("/").filter(Boolean).join("/")}`; } return s; } function buildBase(): string { const raw = process.env.VITE_GWT_BASE?.trim(); if (raw) { let b = normalizeVitePublicBase(raw); if (!b.endsWith("/")) b = `${b}/`; console.info(`[vite] base (VITE_GWT_BASE): ${b}`); return b; } console.info("[vite] base: ./ (relative — aligned with ECC GWT sample app layout)"); return "./"; } /** ECC / some proxies mis-handle `…/ver//static/js/…` (403). Collapse before `static/js/`. */ function collapseDoubleSlashBeforeStaticJs(html: string): string { let next = html; let prev: string; do { prev = next; next = prev.replace(/\/\/+(static\/js\/)/g, "/$1"); } while (next !== prev); return next; } export default defineConfig(({ command }) => ({ plugins: [ react(), { name: "collapse-double-slash-before-static-js", enforce: "post", transformIndexHtml: { order: "post", handler(html: string) { return collapseDoubleSlashBeforeStaticJs(html); } } } ], base: command === "serve" ? "/" : buildBase(), build: { outDir: "dist", emptyOutDir: true, chunkSizeWarningLimit: 12000, rollupOptions: { output: { entryFileNames: "static/js/main.[hash].js", chunkFileNames: "static/js/[name].[hash].js", assetFileNames: (assetInfo) => { const n = assetInfo.name ?? ""; if (n.endsWith(".css")) return "static/css/[name].[hash][extname]"; return "static/media/[name].[hash][extname]"; } } } } }));