bundle-boundary.test.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { fileURLToPath } from "node:url";
  2. import { build } from "esbuild";
  3. import { describe, expect, it } from "vitest";
  4. const packageRoot = fileURLToPath(new URL("..", import.meta.url));
  5. const MAX_BROWSER_CLIENT_BUNDLE_BYTES = 150_000;
  6. function normalizePath(value: string): string {
  7. return value.replaceAll("\\", "/");
  8. }
  9. describe("browser-client bundle boundary", () => {
  10. it("只打包 Browser runtime,不带入 Agent Core", async () => {
  11. const result = await build({
  12. absWorkingDir: packageRoot,
  13. bundle: true,
  14. entryPoints: ["src/browser-client.ts"],
  15. format: "esm",
  16. legalComments: "none",
  17. metafile: true,
  18. platform: "node",
  19. target: "node24",
  20. write: false,
  21. });
  22. const output = result.outputFiles[0];
  23. expect(output).toBeDefined();
  24. expect(output!.contents.byteLength).toBeLessThanOrEqual(MAX_BROWSER_CLIENT_BUNDLE_BYTES);
  25. const inputs = Object.keys(result.metafile.inputs).map(normalizePath);
  26. const coreInputs = inputs.filter((path) => /(?:^|\/)core\/dist\//u.test(path));
  27. expect(coreInputs.length).toBeGreaterThan(0);
  28. expect(coreInputs.every((path) => /(?:^|\/)core\/dist\/browser-client\//u.test(path))).toBe(
  29. true,
  30. );
  31. const contractInputs = inputs.filter((path) => /(?:^|\/)contracts\/dist\//u.test(path));
  32. expect(contractInputs).toEqual([
  33. expect.stringMatching(/contracts\/dist\/interfaces\/browser-control\.port\.js$/u),
  34. ]);
  35. // Bug 原因:根 barrel 中的顶层注册有副作用,tree-shaking 无法去掉这些业务模块。
  36. expect(inputs).not.toEqual(
  37. expect.arrayContaining([
  38. expect.stringMatching(/(?:^|\/)core\/dist\/(?:agent|runtime|subagent|tool|workflow)\//u),
  39. ]),
  40. );
  41. });
  42. });