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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
122a3a8b71
commit
19e8decf46
@@ -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");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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<ServiceWorkerBuilds> {
|
||||
export async function prepareServiceWorkerBuilds(): Promise<ServiceWorkerBuilds> {
|
||||
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,
|
||||
|
||||
@@ -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<T>(path: string, exportName: string): Promise<T> {
|
||||
try {
|
||||
const module = (await import(path)) as Record<string, unknown>;
|
||||
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}.`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user