import { execFileSync } from "node:child_process"; import { readFileSync } from "node:fs"; import path from "node:path"; import type { Plugin } from "vite"; /** * Generated at dev server start, beside the build's generated contract set and * ignored with it. Not under src/: the removal drills scan src/ for the * sample's names, and this copies the placeholder's route chunks. */ export const DEV_RELEASE_MANIFEST_PATH = ".generated/frontend-runtime/dev-release-manifest.json"; /** * 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 * written in a child process because it imports the installed feature * declarations, which this config must not load. 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) { execFileSync( process.execPath, [path.join(repositoryRoot, "scripts/write-dev-release-manifest.ts")], { cwd: repositoryRoot, stdio: "inherit" }, ); const manifest = path.join(repositoryRoot, DEV_RELEASE_MANIFEST_PATH); 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(readFileSync(manifest)); }); }, }; }