| 1 | import * as NodeFs from "node:fs/promises"; |
| 2 | import * as NodeOs from "node:os"; |
| 3 | import * as NodePath from "node:path"; |
| 4 | import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 5 | import { readWorkerModules } from "../preview-server.ts"; |
| 6 | |
| 7 | describe("readWorkerModules", () => { |
| 8 | let directory: string; |
| 9 | |
| 10 | beforeAll(async () => { |
| 11 | directory = await NodeFs.mkdtemp( |
| 12 | NodePath.join(NodeOs.tmpdir(), "distilled-modules-"), |
| 13 | ); |
| 14 | await NodeFs.mkdir(NodePath.join(directory, "assets"), { recursive: true }); |
| 15 | await NodeFs.mkdir(NodePath.join(directory, "ssr"), { recursive: true }); |
| 16 | await NodeFs.writeFile( |
| 17 | NodePath.join(directory, "entry.js"), |
| 18 | "export default {};", |
| 19 | ); |
| 20 | await NodeFs.writeFile(NodePath.join(directory, "entry.js.map"), "{}"); |
| 21 | await NodeFs.writeFile( |
| 22 | NodePath.join(directory, "assets", "chunk.js"), |
| 23 | "export const a = 1;", |
| 24 | ); |
| 25 | await NodeFs.writeFile( |
| 26 | NodePath.join(directory, "ssr", "index.js"), |
| 27 | "export const s = 1;", |
| 28 | ); |
| 29 | await NodeFs.writeFile(NodePath.join(directory, "manifest.json"), "{}"); |
| 30 | await NodeFs.writeFile(NodePath.join(directory, "notes.txt"), "text"); |
| 31 | await NodeFs.writeFile( |
| 32 | NodePath.join(directory, "blob.bin"), |
| 33 | new Uint8Array([1, 2, 3]), |
| 34 | ); |
| 35 | await NodeFs.writeFile( |
| 36 | NodePath.join(directory, "lib.wasm"), |
| 37 | new Uint8Array([0, 97, 115, 109]), |
| 38 | ); |
| 39 | }); |
| 40 | |
| 41 | afterAll(async () => { |
| 42 | await NodeFs.rm(directory, { recursive: true, force: true }); |
| 43 | }); |
| 44 | |
| 45 | it("reads the output directory recursively, entry first, skipping source maps", async () => { |
| 46 | const modules = await readWorkerModules({ |
| 47 | directory, |
| 48 | entryModule: "entry.js", |
| 49 | }); |
| 50 | expect(modules[0]).toMatchObject({ name: "entry.js", type: "ESModule" }); |
| 51 | const byName = new Map(modules.map((module) => [module.name, module])); |
| 52 | expect(byName.has("entry.js.map")).toBe(false); |
| 53 | expect(byName.get("assets/chunk.js")).toMatchObject({ type: "ESModule" }); |
| 54 | expect(byName.get("ssr/index.js")).toMatchObject({ type: "ESModule" }); |
| 55 | expect(byName.get("manifest.json")).toMatchObject({ type: "Json" }); |
| 56 | expect(byName.get("notes.txt")).toMatchObject({ type: "Text" }); |
| 57 | expect(byName.get("blob.bin")).toMatchObject({ type: "Data" }); |
| 58 | expect(byName.get("lib.wasm")).toMatchObject({ type: "Wasm" }); |
| 59 | expect(byName.get("lib.wasm")?.content).toBeInstanceOf(Uint8Array); |
| 60 | }); |
| 61 | |
| 62 | it("fails when the entry module is not in the output", async () => { |
| 63 | await expect( |
| 64 | readWorkerModules({ directory, entryModule: "nope.js" }), |
| 65 | ).rejects.toThrow(/nope\.js/); |
| 66 | }); |
| 67 | }); |