Skip to content

Commit 3492905

Browse files
committed
bun server
1 parent a45a9ed commit 3492905

File tree

5 files changed

+48
-103
lines changed

5 files changed

+48
-103
lines changed

cargo/Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resolver = "3"
33

44
members = [
55
"snk-js",
6-
"snk-grid", "hello-wasm",
6+
"snk-grid",
77
# "snk-solver",
88
# "snk-gif-creator"
99
]

packages/demo3/dev.ts

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,50 @@
1-
import { type BuildOutput, serve } from "bun";
1+
import { serve } from "bun";
22
import * as childProcess from "child_process";
33
import * as fs from "fs";
44
import * as path from "path";
55

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";
167

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+
},
3012

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,
3417
});
3518

19+
console.log(`Listening on ${server.url}`);
20+
3621
//
37-
// Cargo
22+
// Cargo rebuild
3823
//
39-
const cargoBuilder = (() => {
24+
{
4025
const build = () => {
4126
const wasmPackFile = path.resolve(
4227
__dirname,
4328
"../../node_modules/wasm-pack/run.js",
4429
);
30+
const cwd = path.resolve(__dirname, "../../cargo/snk-js");
4531
childProcess.execSync(
4632
`${wasmPackFile} build --dev --target web --out-dir ../../packages/snk-js`,
47-
{ cwd: path.resolve(__dirname, "../../cargo/snk-js") },
33+
{ cwd },
4834
);
4935
};
5036

5137
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-
});
9038

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+
}

packages/demo3/rr/index.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
// auto-refresh
2-
fetch("/")
3-
.then((res) => res.text())
4-
.then(async (original) => {
5-
while (true) {
6-
await new Promise((resolve) => setTimeout(resolve, 1000));
7-
const newContent = await fetch("/").then((res) => res.text());
8-
if (newContent !== original) {
9-
window.location.reload();
10-
}
11-
}
12-
});
13-
141
import init, { get_grid_sample, init_panic_hook } from "snk-js";
152
import wasmUrl from "snk-js/snk_js_bg.wasm";
163
import { createCanvas } from "../utils/canvas";
@@ -21,6 +8,10 @@ init_panic_hook();
218
{
229
const grid = get_grid_sample("caves");
2310
const { canvas, draw } = createCanvas(grid);
24-
draw({ width: grid.width, height: grid.height, data: grid.cells }, [], []);
11+
draw(
12+
{ width: grid.width, height: grid.height, data: grid.cells },
13+
[] as any,
14+
[],
15+
);
2516
document.body.appendChild(canvas);
2617
}

tsconfig.json

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
2-
"compilerOptions": {
3-
"lib": ["dom", "ES2020"],
4-
"strict": true,
5-
"skipLibCheck": true,
6-
"noUnusedLocals": true,
7-
"noUnusedParameters": true,
8-
"forceConsistentCasingInFileNames": true,
9-
"esModuleInterop": true,
10-
"noEmit": true,
11-
"moduleResolution": "node"
12-
},
13-
"exclude": ["node_modules"]
2+
"compilerOptions": {
3+
"lib": ["dom", "ES2020"],
4+
"target": "ES2024",
5+
"strict": true,
6+
"skipLibCheck": true,
7+
"noUnusedLocals": true,
8+
"noUnusedParameters": true,
9+
"forceConsistentCasingInFileNames": true,
10+
"esModuleInterop": true,
11+
"noEmit": true,
12+
"module": "preserve",
13+
"moduleResolution": "node"
14+
},
15+
"exclude": ["node_modules"]
1416
}

0 commit comments

Comments
 (0)