Production Build Setup

* Use Dist folder instead of runtime hosting

Signed-off-by: Indrajith K L <indrajith@indrajith.dev>
This commit is contained in:
2026-07-05 20:40:27 +05:30
parent 746593c60d
commit 9e68fa4cea
3 changed files with 54 additions and 37 deletions

View File

@@ -13,7 +13,7 @@ const result = await Bun.build({
plugins: [tailwind],
minify: true,
target: "browser",
sourcemap: "linked",
sourcemap: "none",
define: {
"process.env.NODE_ENV": JSON.stringify("production"),
},

View File

@@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"dev": "bun --hot src/index.ts",
"start": "NODE_ENV=production bun src/index.ts",
"start": "bun run build && NODE_ENV=production bun src/index.ts",
"build": "bun run build.ts"
},
"dependencies": {

View File

@@ -1,11 +1,12 @@
import { serve } from "bun";
import index from "./index.html";
import { fetchAllPackageDetails } from "./services/api";
import path from "node:path";
const server = serve({
routes: {
"/*": index,
"/api/analyze": async req => {
const isProd = process.env.NODE_ENV === "production";
const distDir = path.join(process.cwd(), "dist");
const apiRoutes = {
"/api/analyze": async (req: Request) => {
const data = await req.json();
const dependencies = data.dependencies || {};
const devDependencies = data.devDependencies || {};
@@ -19,23 +20,39 @@ const server = serve({
devDependeciesPackagetDetails = [];
dependenciesPackagetDetails = [];
}
return Response.json({
data: {
devDependencies: devDependeciesPackagetDetails,
dependencies: dependenciesPackagetDetails
dependencies: dependenciesPackagetDetails,
},
});
},
};
let server;
if (isProd) {
server = serve({
routes: {
...apiRoutes,
"/*": async req => {
const url = new URL(req.url);
let filePath = path.join(distDir, url.pathname === "/" ? "index.html" : url.pathname);
const file = Bun.file(filePath);
if (await file.exists()) return new Response(file);
return new Response(Bun.file(path.join(distDir, "index.html")));
},
},
});
} else {
const index = (await import("./index.html")).default;
server = serve({
routes: {
"/*": index,
...apiRoutes,
},
development: { hmr: true, console: true },
});
}
});
},
},
development: process.env.NODE_ENV !== "production" && {
// Enable browser hot reloading in development
hmr: true,
// Echo console logs from the browser to the server
console: true,
},
});
console.log(`Server running at ${server.url}`);