|
| 1 | +import { rmSync } from "fs"; |
| 2 | +import { join } from "path"; |
| 3 | + |
| 4 | +const outdir = join(import.meta.dir, "dist"); |
| 5 | +rmSync(outdir, { recursive: true, force: true }); |
| 6 | + |
| 7 | +// |
| 8 | +// 1. build worker first to get its hashed filename |
| 9 | +const workerBuild = await Bun.build({ |
| 10 | + entrypoints: [join(import.meta.dir, "interactive/worker.ts")], |
| 11 | + outdir, |
| 12 | + target: "browser", |
| 13 | + naming: "[name]-[hash].[ext]", |
| 14 | +}); |
| 15 | + |
| 16 | +if (!workerBuild.success) { |
| 17 | + console.error(workerBuild.logs); |
| 18 | + process.exit(1); |
| 19 | +} |
| 20 | + |
| 21 | +const workerFile = "./" + workerBuild.outputs[0].path.split("/").at(-1)!; |
| 22 | + |
| 23 | +// |
| 24 | +// 2. build all demos, injecting the worker url |
| 25 | + |
| 26 | +const [interactiveBuild, demoBuild] = await Promise.all([ |
| 27 | + // interactive -> dist/index.html (root) |
| 28 | + Bun.build({ |
| 29 | + entrypoints: [join(import.meta.dir, "interactive/index.html")], |
| 30 | + outdir, |
| 31 | + root: join(import.meta.dir, "interactive"), |
| 32 | + target: "browser", |
| 33 | + define: { |
| 34 | + "process.env.WORKER_URL": JSON.stringify(workerFile), |
| 35 | + "process.env.GITHUB_USER_CONTRIBUTION_API_ENDPOINT": JSON.stringify( |
| 36 | + process.env.GITHUB_USER_CONTRIBUTION_API_ENDPOINT ?? "/", |
| 37 | + ), |
| 38 | + }, |
| 39 | + }), |
| 40 | + |
| 41 | + // all other demos -> dist/{name}/index.html |
| 42 | + Bun.build({ |
| 43 | + entrypoints: Array.from( |
| 44 | + new Bun.Glob("*/index.html").scanSync(import.meta.dir), |
| 45 | + ) |
| 46 | + .filter((f) => !f.startsWith("dist/") && !f.startsWith("interactive/")) |
| 47 | + .map((f) => join(import.meta.dir, f)), |
| 48 | + outdir, |
| 49 | + target: "browser", |
| 50 | + }), |
| 51 | +]); |
| 52 | + |
| 53 | +if (!interactiveBuild.success) { |
| 54 | + console.error(interactiveBuild.logs); |
| 55 | + process.exit(1); |
| 56 | +} |
| 57 | + |
| 58 | +if (!demoBuild.success) { |
| 59 | + console.error(demoBuild.logs); |
| 60 | + process.exit(1); |
| 61 | +} |
0 commit comments