#Serve Angular with Bun

1 messages · Page 1 of 1 (latest)

crude jackal
#

Hia gang!
I'm trying to basically do everything "ng serve" does only with Bun but after building with Bun.build and serving the output folder, the app isn't loading.

What am I doing wrong?

crude jackal
#

import { serve, spawnSync } from "bun";
import { environment } from "../src/environments/environment.local_stage";

const PORT = 4200; // or any port you prefer
const DIST_DIR = "dist"; // replace with your actual dist directory path

//spawnSync(["bun", "tsc"], { stdio: ["inherit"] });

const result = await Bun.build({
entrypoints: ['src/main.ts'],
root: './',
outdir: DIST_DIR,
target: 'browser',
sourcemap: 'inline',
external: ['chrome'],
define: {
"Bun.env": JSON.stringify(environment)
}
});

serve({
fetch(request) {
// Resolve the file path
const url = new URL(request.url);
let filePath = DIST_DIR + url.pathname;

// Default to index.html for root and any not-found paths (SPA routing)
//if (url.pathname === "/" || !Bun.file(filePath).existsSync()) {
  filePath = DIST_DIR + "/index.html";
//}

// Serve the file
return new Response(Bun.file(filePath));

},
port: PORT,
});

console.log(Server is running on http://localhost:${PORT});

grand umbra
#

@crude jackal there's no existsSync method on Bun.file()