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, 04:45 UTC
| 1 | import { readFile } from "node:fs/promises"; |
| 2 | import { rolldown } from "rolldown"; |
| 3 | import { describe, expect, it } from "vitest"; |
| 4 | import { createMiniflareFromRolldown } from "../../../../cloudflare-test-tools/src/miniflare/miniflare.ts"; |
| 5 | import cloudflare from "../plugin.ts"; |
| 6 | import { buildFixture } from "./utils/build-fixture.ts"; |
| 7 | import { getAsset, getEntryChunk } from "./utils/output.ts"; |
| 8 | |
| 9 | const fixture = "module-rules/index.ts"; |
| 10 | const ttf = { type: "Data" as const, test: (path: string) => path.endsWith(".ttf") }; |
| 11 | |
| 12 | describe("module rules", () => { |
| 13 | it("emits an import matching a user rule as a separate module", async () => { |
| 14 | const built = await buildFixture({ |
| 15 | fixture, |
| 16 | pluginOptions: { compatibilityDate: "2025-07-01", moduleRules: [ttf] }, |
| 17 | }); |
| 18 | const asset = getAsset(built.output, /font.*\.ttf$/); |
| 19 | const font = await readFile(new URL("./fixtures/module-rules/font.ttf", import.meta.url)); |
| 20 | |
| 21 | expect(Buffer.from(asset.source as Uint8Array)).toEqual(font); |
| 22 | expect(getEntryChunk(built.output).code).toContain(asset.fileName); |
| 23 | }); |
| 24 | |
| 25 | it("uploads a match as its rule's type", async () => { |
| 26 | const built = await buildFixture({ |
| 27 | fixture, |
| 28 | pluginOptions: { compatibilityDate: "2025-07-01", moduleRules: [{ ...ttf, type: "Text" }] }, |
| 29 | }); |
| 30 | expect(getAsset(built.output, /font.*\.txt$/)).toBeDefined(); |
| 31 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 32 | compatibilityDate: "2025-07-01", |
| 33 | }); |
| 34 | expect(await miniflare.fetchJson("/")).toEqual({ type: "[object String]" }); |
| 35 | }); |
| 36 | |
| 37 | it("keeps identical files of different types apart", async () => { |
| 38 | const built = await buildFixture({ |
| 39 | fixture: "module-rules-dedupe/index.ts", |
| 40 | pluginOptions: { |
| 41 | compatibilityDate: "2025-07-01", |
| 42 | moduleRules: [{ type: "Text", test: (path) => path.endsWith(".md") }], |
| 43 | }, |
| 44 | }); |
| 45 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 46 | compatibilityDate: "2025-07-01", |
| 47 | }); |
| 48 | expect(await miniflare.fetchJson("/")).toEqual({ |
| 49 | text: "[object String]", |
| 50 | data: "[object ArrayBuffer]", |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | it("leaves an unmatched extension to rolldown", async () => { |
| 55 | await expect(buildFixture({ fixture })).rejects.toThrow(/font\.ttf/); |
| 56 | }); |
| 57 | |
| 58 | it("leaves query imports to their own plugins", async () => { |
| 59 | const built = await buildFixture({ |
| 60 | fixture: "additional-modules/index.ts", |
| 61 | pluginOptions: { |
| 62 | compatibilityDate: "2025-07-01", |
| 63 | moduleRules: [{ test: () => true }], |
| 64 | }, |
| 65 | }); |
| 66 | await using miniflare = await createMiniflareFromRolldown(built.output, { |
| 67 | compatibilityDate: "2025-07-01", |
| 68 | }); |
| 69 | |
| 70 | expect(await miniflare.fetchJson<{ result: number }>("/wasm-with-init-param")).toEqual({ |
| 71 | result: 15, |
| 72 | }); |
| 73 | expect(await miniflare.fetchJson<{ result: number }>("/wasm-with-module-param")).toEqual({ |
| 74 | result: 11, |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | it("leaves virtual modules resolved by later plugins alone", async () => { |
| 79 | // Resolves after the Cloudflare plugins, so `additionalModulesPlugin` |
| 80 | // sees the virtual id from `this.resolve`. |
| 81 | const bundle = await rolldown({ |
| 82 | input: "test/fixtures/module-rules/index.ts", |
| 83 | plugins: [ |
| 84 | cloudflare({ compatibilityDate: "2025-07-01", moduleRules: [ttf] }), |
| 85 | { |
| 86 | name: "virtual-font", |
| 87 | resolveId: (id) => (id === "./font.ttf" ? "\0virtual-font.txt" : undefined), |
| 88 | load: (id) => |
| 89 | id === "\0virtual-font.txt" ? "export default new ArrayBuffer(3);" : undefined, |
| 90 | }, |
| 91 | ], |
| 92 | }); |
| 93 | const { output } = await bundle.generate({ format: "esm" }); |
| 94 | await bundle.close(); |
| 95 | |
| 96 | expect(output.some((item) => item.type === "asset" && !item.fileName.endsWith(".map"))).toBe( |
| 97 | false, |
| 98 | ); |
| 99 | expect(output[0].code).toContain("ArrayBuffer(3)"); |
| 100 | }); |
| 101 | }); |