Files
clean-architecture-frontend…/vite.service-worker.config.ts
T
DongHyeonkaandClaude Opus 5.5 19e8decf46 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>
2026-09-24 15:42:09 +09:00

99 lines
3.2 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { defineConfig } from "vite";
import {
SERVICE_WORKER_CACHE_SCHEMA_VERSION,
SERVICE_WORKER_PROTOCOL_VERSION,
SERVICE_WORKER_SCRIPT_PATH,
} from "./src/contracts/service-worker.ts";
import { INSTALLED_RUNTIME_CAPABILITIES } from "./src/features/installed-runtime-capabilities.ts";
import { resolveServiceWorkerBuildInput } from "./scripts/lib/service-worker-build-input.ts";
/**
* §17.2.2 step 5. The Service Worker build.
*
* Runs only when the static selection is `ACTIVE`, after the app build has
* completed and the hashed asset list has been generated. `emptyOutDir` is
* false so it adds `service-worker.js` beside the already-built app rather than
* replacing it.
*/
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(new URL(path, import.meta.url).href)) as Record<
string,
unknown
>;
if (!(exportName in module)) {
throw new TypeError(`Generated module does not export ${exportName}.`);
}
return module[exportName] as T;
} catch (error) {
throw new TypeError(`Required generated module is unavailable: ${path}`, {
cause: error,
});
}
}
const assets = await readGenerated(
"./.generated/frontend-runtime/service-worker-assets.ts",
"SERVICE_WORKER_ASSETS",
);
const contractSet = await readGenerated<{ setDigest?: string }>(
"./.generated/frontend-runtime/contract-set.ts",
"BUILD_CONTRACT_SET",
);
const runtimeConfig = JSON.parse(
await readFile("public/config.json", "utf8"),
) as Record<string, string>;
const buildId = process.env.VITE_BUILD_ID ?? "local-build";
const releaseId = process.env.RELEASE_ID ?? "local-release";
const buildInput = resolveServiceWorkerBuildInput({
selection,
assets,
contractSet,
runtimeConfig,
buildId,
releaseId,
});
export default defineConfig({
build: {
// §17.2.2: exactly one input, exactly one output, no additional chunk.
emptyOutDir: false,
sourcemap: false,
rollupOptions: {
input: "src/adapters/service-worker/service-worker-entry.ts",
output: {
format: "es",
inlineDynamicImports: true,
entryFileNames: SERVICE_WORKER_SCRIPT_PATH,
},
},
},
define: {
__CA_SERVICE_WORKER_BUILD_INFO__: JSON.stringify({
serviceWorkerProtocolVersion: SERVICE_WORKER_PROTOCOL_VERSION,
cacheSchemaVersion: SERVICE_WORKER_CACHE_SCHEMA_VERSION,
buildId,
releaseId,
contractSetDigest: buildInput.contractSetDigest,
staticAssetSetDigest: buildInput.assets.setDigest,
}),
__CA_SERVICE_WORKER_ASSETS__: JSON.stringify(buildInput.assets),
__CA_SERVICE_WORKER_HANDLERS__: JSON.stringify(buildInput.handlers),
__CA_RUNTIME_CONFIG_URL__: JSON.stringify(
process.env.VITE_RUNTIME_CONFIG_URL ?? "/config.json",
),
__CA_RELEASE_MANIFEST_URL__: JSON.stringify(
buildInput.releaseManifestUrl,
),
},
});