@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 108 lines 4.56 KB
Raw
1 import { expect, layer } from "@alchemy.run/test";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as Path from "effect/Path";
6 import * as Bundle from "@/Bundle/Bundle";
7
8 const HEAVY = "HEAVY_DEPLOY_TOOLING_MARKER";
9
10 /**
11 * A temporary package whose `entry.ts` is `source`, next to a `heavy.ts`
12 * standing in for deploy-time tooling (rolldown, Vite, …).
13 */
14 const makeProject = (source: string) =>
15 Effect.gen(function* () {
16 const fs = yield* FileSystem.FileSystem;
17 const path = yield* Path.Path;
18 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-dead-dynamic-" });
19 const entry = path.join(root, "entry.ts");
20 yield* fs.writeFileString(entry, source);
21 yield* fs.writeFileString(
22 path.join(root, "heavy.ts"),
23 `export const heavy = ${JSON.stringify(HEAVY)};\n`,
24 );
25 return { root, entry };
26 });
27
28 const contents = (output: Bundle.BundleOutput) =>
29 output.files.map((file) =>
30 typeof file.content === "string" ? file.content : new TextDecoder().decode(file.content),
31 );
32
33 layer(NodeServices.layer)("Bundle dead dynamic imports", (it) => {
34 it.effect(
35 "drops the chunk of a dynamic import that constant inlining made dead",
36 () =>
37 Effect.gen(function* () {
38 const fs = yield* FileSystem.FileSystem;
39 const path = yield* Path.Path;
40 // The import only dies once `isRuntime` is inlined from another module,
41 // after rolldown has already split `heavy.ts` into its own chunk.
42 const { root, entry } = yield* makeProject(
43 `import { load } from "./lib.ts";\nconsole.log(load());\n`,
44 );
45 yield* fs.writeFileString(path.join(root, "flag.ts"), `export const isRuntime = true;\n`);
46 yield* fs.writeFileString(
47 path.join(root, "lib.ts"),
48 `import { isRuntime } from "./flag.ts";\n` +
49 `export const load = () => (isRuntime ? undefined : import("./heavy.ts"));\n`,
50 );
51 const output = yield* Bundle.build({ input: entry, cwd: root });
52 expect(output.files.map((file) => file.path)).toEqual(["entry.js"]);
53 expect(contents(output).some((code) => code.includes(HEAVY))).toBe(false);
54 }),
55 { tags: ["unit", "local"] },
56 );
57
58 it.effect(
59 "keeps the chunk of a reachable dynamic import",
60 () =>
61 Effect.gen(function* () {
62 const { root, entry } = yield* makeProject(
63 `export const load = () => import("./heavy.ts");\n`,
64 );
65 const output = yield* Bundle.build({ input: entry, cwd: root });
66 expect(contents(output).some((code) => code.includes(HEAVY))).toBe(true);
67 }),
68 { tags: ["unit", "local"] },
69 );
70
71 it.effect(
72 "drops chunks a dead lazy-import cycle keeps alive",
73 () =>
74 Effect.gen(function* () {
75 const fs = yield* FileSystem.FileSystem;
76 const path = yield* Path.Path;
77 // Vite's shape: the tool sets up lazy imports of its parts with
78 // module-level calls, and the parts share a module that imports the
79 // tool back, so rolldown keeps the parts alive although the only path
80 // to the tool is deploy-time code that tree-shaking removes.
81 const { root, entry } = yield* makeProject(
82 `import { deploy } from "./deploy.ts";\n` +
83 `export const runtime = globalThis.__ALCHEMY_RUNTIME__ ? "runtime" : deploy();\n`,
84 );
85 const files: Record<string, string> = {
86 "deploy.ts": `export const deploy = () => import("./tool.ts");\n`,
87 "tool.ts":
88 `export const lazy = (load: () => unknown) => load;\n` +
89 `lazy(() => import("./part-a.ts"));\n` +
90 `lazy(() => import("./part-b.ts"));\n`,
91 "shared.ts":
92 `import { lazy } from "./tool.ts";\n` +
93 `export const shared = lazy(() => ${JSON.stringify(HEAVY)});\n`,
94 "part-a.ts": `import { shared } from "./shared.ts";\nexport default shared;\n`,
95 "part-b.ts": `import { shared } from "./shared.ts";\nexport default shared;\n`,
96 };
97 for (const [name, source] of Object.entries(files)) {
98 yield* fs.writeFileString(path.join(root, name), source);
99 }
100 for (const codeSplitting of [true, false]) {
101 const output = yield* Bundle.build({ input: entry, cwd: root }, { codeSplitting });
102 expect(output.files.map((file) => file.path)).toEqual(["entry.js"]);
103 expect(contents(output).some((code) => code.includes(HEAVY))).toBe(false);
104 }
105 }),
106 { tags: ["unit", "local"] },
107 );
108 });