문제: SW를 ACTIVE로 켜면 build-frontend 6단계의 SW Vite 빌드가 Vite 기본값대로 public/을 dist에 다시 복사한다. 4단계가 프로필로 만든 dist/config.json이 public/config.json(local, demo 인증, http://localhost:8080/, 루트 release manifest)으로 덮어써진다. 빌드와 verify-release는 통과하고 release admission만 거절한다. 그래서 SW를 켠 제품은 local이 아닌 릴리스를 만들 수 없다. VITE_ROUTER_BASE_PATH를 쓰면 RELEASE_MANIFEST_URL도 base를 잃는다. 고른 방법: vite.service-worker.config.ts에 publicDir: false를 넣는다. dist에는 3단계 앱 빌드가 이미 public/을 복사해 두었고, 워커 번들은 public/ 파일을 쓰지 않는다. 같은 repo의 다른 두 번째 Vite 빌드 (scripts/lib/optional-recipe-bundle.ts)도 같은 설정이다. build-frontend.ts 6단계 주석에 이 조건을 한 줄 적었다. 버린 대안: - build.copyPublicDir: false. 복사만 끄고 public 디렉터리는 계속 설정에 남는다. 워커 빌드는 public/을 쓸 일이 없어 publicDir: false가 의도를 바로 말한다. - 앱 빌드 plugin의 closeBundle에서 워커를 만들기, 또는 Vite build()를 configFile: false로 부르는 스크립트. 둘 다 리뷰 5.2절의 대안이다. two-pass 구조를 바꾸거나 파일이 늘어 이 결함에 비해 범위가 크다. 테스트: tests/unit/service-worker-build-output.test.ts는 임시 복사본에서 SW ACTIVE + APP_PROFILE=production + VITE_ROUTER_BASE_PATH=/app/로 실제 build-frontend.ts를 돌린다. 그리고 dist/config.json이 generateRuntimeConfig("production", env) 결과와 같은지 본다(리뷰의 완료 기준). 설정 객체에서 publicDir === false를 보는 테스트는 수정을 다시 적을 뿐이고, 설정이 5단계 생성물 없이는 로드되지 않아 버렸다. 빌드 1회가 약 2초라 unit 계층에 둔다(build-environment-bundle.test.ts와 같은 방식, docs/testing/taxonomy.md의 짧은 child process 규칙). 복사와 빌드는 e2e 도우미 prepareFixtureBuilds를 재사용하고, 빌드별 env 옵션만 추가했다(기존 호출은 그대로). 이 테스트로 0925/06-10(SW Vite 설정의 dist/config.json 읽기 테스트 없음)도 닫힌다. 위험·한계: 하위 경로에서 사전 캐시 URL이 base를 잃는 문제(0929/06-02)와 워커의 runtime config URL 기본값은 그대로다. 이 테스트는 /app/ 빌드의 설정 문서만 보고 워커 설치는 보지 않는다. Vite 8.1.5 동작 기준이다. check:architecture는 pnpm 스크립트로만 돌아서 실행하지 않았다(src는 바꾸지 않았다). 검증: - RED(수정 전): 새 테스트 실패. APP_ENV local(기대 production), AUTH_MODE demo(기대 external), API_BASE_URL http://localhost:8080/, RELEASE_MANIFEST_URL /release-manifest.json(기대 /app/...) - GREEN: 새 테스트 통과(1.9초) - 수정 전후 production + /app/ 빌드 비교: dist 파일 34개로 같음. 수정 후 RELEASE_TARGET=production check-release-admission ADMITTED, 수정 전 rc 1 - node scripts/run-vitest.ts run tests/unit(pnpm을 띄우는 2파일 제외): 164파일 2343건 통과 - tsc tsconfig.test.json, tsconfig.node.json 통과. 바꾼 4파일 eslint 통과 Refs: docs/reviews/2026-11-01-maturity-review/06-adapters-service-worker-push.md 2절, 13-build-release-supply-chain.md 실험 E-B
106 lines
3.6 KiB
TypeScript
106 lines
3.6 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",
|
|
);
|
|
// 06-10 / F-8. The profile document `scripts/generate-runtime-config.ts` wrote
|
|
// in step 4, not `public/config.json`, which is always the local profile.
|
|
const runtimeConfig = JSON.parse(
|
|
await readFile("dist/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,
|
|
appProfile: process.env.APP_PROFILE ?? "local",
|
|
buildId,
|
|
releaseId,
|
|
});
|
|
|
|
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,
|
|
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,
|
|
),
|
|
},
|
|
});
|