diff --git a/scripts/lib/dev-release-manifest.ts b/scripts/lib/dev-release-manifest.ts index 0395cd6a..d9acd8cf 100644 --- a/scripts/lib/dev-release-manifest.ts +++ b/scripts/lib/dev-release-manifest.ts @@ -1,24 +1,17 @@ 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. + * 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 { @@ -27,12 +20,11 @@ export function devReleaseManifestPlugin(repositoryRoot = process.cwd()): Plugin // from a server, so they skip the child process. apply: (_config, env) => env.command === "serve" && !process.env["VITEST"], configureServer(server) { - execFileSync( + const manifest = execFileSync( process.execPath, [path.join(repositoryRoot, "scripts/write-dev-release-manifest.ts")], - { cwd: repositoryRoot, stdio: "inherit" }, + { cwd: repositoryRoot, encoding: "utf8", stdio: ["ignore", "pipe", "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")) { @@ -41,7 +33,7 @@ export function devReleaseManifestPlugin(repositoryRoot = process.cwd()): Plugin } response.setHeader("content-type", "application/json"); response.setHeader("cache-control", "no-store"); - response.end(readFileSync(manifest)); + response.end(manifest); }); }, }; diff --git a/scripts/write-dev-release-manifest.ts b/scripts/write-dev-release-manifest.ts index 48a6c75a..a96e28da 100644 --- a/scripts/write-dev-release-manifest.ts +++ b/scripts/write-dev-release-manifest.ts @@ -1,27 +1,24 @@ -import { mkdir, readFile, writeFile } from "node:fs/promises"; -import { dirname } from "node:path"; +import { readFile } from "node:fs/promises"; import { buildContractSet } from "./generate-contract-set.ts"; -import { DEV_RELEASE_MANIFEST_PATH } from "./lib/dev-release-manifest.ts"; /** * The release manifest the dev server serves: the committed placeholder * (public/release-manifest.json) with the contract set of the features * installed now. Boot checks that set against the installed declarations, so * a placeholder with a fixed set refused every app with one more feature - * (CONTRACT_SET_PACKAGE_MISSING) until someone edited it by hand. The output - * is generated and ignored, as .generated/ is. + * (CONTRACT_SET_PACKAGE_MISSING) until someone edited it by hand. */ -export async function writeDevReleaseManifest( +export async function devReleaseManifest( source = "public/release-manifest.json", - output = DEV_RELEASE_MANIFEST_PATH, -): Promise { +): Promise { const manifest = JSON.parse(await readFile(source, "utf8")) as Record; manifest["contractSet"] = buildContractSet(); - await mkdir(dirname(output), { recursive: true }); - await writeFile(output, `${JSON.stringify(manifest, null, 2)}\n`, "utf8"); + return `${JSON.stringify(manifest, null, 2)}\n`; } +// Printed, not written: a file the dev server's watcher picks up after the +// server started could reload a page while it is booting. if (process.argv[1]?.endsWith("write-dev-release-manifest.ts")) { - await writeDevReleaseManifest(); + process.stdout.write(await devReleaseManifest()); } diff --git a/tests/unit/dev-release-manifest.test.ts b/tests/unit/dev-release-manifest.test.ts index 9eaee1d9..a0b72ff6 100644 --- a/tests/unit/dev-release-manifest.test.ts +++ b/tests/unit/dev-release-manifest.test.ts @@ -5,7 +5,7 @@ import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { buildContractSet } from "../../scripts/generate-contract-set.ts"; -import { writeDevReleaseManifest } from "../../scripts/write-dev-release-manifest.ts"; +import { devReleaseManifest } from "../../scripts/write-dev-release-manifest.ts"; /** * 1103 X2. `create:feature` declares a contract package; the dev server used @@ -22,7 +22,6 @@ describe("dev release manifest", () => { it("serves the placeholder with the contract set of the installed features", async () => { directory = await mkdtemp(path.join(tmpdir(), "dev-release-manifest-")); const source = path.join(directory, "release-manifest.json"); - const output = path.join(directory, "generated", "dev-release-manifest.json"); await writeFile( source, JSON.stringify({ @@ -36,22 +35,15 @@ describe("dev release manifest", () => { }), ); - await writeDevReleaseManifest(source, output); - - const written = JSON.parse(await readFile(output, "utf8")) as Record; + const written = JSON.parse(await devReleaseManifest(source)) as Record; expect(written["buildId"]).toBe("local-build"); expect(written["contractSet"]).toEqual(JSON.parse(JSON.stringify(buildContractSet()))); }); it("keeps every other field of the committed placeholder", async () => { - directory = await mkdtemp(path.join(tmpdir(), "dev-release-manifest-")); - const output = path.join(directory, "dev-release-manifest.json"); - - await writeDevReleaseManifest("public/release-manifest.json", output); - // Whatever features are installed (a product's, or a drill's generated // one): only the contract set may differ from the placeholder. - const { contractSet, ...written } = JSON.parse(await readFile(output, "utf8")) as Record< + const { contractSet, ...written } = JSON.parse(await devReleaseManifest()) as Record< string, unknown >;