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], plugins: [tailwind],
minify: true, minify: true,
target: "browser", target: "browser",
sourcemap: "linked", sourcemap: "none",
define: { define: {
"process.env.NODE_ENV": JSON.stringify("production"), "process.env.NODE_ENV": JSON.stringify("production"),
}, },

View File

@@ -5,7 +5,7 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "bun --hot src/index.ts", "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" "build": "bun run build.ts"
}, },
"dependencies": { "dependencies": {

View File

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