-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasm-build.ts
53 lines (42 loc) · 1.2 KB
/
wasm-build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { ChildProcess, SpawnOptions } from "node:child_process";
import { spawn } from "node:child_process";
import type { FSWatcher } from "chokidar";
import chokidar from "chokidar";
const CRATE_PATH = "./acrate";
const OUTPUT_PATH = "../app/pkg";
const WASM_PACK_ARGS = ["build", "--out-dir", OUTPUT_PATH];
const SPAWN_OPTS: SpawnOptions = { cwd: CRATE_PATH, stdio: "inherit" };
let currentBuild: ChildProcess | null = null;
let watcher: FSWatcher | null = null;
const args = process.argv.slice(2);
const watch = args.includes("--watch");
function runWasmPack() {
// Kill any existing build process
if (currentBuild) {
currentBuild.kill();
}
currentBuild = spawn("wasm-pack", WASM_PACK_ARGS, SPAWN_OPTS);
currentBuild.on("exit", () => {
currentBuild = null;
});
}
// Initial build
runWasmPack();
if (watch) {
watcher = chokidar.watch(CRATE_PATH, {
ignored: /target/,
});
watcher.on("change", (path) => {
console.log(`🦀 Rust file changed: ${path}`);
runWasmPack();
});
}
function cleanup() {
currentBuild?.kill();
watcher?.close();
process.exit(0);
}
// Handle cleanup
process.on("SIGINT", cleanup);
process.on("SIGTERM", cleanup);
process.on("exit", cleanup);