Refactor build process to separate build logic into build.ts and update scripts in package.json

This commit is contained in:
Seth 2025-01-17 19:58:56 -05:00
parent ae63818fe1
commit 876d018239
No known key found for this signature in database
GPG key ID: 8B7A2C60CDF65CAC
3 changed files with 39 additions and 36 deletions

19
build.ts Normal file
View file

@ -0,0 +1,19 @@
const build = async (minify: boolean) => {
const start = Date.now();
const built = await Bun.build({
entrypoints: ["./src/index.html"],
outdir: "./dist",
html: true,
experimentalCss: true,
splitting: true,
sourcemap: "linked",
minify
});
const end = Date.now();
console.log("Did build", built.success, built.logs);
console.log("Build time:", end - start, "ms");
};
await build(true);
export default build;

View file

@ -1,40 +1,24 @@
import { unlink } from "node:fs/promises";
import { watch } from "node:fs";
const NOWATCH = process.env.NOWATCH === "1";
const build = async () => {
try { await unlink("dist"); } catch {}
const start = Date.now();
const built = await Bun.build({
entrypoints: ["./src/index.html"],
outdir: "./dist",
html: true,
experimentalCss: true,
splitting: true,
sourcemap: "linked",
...(NOWATCH ? { minify: true } : {}),
});
const end = Date.now();
console.log("Did build", built.success, built.logs);
console.log("Build time:", end - start, "ms");
};
const build = await import("./build");
if (!NOWATCH) {
watch(import.meta.dir, { recursive: true }, async (_, file) => {
if (!file?.startsWith("dist")) return await build();
return;
});
Bun.serve({
async fetch(request, server) {
const { pathname } = new URL(request.url);
const file = pathname === "/" ? "/index.html" : pathname;
return new Response(Bun.file(`dist${file}`));
},
});
build.default(false);
}
await build();
watch(import.meta.dir, { recursive: true }, async (_, file) => {
if (file?.startsWith("dist")) return;
return await build();
});
Bun.serve({
async fetch(request, server) {
const { pathname } = new URL(request.url);
const file = pathname === "/" ? "/index.html" : pathname;
return new Response(Bun.file(`dist${file}`));
},
});
await build();

View file

@ -3,8 +3,8 @@
"module": "index.ts",
"type": "module",
"scripts": {
"build": "NOWATCH=1 bun .",
"start": "bun ."
"build": "bun run build.ts",
"start": "bun run ."
},
"devDependencies": {
"@biomejs/biome": "1.9.4",