| 1 | import { createMiniflareFromRolldown } from "../../../../cloudflare-test-tools/src/miniflare/miniflare.ts"; |
| 2 | import path from "node:path"; |
| 3 | import { describe, expect, it } from "vitest"; |
| 4 | import { buildFixture } from "./utils/build-fixture.ts"; |
| 5 | |
| 6 | describe("module resolution", async () => { |
| 7 | const built = await buildFixture({ |
| 8 | fixture: "module-resolution/index.ts", |
| 9 | inputOptions: { |
| 10 | resolve: { |
| 11 | alias: { |
| 12 | "@alias/test": path.resolve( |
| 13 | "test/fixtures/module-resolution/aliasing.ts", |
| 14 | ), |
| 15 | }, |
| 16 | }, |
| 17 | }, |
| 18 | }); |
| 19 | |
| 20 | it("supports package exports and subpath exports", async () => { |
| 21 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 22 | compatibilityDate: "2025-07-01", |
| 23 | }); |
| 24 | |
| 25 | expect( |
| 26 | await miniflare.fetchJson<{ packageMessage: string }>("/package-export"), |
| 27 | ).toEqual({ |
| 28 | packageMessage: "package export", |
| 29 | }); |
| 30 | expect( |
| 31 | await miniflare.fetchJson<{ featureMessage: string }>("/package-subpath"), |
| 32 | ).toEqual({ |
| 33 | featureMessage: "package subpath", |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | it("resolves packages that rely on mainFields when no exports map is present", async () => { |
| 38 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 39 | compatibilityDate: "2025-07-01", |
| 40 | }); |
| 41 | |
| 42 | expect( |
| 43 | await miniflare.fetchJson<{ legacyMessage: string }>( |
| 44 | "/package-main-fields", |
| 45 | ), |
| 46 | ).toEqual({ |
| 47 | legacyMessage: "browser field", |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | it("supports explicit and extensionless resolution for js and cjs modules", async () => { |
| 52 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 53 | compatibilityDate: "2025-07-01", |
| 54 | }); |
| 55 | |
| 56 | expect( |
| 57 | await miniflare.fetchJson<{ helloWorldExt: string }>("/require-ext"), |
| 58 | ).toEqual({ |
| 59 | helloWorldExt: "hello (.js) world (.cjs)", |
| 60 | }); |
| 61 | expect( |
| 62 | await miniflare.fetchJson<{ helloWorldNoExt: string }>("/require-no-ext"), |
| 63 | ).toEqual({ |
| 64 | helloWorldNoExt: "hello (.js) world (.cjs)", |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | it("coexists with user aliases", async () => { |
| 69 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 70 | compatibilityDate: "2025-07-01", |
| 71 | }); |
| 72 | |
| 73 | expect(await miniflare.fetchText("/@alias/test")).toBe("OK!"); |
| 74 | }); |
| 75 | }); |