diff --git a/scripts/build-frontend.ts b/scripts/build-frontend.ts index d77ba74b..61f759f1 100644 --- a/scripts/build-frontend.ts +++ b/scripts/build-frontend.ts @@ -18,7 +18,8 @@ import { SELF_UNREGISTERING_SERVICE_WORKER } from "./lib/self-unregistering-serv * 3. Vite app build (emptyOutDir = true) * 4. materialize dist/config.json from the declared APP_PROFILE * 5. scan app dist and generate the static asset source - * 6. ACTIVE only: Vite Service Worker build (emptyOutDir = false) + * 6. ACTIVE only: Vite Service Worker build (emptyOutDir = false, + * publicDir = false: it must not copy `public/` over step 4, 0929/06-01) * REMOVE_REGISTRATION only: write the self-unregistering worker (06-03) * 7. generate Release Manifest V2 and the build manifest * 8. make every dist file readable by other users (13-02) diff --git a/tests/e2e/support/fixture-builds.ts b/tests/e2e/support/fixture-builds.ts index a29238a6..7e1350ea 100644 --- a/tests/e2e/support/fixture-builds.ts +++ b/tests/e2e/support/fixture-builds.ts @@ -29,6 +29,8 @@ const skipped = new Set([ export type FixtureBuild = Readonly<{ name: Name; edit?: (root: string) => Promise; + /** Extra build variables for this build only (e.g. `APP_PROFILE`). */ + env?: Readonly>; }>; export type FixtureBuilds = Readonly<{ @@ -49,11 +51,16 @@ export async function editOnce( await writeFile(file, source.replace(search, replacement)); } -function run(cwd: string, script: string, buildId: string): void { +function run( + cwd: string, + script: string, + buildId: string, + env: Readonly> = {}, +): void { const result = spawnSync("node", [script], { cwd, encoding: "utf8", - env: { ...process.env, VITE_BUILD_ID: buildId, RELEASE_ID: buildId }, + env: { ...process.env, ...env, VITE_BUILD_ID: buildId, RELEASE_ID: buildId }, }); if (result.status !== 0) { throw new Error( @@ -78,8 +85,8 @@ export async function prepareFixtureBuilds( for (const build of builds) { await build.edit?.(root); const buildId = `${prefix}-${build.name}`; - run(root, "scripts/generate-product-feature-index.ts", buildId); - run(root, "scripts/build-frontend.ts", buildId); + run(root, "scripts/generate-product-feature-index.ts", buildId, build.env); + run(root, "scripts/build-frontend.ts", buildId, build.env); dist[build.name] = path.join(root, `dist-${build.name}`); await rename(path.join(root, "dist"), dist[build.name]); } diff --git a/tests/unit/service-worker-build-output.test.ts b/tests/unit/service-worker-build-output.test.ts new file mode 100644 index 00000000..b82a33fe --- /dev/null +++ b/tests/unit/service-worker-build-output.test.ts @@ -0,0 +1,71 @@ +import { access, readFile } from "node:fs/promises"; +import path from "node:path"; + +import { afterAll, describe, expect, it } from "vitest"; + +import { generateRuntimeConfig } from "../../scripts/generate-runtime-config.ts"; +import { + editOnce, + type FixtureBuilds, + prepareFixtureBuilds, + removeFixtureBuilds, +} from "../e2e/support/fixture-builds.ts"; +import { ACTIVE_SELECTION } from "../e2e/support/service-worker-builds.ts"; + +/** + * 0929/06-01. Step 6 of `scripts/build-frontend.ts`, the Service Worker Vite + * build, must not change the app output. Vite copies `public/` into `outDir` + * unless the config turns it off, so the worker build replaced the profile + * `dist/config.json` from step 4 with `public/config.json` (local, demo auth, + * loopback API, root release manifest). The build and `verify-release` passed; + * only release admission refused it. + * + * The defect is in how two Vite runs share `dist`, not in one function, so this + * runs the real build over a temp copy with the worker on (one build, ~2s). + * A check of `publicDir === false` on the config object would only restate the + * fix, and the config cannot load without the inputs step 5 generates. + */ +const PROFILE = Object.freeze({ + APP_PROFILE: "production", + VITE_ROUTER_BASE_PATH: "/app/", +}); +// prepareFixtureBuilds sets VITE_BUILD_ID and RELEASE_ID to `-`. +const BUILD_ID = "sw-profile-active"; + +let builds: FixtureBuilds<"active"> | undefined; + +afterAll(async () => { + await removeFixtureBuilds(builds); +}); + +describe("Service Worker build output (0929/06-01)", () => { + it("keeps the profile runtime config step 4 wrote", async () => { + builds = await prepareFixtureBuilds("sw-profile", [ + { + name: "active", + env: PROFILE, + edit: (root) => + editOnce( + path.join(root, "src/features/installed-runtime-capabilities.ts"), + "const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;", + ACTIVE_SELECTION, + ), + }, + ]); + const dist = builds.dist.active; + // The worker build really ran; otherwise nothing below tests step 6. + await access(path.join(dist, "service-worker.js")); + + const shipped: unknown = JSON.parse( + await readFile(path.join(dist, "config.json"), "utf8"), + ); + expect(shipped).toEqual( + await generateRuntimeConfig("production", { + ...process.env, + ...PROFILE, + VITE_BUILD_ID: BUILD_ID, + RELEASE_ID: BUILD_ID, + }), + ); + }, 180_000); +}); diff --git a/vite.service-worker.config.ts b/vite.service-worker.config.ts index 9ecce64b..2477b752 100644 --- a/vite.service-worker.config.ts +++ b/vite.service-worker.config.ts @@ -67,6 +67,10 @@ const buildInput = resolveServiceWorkerBuildInput({ }); export default defineConfig({ + // 0929/06-01. `dist` already holds the app build, `public/` included. Copying + // `public/` again would put the local `public/config.json` back over the + // profile document step 4 wrote. + publicDir: false, build: { // ยง17.2.2: exactly one input, exactly one output, no additional chunk. emptyOutDir: false,