From 19e8decf46b1c56ead33cc068d2804161c7816b3 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 15:37:31 +0900 Subject: [PATCH] fix(build): resolve worker config generated inputs from the config file (R3-6) vite.service-worker.config.ts loaded .generated/frontend-runtime/*.ts with a bare relative import(). Vite's default `bundle` config loader runs the config from node_modules/.vite-temp/, so with the ACTIVE selection `corepack pnpm build` stopped at the worker step. Resolve the paths against import.meta.url, which every loader keeps pointing at the config file. The e2e helper no longer patches build-frontend.ts with `--configLoader native`; the quarantined fixme is replaced by a check that both unmodified ACTIVE builds emitted service-worker.js. RED (throwaway copy, ACTIVE selection, unmodified scripts/build-frontend.ts): TypeError: Required generated module is unavailable: ./.generated/frontend-runtime/service-worker-assets.ts [cause]: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '.../node_modules/.vite-temp/.generated/frontend-runtime/service-worker-assets.ts' build step failed: npx vite build --config vite.service-worker.config.ts build-frontend exit=1 GREEN: same copy after the fix -> build-frontend exit=0, dist/service-worker.js emitted; `pnpm test:e2e --project=chromium tests/e2e/service-worker-update.spec.ts` 5 passed. Co-Authored-By: Claude Opus 5.5 --- tests/e2e/service-worker-update.spec.ts | 22 ++++++++-------------- tests/e2e/support/service-worker-builds.ts | 19 +------------------ vite.service-worker.config.ts | 8 +++++++- 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/tests/e2e/service-worker-update.spec.ts b/tests/e2e/service-worker-update.spec.ts index b0dab043..6906d97c 100644 --- a/tests/e2e/service-worker-update.spec.ts +++ b/tests/e2e/service-worker-update.spec.ts @@ -21,8 +21,7 @@ import { * copy of the repo, builds it twice (`VITE_BUILD_ID` sw-e2e-a / sw-e2e-b), * and serves either dist from its own port per test. The copy also records * the worker host's `onUpdateWaiting` / `onReloadRequired` callbacks, which - * the app itself does not subscribe to yet. See that file for the build-script - * workaround (product defect found here). + * the app itself does not subscribe to yet. * * The preview server of this suite is not used; each test uses absolute URLs. */ @@ -247,16 +246,11 @@ test("serves A's removed chunk from the previous cache once B controls the page" } }); -// quarantine(owner=frontend-platform, defect=R3-6-service-worker-config-loader, expires=2026-12-31) -// `vite.service-worker.config.ts` loads `.generated/frontend-runtime/*.ts` -// with a runtime `import("./.generated/...")`. Vite's default `bundle` config -// loader executes the config from `node_modules/.vite-temp/`, so the path -// resolves there and the ACTIVE build stops with ERR_MODULE_NOT_FOUND at -// `npx vite build --config vite.service-worker.config.ts`. Remove the fixme -// (and the helper's `configLoaderWorkaround`) once the build script works. -test.fixme("builds the ACTIVE worker with the repo's unmodified build script", async () => { - test.setTimeout(180_000); - await removeServiceWorkerBuilds( - await prepareServiceWorkerBuilds({ configLoaderWorkaround: false }), - ); +// `beforeAll` already ran `scripts/build-frontend.ts` unmodified (default +// `bundle` config loader); this pins that both builds emitted the worker. +test("builds the ACTIVE worker with the repo's unmodified build script", async () => { + const { dist } = requireBuilds(); + for (const root of [dist.a, dist.b]) { + expect(await readdir(root)).toContain("service-worker.js"); + } }); diff --git a/tests/e2e/support/service-worker-builds.ts b/tests/e2e/support/service-worker-builds.ts index be431514..ab591f46 100644 --- a/tests/e2e/support/service-worker-builds.ts +++ b/tests/e2e/support/service-worker-builds.ts @@ -104,17 +104,7 @@ function run(cwd: string, script: string, buildId: string): void { } } -/** - * `configLoaderWorkaround` (default on) works around a product defect found - * by this spec: `vite.service-worker.config.ts` imports its generated inputs - * with a runtime-relative `import("./.generated/...")`, and Vite's default - * `bundle` config loader runs the config from `node_modules/.vite-temp/`, so - * the ACTIVE worker build fails with ERR_MODULE_NOT_FOUND. The copy's - * `build-frontend.ts` is given `--configLoader native` for the worker step. - */ -export async function prepareServiceWorkerBuilds( - { configLoaderWorkaround = true } = {}, -): Promise { +export async function prepareServiceWorkerBuilds(): Promise { const root = await mkdtemp(path.join(tmpdir(), "ca-sw-builds-")); await cp(repoRoot, root, { recursive: true, @@ -127,13 +117,6 @@ export async function prepareServiceWorkerBuilds( "const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;", ACTIVE_SELECTION, ); - if (configLoaderWorkaround) { - await replaceOnce( - path.join(root, "scripts/build-frontend.ts"), - '"vite.service-worker.config.ts"]', - '"vite.service-worker.config.ts", "--configLoader", "native"]', - ); - } const hostFile = path.join(root, "src/bootstrap/register-service-worker.ts"); await replaceOnce( hostFile, diff --git a/vite.service-worker.config.ts b/vite.service-worker.config.ts index 31b0455d..28eeb96e 100644 --- a/vite.service-worker.config.ts +++ b/vite.service-worker.config.ts @@ -20,9 +20,15 @@ import { resolveServiceWorkerBuildInput } from "./scripts/lib/service-worker-bui const selection = INSTALLED_RUNTIME_CAPABILITIES.serviceWorker; +// Relative to this file: Vite's default `bundle` config loader runs the config +// from `node_modules/.vite-temp/`, so a bare `import("./.generated/...")` would +// resolve there. Every loader keeps `import.meta.url` pointing at this file. async function readGenerated(path: string, exportName: string): Promise { try { - const module = (await import(path)) as Record; + const module = (await import(new URL(path, import.meta.url).href)) as Record< + string, + unknown + >; if (!(exportName in module)) { throw new TypeError(`Generated module does not export ${exportName}.`); }