|
1 | | -import { type BuildOutput, serve } from "bun"; |
| 1 | +import { serve } from "bun"; |
2 | 2 | import * as childProcess from "child_process"; |
3 | 3 | import * as fs from "fs"; |
4 | 4 | import * as path from "path"; |
5 | 5 |
|
6 | | -// |
7 | | -// JS |
8 | | -// |
9 | | -const Jsbuilder = (() => { |
10 | | - let buildPromise: Promise<BuildOutput> | undefined; |
11 | | - |
12 | | - const build = () => |
13 | | - Bun.build({ |
14 | | - entrypoints: [__dirname + "/rr/index.html"], |
15 | | - }); |
| 6 | +import homepage from "./rr/index.html"; |
16 | 7 |
|
17 | | - const invalidate = () => { |
18 | | - buildPromise = undefined; |
19 | | - }; |
20 | | - const getArtifacts = async () => { |
21 | | - while (true) { |
22 | | - if (!buildPromise) buildPromise = build(); |
23 | | - const b = buildPromise; |
24 | | - const output = await b; |
25 | | - if (b === buildPromise) return output.outputs; |
26 | | - } |
27 | | - }; |
28 | | - return { invalidate, getArtifacts }; |
29 | | -})(); |
| 8 | +const server = serve({ |
| 9 | + routes: { |
| 10 | + "/": homepage, |
| 11 | + }, |
30 | 12 |
|
31 | | -fs.watch(__dirname + "/../", { recursive: true }, (event, filename) => { |
32 | | - console.log(`Detected ${event} in ${filename}`); |
33 | | - Jsbuilder.invalidate(); |
| 13 | + // Enable development mode for: |
| 14 | + // - Detailed error messages |
| 15 | + // - Hot reloading (Bun v1.2.3+ required) |
| 16 | + development: true, |
34 | 17 | }); |
35 | 18 |
|
| 19 | +console.log(`Listening on ${server.url}`); |
| 20 | + |
36 | 21 | // |
37 | | -// Cargo |
| 22 | +// Cargo rebuild |
38 | 23 | // |
39 | | -const cargoBuilder = (() => { |
| 24 | +{ |
40 | 25 | const build = () => { |
41 | 26 | const wasmPackFile = path.resolve( |
42 | 27 | __dirname, |
43 | 28 | "../../node_modules/wasm-pack/run.js", |
44 | 29 | ); |
| 30 | + const cwd = path.resolve(__dirname, "../../cargo/snk-js"); |
45 | 31 | childProcess.execSync( |
46 | 32 | `${wasmPackFile} build --dev --target web --out-dir ../../packages/snk-js`, |
47 | | - { cwd: path.resolve(__dirname, "../../cargo/snk-js") }, |
| 33 | + { cwd }, |
48 | 34 | ); |
49 | 35 | }; |
50 | 36 |
|
51 | 37 | let timeout: number | Timer | undefined; |
52 | | - const invalidate = () => { |
53 | | - clearTimeout(timeout); |
54 | | - timeout = setTimeout(build, 100); |
55 | | - }; |
56 | | - |
57 | | - return { invalidate }; |
58 | | -})(); |
59 | | - |
60 | | -fs.watch( |
61 | | - path.resolve(__dirname, "../../cargo"), |
62 | | - { recursive: true }, |
63 | | - (event, filename) => { |
64 | | - if (filename?.startsWith("target/")) return; |
65 | | - console.log(`Detected ${event} in ${filename}`); |
66 | | - cargoBuilder.invalidate(); |
67 | | - }, |
68 | | -); |
69 | | - |
70 | | -cargoBuilder.invalidate(); |
71 | | - |
72 | | -// |
73 | | -// http server |
74 | | -// |
75 | | -serve({ |
76 | | - port: 3000, |
77 | | - async fetch(request) { |
78 | | - let { pathname } = new URL(request.url, "http://localhost"); |
79 | | - |
80 | | - if (pathname === "/") pathname = "/index.html"; |
81 | | - |
82 | | - const outputs = await Jsbuilder.getArtifacts(); |
83 | | - const artifact = outputs.find((output) => output.path === `.${pathname}`); |
84 | | - |
85 | | - if (!artifact) return new Response("Not Found", { status: 404 }); |
86 | | - |
87 | | - return new Response(artifact); |
88 | | - }, |
89 | | -}); |
90 | 38 |
|
91 | | -console.log("dev server started at", "http://localhost:3000"); |
| 39 | + fs.watch( |
| 40 | + path.resolve(__dirname, "../../cargo"), |
| 41 | + { recursive: true }, |
| 42 | + (_event, filename) => { |
| 43 | + if (filename?.startsWith("target/")) return; |
| 44 | + clearTimeout(timeout); |
| 45 | + timeout = setTimeout(build, 60); |
| 46 | + }, |
| 47 | + ); |
| 48 | + |
| 49 | + build(); |
| 50 | +} |
0 commit comments