| 1 | import { createMiniflareFromRolldown } from "../../../../cloudflare-test-tools/src/miniflare/miniflare.ts"; |
| 2 | import type { ResolveIdResult } from "rolldown"; |
| 3 | import { describe, expect, it } from "vitest"; |
| 4 | import { cloudflareExternalsPlugin } from "../plugins/cloudflare-externals.ts"; |
| 5 | import { buildFixture } from "./utils/build-fixture.ts"; |
| 6 | |
| 7 | describe("cloudflare externals", () => { |
| 8 | it("marks supported cloudflare:* builtins as external", () => { |
| 9 | const resolved = ( |
| 10 | cloudflareExternalsPlugin.rolldown({}).resolveId as { |
| 11 | handler: (id: string) => ResolveIdResult; |
| 12 | } |
| 13 | ).handler("cloudflare:workers"); |
| 14 | |
| 15 | expect(resolved).toEqual({ |
| 16 | id: "cloudflare:workers", |
| 17 | external: true, |
| 18 | }); |
| 19 | }); |
| 20 | |
| 21 | it("ignores unknown cloudflare:* imports", () => { |
| 22 | expect( |
| 23 | ( |
| 24 | cloudflareExternalsPlugin.rolldown({}).resolveId as { |
| 25 | handler: (id: string) => ResolveIdResult; |
| 26 | } |
| 27 | ).handler("cloudflare:unknown"), |
| 28 | ).toBeUndefined(); |
| 29 | }); |
| 30 | |
| 31 | it("keeps Cloudflare builtin imports working at runtime", async () => { |
| 32 | const built = await buildFixture({ |
| 33 | fixture: "cloudflare-imports/index.ts", |
| 34 | }); |
| 35 | |
| 36 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 37 | compatibilityDate: "2025-07-01", |
| 38 | }); |
| 39 | |
| 40 | const result = await miniflare.fetchJson<Record<string, string>>("/"); |
| 41 | expect(result["(cloudflare:workers) WorkerEntrypoint.name"]).toBe( |
| 42 | "WorkerEntrypoint", |
| 43 | ); |
| 44 | expect(["DurableObject", "DurableObjectBase"]).toContain( |
| 45 | result["(cloudflare:workers) DurableObject.name"], |
| 46 | ); |
| 47 | expect(result["(cloudflare:sockets) typeof connect"]).toBe("function"); |
| 48 | }); |
| 49 | |
| 50 | it("keeps externalized Cloudflare builtins working through dependencies", async () => { |
| 51 | const built = await buildFixture({ |
| 52 | fixture: "cloudflare-imports/index.ts", |
| 53 | }); |
| 54 | |
| 55 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 56 | compatibilityDate: "2025-07-01", |
| 57 | }); |
| 58 | |
| 59 | const result = |
| 60 | await miniflare.fetchJson<Record<string, string>>("/external"); |
| 61 | expect(["DurableObject", "DurableObjectBase"]).toContain( |
| 62 | result["(EXTERNAL) (cloudflare:workers) DurableObject.name"], |
| 63 | ); |
| 64 | }); |
| 65 | }); |