// test-bundle.js import { build } from "bun"; import { mkdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; // Ensure output directory exists await mkdir("./test-dist", { recursive: true }); // First, write a temporary platform.js that uses browser code const platformContent = `export * from "./platform.browser.js";`; await writeFile("./src/platform.js", platformContent); console.log("Building main bundle..."); const result = await build({ entrypoints: ["./src/index.js"], outdir: "./test-dist", format: "esm", target: "browser", platform: "browser", minify: true, }); if (!result.success) { console.error("Main bundle build failed:", result.logs); process.exit(1); } console.log("Building worker bundle..."); const workerResult = await build({ entrypoints: ["./src/worker.js"], outdir: "./test-dist", format: "esm", target: "browser", platform: "browser", minify: true, }); if (!workerResult.success) { console.error("Worker bundle build failed:", workerResult.logs); process.exit(1); } console.log("Builds complete"); // Create a simple server to serve the bundles const server = Bun.serve({ port: 3001, async fetch(req) { const url = new URL(req.url); try { // Serve main bundle if (url.pathname === "/d2.mjs") { const file = await Bun.file("./test-dist/index.js").text(); return new Response(file, { headers: { "Content-Type": "application/javascript", "Access-Control-Allow-Origin": "*", }, }); } // Serve worker bundle if (url.pathname === "/worker.js") { const file = await Bun.file("./test-dist/worker.js").text(); return new Response(file, { headers: { "Content-Type": "application/javascript", "Access-Control-Allow-Origin": "*", }, }); } // Serve test page if (url.pathname === "/") { return new Response( `