/// import { svelte } from '@sveltejs/vite-plugin-svelte'; import { readFile } from 'node:fs/promises'; import { defaultClientConditions, defineConfig, type Plugin, type ProxyOptions } from 'vite'; import { viteSingleFile } from 'vite-plugin-singlefile'; // A running hud server, which the dev host's MCP calls go to: `HUD_URL`, or // hud's default port; and the origin it is reached at, `HUD_PUBLIC_URL`, // which it takes the workspace's POSTs from. const hud = (process.env.HUD_URL ?? 'http://localhost:8787').replace(/\/$/, ''); const hudOrigin = new URL(process.env.HUD_PUBLIC_URL ?? hud).origin; // `npm run dev`: the dev host at /dev/ (web/dev), which plays an MCP Apps // host, and at /view.html the built view, web/dist/index.html, as it is, // read afresh on each load so a rebuild shows on reload. Its // Content-Security-Policy is the one the dev host built from the UI // resource's metadata, as a host does, passed as `?csp=`. And at // /workspace the workspace (web/workspace), live, its API proxied to HUD, // and at /playground the view playground beside it. function devHost(): Plugin { return { name: 'hud-dev-host', apply: 'serve', configureServer(server) { server.middlewares.use(async (req, res, next) => { const url = new URL(req.url ?? '/', 'http://dev'); if (url.pathname === '/') { res.writeHead(302, { Location: '/dev/' }).end(); return; } if (url.pathname === '/workspace' || url.pathname === '/playground') { req.url = `${url.pathname}.html${url.search}`; next(); return; } if (url.pathname !== '/view.html') { next(); return; } const csp = url.searchParams.get('csp'); if (!csp) { res.writeHead(400, { 'Content-Type': 'text/plain; charset=utf-8' }).end('No ?csp=: the dev host loads the view with the CSP it built.'); return; } let html: string; try { html = await readFile(new URL('dist/index.html', import.meta.url), 'utf8'); } catch { res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' }).end('No dist/index.html: run `npm run build` first.'); return; } res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Security-Policy': csp, 'Cache-Control': 'no-store' }).end(html); }); } }; } // Three documents, each one HTML file with its script and styles inline, // built one at a time (a single-file build has one entry): the view // (dist/index.html), which hud-server embeds and serves as // ui://records/view; with `--mode workspace` the workspace // (dist/workspace.html), which it serves at /workspace, beside it; and // with `--mode playground` the view playground (dist/playground.html), // at /playground. Only the first build empties dist. const inputs: Record = { workspace: 'workspace.html', playground: 'playground.html' }; export default defineConfig(({ mode }) => ({ plugins: [svelte({ compilerOptions: { runes: true } }), viteSingleFile(), devHost()], // HUD's packages (@hud/renderer, @hud/shell, @hud/shared) as their // source, by the condition their exports name it by, not as published // (their dist, which `npm run package` makes). resolve: { conditions: ['@hud/source', ...defaultClientConditions] }, build: { outDir: 'dist', emptyOutDir: !Object.hasOwn(inputs, mode), // Browsers that draw `light-dark()` themselves: an older target has // it rewritten to follow the system's colour scheme alone, so the // view would stay light when the host says dark (its theme sets the // root's `color-scheme`, which `light-dark()` follows). cssTarget: ['chrome123', 'edge123', 'firefox120', 'safari17.5'], rollupOptions: { input: Object.hasOwn(inputs, mode) ? inputs[mode] : 'index.html' } }, server: { // The MCP endpoint, the blob proxy, apps' icons, the sign-in // dialog's logos and the landing's picture, and sign-in: HUD's // OAuth endpoints and its metadata documents; and the workspace's // XRPC methods and web sign-in, whose POSTs HUD takes only from its // own origin, so they go as if from there. proxy: { '/mcp': hud, '/blob': hud, '/icon': hud, '/workspace/atmo': hud, '/workspace/hero': hud, '/oauth': hud, '/terms': hud, '/privacy': hud, '/guidelines': hud, '/.well-known': hud, '/xrpc': { target: hud, configure: sameOrigin }, '/workspace/signin': { target: hud, configure: sameOrigin }, '/workspace/switch': { target: hud, configure: sameOrigin }, '/workspace/signout': { target: hud, configure: sameOrigin } } } })); /** Send a proxied request with HUD's public origin as its `Origin`. */ const sameOrigin: ProxyOptions['configure'] = (proxy) => { proxy.on('proxyReq', (req) => { if (req.getHeader('origin')) req.setHeader('origin', hudOrigin); }); };