import os from "node:os"; import { resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import tailwindcss from "@tailwindcss/vite"; const portValue = Number.parseInt(process.env.PORT ?? "", 10); const devPort = Number.isFinite(portValue) && portValue > 0 ? portValue : 5173; const allowedHosts = new Set(); const envAllowedHosts = process.env.VITE_ALLOWED_HOSTS ?? ""; const addHost = (value?: string | null) => { const trimmed = value?.trim(); if (!trimmed) return; allowedHosts.add(trimmed); }; envAllowedHosts.split(",").forEach(addHost); addHost(process.env.OPENWORK_PUBLIC_HOST ?? null); const hostname = os.hostname(); addHost(hostname); const shortHostname = hostname.split(".")[0]; if (shortHostname && shortHostname !== hostname) { addHost(shortHostname); } const appRoot = resolve(fileURLToPath(new URL(".", import.meta.url))); // Electron packaged builds load index.html via `file://`, so asset URLs // must be relative. Tauri serves via its own protocol so absolute paths // work there. Gate on an env var the electron build script sets. const isElectronPackagedBuild = process.env.OPENWORK_ELECTRON_BUILD === "1"; export default defineConfig({ base: isElectronPackagedBuild ? "./" : "/", plugins: [ { name: "openwork-dev-server-id", configureServer(server) { server.middlewares.use("/__openwork_dev_server_id", (_req, res) => { res.setHeader("Content-Type", "application/json"); res.end(JSON.stringify({ appRoot })); }); }, }, tailwindcss(), react(), ], server: { port: devPort, strictPort: true, ...(allowedHosts.size > 0 ? { allowedHosts: Array.from(allowedHosts) } : {}), }, build: { target: "esnext", }, });