-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
60 lines (52 loc) · 1.4 KB
/
build.ts
File metadata and controls
60 lines (52 loc) · 1.4 KB
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
54
55
56
57
58
59
60
import { spawn } from "bun";
import { globImportPlugin } from "bun-plugin-glob-import";
import { watch } from "fs";
const isCompile = process.argv.includes("--compile");
const isWatch = process.argv.includes("--watch");
async function build() {
const result = await Bun.build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
target: "bun",
minify: !isWatch,
plugins: [globImportPlugin()],
});
if (!result.success) {
console.error("Build failed:", result.logs);
return false;
}
return true;
}
if (isCompile) {
if (await build()) {
const proc = spawn(
["bun", "build", "--compile", "--outfile", "server", "./dist/index.js"],
{ stdout: "inherit", stderr: "inherit" },
);
await proc.exited;
console.log("Compiled: ./server");
}
} else if (isWatch) {
// Dev mode: build, run, and watch for changes
if (!(await build())) process.exit(1);
let serverProc = spawn(["bun", "run", "./dist/index.js"], {
stdout: "inherit",
stderr: "inherit",
});
console.log("Watching for changes...");
watch("./src", { recursive: true }, async (event, filename) => {
if (!filename?.endsWith(".ts")) return;
console.log(`\n[${event}] ${filename} - rebuilding...`);
serverProc.kill();
if (await build()) {
serverProc = spawn(["bun", "run", "./dist/index.js"], {
stdout: "inherit",
stderr: "inherit",
});
}
});
} else {
if (await build()) {
console.log("Bundled: ./dist");
}
}