import { execFileSync } from "node:child_process"; import path from "node:path"; import type { Plugin } from "vite"; /** * Serve only. The dev server answers the runtime's release-manifest request * with scripts/write-dev-release-manifest.ts's output instead of the committed * placeholder, so a feature that `create:feature` just added boots in * `corepack pnpm dev` and in the dev-server browser tests. The manifest is * built once in a child process at server start, because it imports the * installed feature declarations, which this config must not load, and kept * in memory: no file is written for the server's watcher to see. A build never * uses it: the built manifest comes from scripts/generate-build-manifest.ts. */ export function devReleaseManifestPlugin(repositoryRoot = process.cwd()): Plugin { return { name: "frontend-dev-release-manifest", // Vitest also starts Vite in serve mode; tests never fetch the manifest // from a server, so they skip the child process. apply: (_config, env) => env.command === "serve" && !process.env["VITEST"], configureServer(server) { const manifest = execFileSync( process.execPath, [path.join(repositoryRoot, "scripts/write-dev-release-manifest.ts")], { cwd: repositoryRoot, encoding: "utf8", stdio: ["ignore", "pipe", "inherit"] }, ); server.middlewares.use((request, response, next) => { const pathname = new URL(request.url ?? "/", "http://dev.invalid").pathname; if (request.method !== "GET" || !pathname.endsWith("/release-manifest.json")) { next(); return; } response.setHeader("content-type", "application/json"); response.setHeader("cache-control", "no-store"); response.end(manifest); }); }, }; }