projections-barrel.test.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { readFileSync } from "node:fs";
  2. import { dirname, join, resolve } from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. import { describe, expect, it } from "vitest";
  5. // `@zcode/dynamic-workflow/projections` 是给作品集浏览器包用的纯投影桶
  6. // (docs/chat/workflow-portfolio.md):站内在冻结的 core.json 上现场重算投影,整条链不能把
  7. // `typescript`(分析器铸造 core 时才用的编译器)拖进前端 bundle。这里不靠打包器报错来发现——
  8. // 直接静态遍历 dist/projections.js 的 ESM import 闭包(tsc 已经擦掉了 type-only import,
  9. // 所以 dist 里剩下的每条 `from "…"` 都是运行时依赖),断言可达文件里没有一条 import 裸
  10. // 说明符 "typescript"。任何一次把投影模块接到 causality-order.ts 之类带编译器的模块上的
  11. // 改动,都会在这里第一时间炸出来,并列出泄漏的文件。
  12. //
  13. // 依赖 dist:turbo 的 test 任务 dependsOn build;在包目录里裸跑 `pnpm test` 前请先
  14. // `pnpm run build`,否则这里检查的是上一次构建的产物。
  15. const distDir = resolve(dirname(fileURLToPath(import.meta.url)), "..", "dist");
  16. const ENTRY = join(distDir, "projections.js");
  17. /** 每条静态 import / re-export 的说明符(含 `export … from`),side-effect import 也算。 */
  18. const SPECIFIER =
  19. /(?:^|\n)\s*(?:import|export)\b[^;]*?\bfrom\s*["']([^"']+)["']|(?:^|\n)\s*import\s*["']([^"']+)["']/g;
  20. function specifiersOf(file: string): string[] {
  21. const source = readFileSync(file, "utf8");
  22. const out: string[] = [];
  23. for (const match of source.matchAll(SPECIFIER)) out.push((match[1] ?? match[2]) as string);
  24. return out;
  25. }
  26. /** 可达的 dist 文件与其中出现的裸(非相对)说明符,按发现文件归类。 */
  27. function walk(entry: string): { files: string[]; bare: Map<string, string[]> } {
  28. const files: string[] = [];
  29. const bare = new Map<string, string[]>();
  30. const queue = [entry];
  31. const seen = new Set<string>();
  32. while (queue.length > 0) {
  33. const file = queue.shift() as string;
  34. if (seen.has(file)) continue;
  35. seen.add(file);
  36. files.push(file);
  37. for (const spec of specifiersOf(file)) {
  38. if (spec.startsWith(".") || spec.startsWith("/")) {
  39. queue.push(resolve(dirname(file), spec));
  40. } else {
  41. const list = bare.get(spec) ?? [];
  42. list.push(file);
  43. bare.set(spec, list);
  44. }
  45. }
  46. }
  47. return { bare, files };
  48. }
  49. describe("projections barrel is browser-safe", () => {
  50. it("no reachable dist file imports typescript (or any other bare specifier)", () => {
  51. const { bare, files } = walk(ENTRY);
  52. // 桶本身加上它再导出的投影模块——闭包不能退化成只有入口文件。
  53. expect(files.length).toBeGreaterThan(5);
  54. const leaks = [...bare.entries()].map(
  55. ([spec, importers]) => `${spec} <- ${importers.join(", ")}`,
  56. );
  57. expect(leaks, "bare imports reachable from the browser barrel").toEqual([]);
  58. expect(bare.has("typescript")).toBe(false);
  59. // 反向校验:走同一套遍历,根导出必然到达 typescript——否则说明遍历本身失效。
  60. expect(walk(join(distDir, "index.js")).bare.has("typescript")).toBe(true);
  61. });
  62. it("does not reach the analyzer front-end modules", () => {
  63. const { files } = walk(ENTRY);
  64. const names = files.map((file) => file.slice(distDir.length + 1));
  65. for (const forbidden of [
  66. "analysis/causality-order.js",
  67. "analysis/analyze.js",
  68. "compiler/compile.js",
  69. "analysis/domain.js",
  70. ]) {
  71. expect(names, `${forbidden} must not be reachable`).not.toContain(forbidden);
  72. }
  73. });
  74. });