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")}`); });