diff --git a/src/features/tech-log/presentation/tech-log-route-runtime.tsx b/src/features/tech-log/presentation/tech-log-route-runtime.tsx index 344bc96..fea021f 100644 --- a/src/features/tech-log/presentation/tech-log-route-runtime.tsx +++ b/src/features/tech-log/presentation/tech-log-route-runtime.tsx @@ -1,17 +1,45 @@ import { lazy, + Suspense, type ComponentType, type LazyExoticComponent, type ReactNode, } from "react"; import { Outlet } from "react-router-dom"; +import { ChunkRecoveryBoundary } from "../../../presentation/boundaries/chunk-recovery-boundary.tsx"; +import { LoadingSurface } from "../../../presentation/design-system/index.ts"; +import { useApplicationRecovery } from "../../../presentation/providers/application-provider.tsx"; import { TECH_LOG_ROUTE_RUNTIME_CONTRACT, type TechLogRouteId, } from "../contracts/tech-log-route-contract.ts"; import { PublicShell } from "./public/public-shell.tsx"; -import { StudioShell } from "./studio/studio-shell.tsx"; + +/* + Studio 셸은 공개 화면을 읽는 사람에게 필요 없다. 정적으로 가져오면 셸이 끌고 오는 + StudioProvider·Studio 머리말·문서 server state 가 모든 첫 화면의 초기 JS 에 들어갔다 + (FE-GATE-012). /studio 에 들어갈 때 받는다. 라우트 청크와 같이 청크를 못 받으면 + ChunkRecoveryBoundary 가 한 번 새로 읽기를 정한다. +*/ +const StudioShell = lazy(async () => { + const module = await import("./studio/studio-shell.tsx"); + return { default: module.StudioShell }; +}); + +function DeferredStudioShell({ children }: Readonly<{ children: ReactNode }>) { + const recovery = useApplicationRecovery(); + return ( + + }> + {children} + + + ); +} type RouteRuntime = Readonly<{ moduleId: string; @@ -297,8 +325,8 @@ export const TECH_LOG_ROUTE_LAYOUTS: Readonly< ), STUDIO: ( - + - + ), }); diff --git a/tests/features/tech-log/boot-graph.test.ts b/tests/features/tech-log/boot-graph.test.ts new file mode 100644 index 0000000..f25cf8c --- /dev/null +++ b/tests/features/tech-log/boot-graph.test.ts @@ -0,0 +1,117 @@ +import assert from "node:assert/strict"; +import { existsSync, readFileSync } from "node:fs"; +import path from "node:path"; + +import { parseAsync } from "@babel/core"; +import { test } from "vitest"; + +/** + * FE-GATE-012. 첫 화면의 초기 JS 는 진입점(main.tsx), 그 정적 import, 진입점이 바로 + * 동적으로 읽는 boot·render-boot-failure 와 그 정적 import 다 + * (`scripts/lib/classify-vite-bundle.ts`). 공개 화면을 읽는 사람에게 필요 없는 아래 코드가 + * 정적 import 하나로 이 그래프에 다시 들어오면 번들 예산을 넘는다. 빌드 없이 여기서 먼저 잡는다. + */ +const BOOT_SEEDS = [ + "src/bootstrap/main.tsx", + "src/bootstrap/boot.tsx", + "src/bootstrap/render-boot-failure.tsx", +]; + +const DEFERRED = [ + // MOCK 소스: 런타임 문서가 MOCK 을 고른 빌드(로컬·시험)에서만 쓰인다. + "src/features/tech-log/adapters/mock/", + "src/features/tech-log/adapters/static/", + // Studio: /studio 에 들어갈 때 받는다. + "src/features/tech-log/presentation/studio/", + "src/features/tech-log/adapters/http/http-studio-gateways.ts", + "src/features/tech-log/adapters/http/http-studio-gateway.ts", + "src/features/tech-log/adapters/http/http-management-gateway.ts", + "src/features/tech-log/adapters/http/http-studio-asset-gateway.ts", + "src/features/tech-log/adapters/http/asset-upload-transport.ts", +]; + +type Node = Readonly & { type: string }>; + +function isTypeOnly(statement: Node): boolean { + if (statement["importKind"] === "type" || statement["exportKind"] === "type") return true; + const specifiers = statement["specifiers"] as readonly Node[] | undefined; + if (statement.type !== "ImportDeclaration" || !specifiers || specifiers.length === 0) { + return false; + } + // `import { type A, type B } from "x"` 는 컴파일 뒤 남지 않는다(verbatimModuleSyntax 없음). + return specifiers.every((specifier) => specifier["importKind"] === "type"); +} + +function resolveRelative(from: string, specifier: string): string | undefined { + if (!specifier.startsWith(".")) return undefined; + const target = path.posix.normalize(path.posix.join(path.posix.dirname(from), specifier)); + if (!/\.tsx?$/u.test(target)) return undefined; + assert.ok(existsSync(target), `${from} 가 없는 파일을 가져온다: ${specifier}`); + return target; +} + +async function staticImports(file: string): Promise { + const plugins: ("jsx" | ["typescript", { dts: boolean }])[] = [["typescript", { dts: false }]]; + if (file.endsWith(".tsx")) plugins.push("jsx"); + const tree = (await parseAsync(readFileSync(file, "utf8"), { + filename: file, + babelrc: false, + configFile: false, + sourceType: "module", + parserOpts: { plugins }, + })) as unknown as { program: { body: readonly Node[] } } | null; + assert.ok(tree, `cannot parse ${file}`); + const imports: string[] = []; + for (const statement of tree.program.body) { + if ( + statement.type !== "ImportDeclaration" && + statement.type !== "ExportNamedDeclaration" && + statement.type !== "ExportAllDeclaration" + ) { + continue; + } + const source = statement["source"] as Node | null | undefined; + if (!source || isTypeOnly(statement)) continue; + const resolved = resolveRelative(file, String(source["value"])); + if (resolved) imports.push(resolved); + } + return imports; +} + +async function bootGraph(): Promise> { + const importedBy = new Map(BOOT_SEEDS.map((seed) => [seed, "(seed)"])); + const pending = [...BOOT_SEEDS]; + while (pending.length > 0) { + const file = pending.pop()!; + for (const imported of await staticImports(file)) { + if (importedBy.has(imported)) continue; + importedBy.set(imported, file); + pending.push(imported); + } + } + return importedBy; +} + +function chain(importedBy: ReadonlyMap, file: string): string { + const links = [file]; + let current = importedBy.get(file); + while (current && current !== "(seed)") { + links.push(current); + current = importedBy.get(current); + } + return links.join(" <- "); +} + +test("공개 첫 화면의 부팅 그래프에 MOCK 소스와 Studio 코드가 없다", async () => { + const graph = await bootGraph(); + + // 그래프를 제대로 따라갔는지: 공개 셸과 기능 입력 조립은 부팅 때 필요하다. + assert.ok(graph.has("src/features/tech-log/presentation/public/public-shell.tsx")); + assert.ok(graph.has("src/features/tech-log/adapters/create-tech-log-feature-input.ts")); + + const leaked = [...graph.keys()] + .filter((file) => DEFERRED.some((prefix) => file.startsWith(prefix))) + .sort() + .map((file) => chain(graph, file)); + assert.deepEqual(leaked, [], `부팅 그래프로 들어온 지연 코드:\n${leaked.join("\n")}`); +});