The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 5 | import type * as Scope from "effect/Scope"; |
| 6 | import { describe, expect, it } from "vitest"; |
| 7 | import { runBuildChild } from "../../core/BuildChild.ts"; |
| 8 | |
| 9 | const run = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | Scope.Scope>) => |
| 10 | Effect.runPromise(Effect.scoped(effect).pipe(Effect.provide(NodeServices.layer))); |
| 11 | |
| 12 | /** |
| 13 | * Copy `fixtures/vinext` into a scratch directory inside it, so the build |
| 14 | * resolves the fixture's own `node_modules`. |
| 15 | */ |
| 16 | const fixture = (options: { readonly prerender: boolean }) => |
| 17 | Effect.gen(function* () { |
| 18 | const fs = yield* FileSystem.FileSystem; |
| 19 | const path = yield* Path.Path; |
| 20 | const source = yield* path.fromFileUrl(new URL("../../../fixtures/vinext/", import.meta.url)); |
| 21 | const root = yield* fs.makeTempDirectoryScoped({ directory: source, prefix: ".vinext-test-" }); |
| 22 | for (const entry of ["app", "worker", "package.json", "vite.config.ts"]) { |
| 23 | yield* fs.copy(path.join(source, entry), path.join(root, entry)); |
| 24 | } |
| 25 | if (options.prerender) { |
| 26 | const config = path.join(root, "vite.config.ts"); |
| 27 | yield* fs.writeFileString( |
| 28 | config, |
| 29 | (yield* fs.readFileString(config)).replace("vinext()", "vinext({ prerender: true })"), |
| 30 | ); |
| 31 | } |
| 32 | return root; |
| 33 | }); |
| 34 | |
| 35 | const build = (root: string, main: string) => |
| 36 | runBuildChild({ |
| 37 | module: new URL("../source.ts", import.meta.url).href, |
| 38 | rootDir: root, |
| 39 | framework: "vinext", |
| 40 | config: { |
| 41 | rootDir: root, |
| 42 | main, |
| 43 | compatibilityDate: "2026-08-31", |
| 44 | compatibilityFlags: ["nodejs_compat"], |
| 45 | }, |
| 46 | }); |
| 47 | |
| 48 | describe("vinext Cloudflare build", () => { |
| 49 | it( |
| 50 | "uploads the Worker main, not vinext's own rsc entry, as the entry module", |
| 51 | () => |
| 52 | run( |
| 53 | Effect.gen(function* () { |
| 54 | const root = yield* fixture({ prerender: false }); |
| 55 | const output = yield* build(root, "worker/server.ts"); |
| 56 | const names = output.serverModules?.map((module) => module.name) ?? []; |
| 57 | // The Worker main builds as `index`, the entry chunk name of |
| 58 | // `@cloudflare/vite-plugin`, in place of the `index` entry vinext's |
| 59 | // rsc environment declares: the one top-level entry uploaded first |
| 60 | // is the main (`worker/server.ts`, its default export a `fetch` |
| 61 | // wrapper), and vinext's app entry is bundled into its chunks. |
| 62 | const entries = names.filter( |
| 63 | (name) => |
| 64 | /^dist\/server\/[^/]+\.js$/.test(name) && !/__|vinext-client-assets/.test(name), |
| 65 | ); |
| 66 | expect(entries).toEqual(["dist/server/index.js"]); |
| 67 | expect(names[0]).toBe("dist/server/index.js"); |
| 68 | // (Source maps are on by default, as with `@cloudflare/vite-plugin`.) |
| 69 | expect( |
| 70 | String(output.serverModules?.[0]?.content).replace( |
| 71 | /\n\/\/# sourceMappingURL=.*\n?$/, |
| 72 | "", |
| 73 | ), |
| 74 | ).toMatch( |
| 75 | /\{fetch\(\w+,\w+,\w+\)\{return \w+\.fetch\(\w+,\w+,\w+\)\}\};export\{\w+ as default\};$/, |
| 76 | ); |
| 77 | }), |
| 78 | ), |
| 79 | 300_000, |
| 80 | ); |
| 81 | |
| 82 | it( |
| 83 | "collects modules vinext writes after the bundle, imported from non-entry chunks", |
| 84 | () => |
| 85 | run( |
| 86 | Effect.gen(function* () { |
| 87 | const fs = yield* FileSystem.FileSystem; |
| 88 | const path = yield* Path.Path; |
| 89 | const root = yield* fixture({ prerender: false }); |
| 90 | const output = yield* build(root, "worker/server.ts"); |
| 91 | const manifest = "dist/server/__vinext_action_owner_manifest.js"; |
| 92 | expect(yield* fs.exists(path.join(root, manifest))).toBe(true); |
| 93 | expect(output.serverModules?.map((module) => module.name)).toContain(manifest); |
| 94 | }), |
| 95 | ), |
| 96 | 300_000, |
| 97 | ); |
| 98 | |
| 99 | it( |
| 100 | "uploads server modules as rewritten by prerendering, not as bundled", |
| 101 | () => |
| 102 | run( |
| 103 | Effect.gen(function* () { |
| 104 | const fs = yield* FileSystem.FileSystem; |
| 105 | const path = yield* Path.Path; |
| 106 | const root = yield* fixture({ prerender: true }); |
| 107 | const output = yield* build(root, "worker/index.ts"); |
| 108 | const stale: Array<string> = []; |
| 109 | for (const module of output.serverModules ?? []) { |
| 110 | if (!module.name.endsWith(".js")) continue; |
| 111 | const onDisk = yield* fs.readFileString(path.join(root, module.name)); |
| 112 | if (onDisk !== Buffer.from(module.content).toString()) stale.push(module.name); |
| 113 | } |
| 114 | expect(stale).toEqual([]); |
| 115 | }), |
| 116 | ), |
| 117 | 300_000, |
| 118 | ); |
| 119 | }); |