Production Build Setup
* Use Dist folder instead of runtime hosting Signed-off-by: Indrajith K L <indrajith@indrajith.dev>
This commit is contained in:
2
build.ts
2
build.ts
@@ -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"),
|
||||
},
|
||||
|
||||
@@ -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": {
|
||||
|
||||
47
src/index.ts
47
src/index.ts
@@ -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,
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
development: process.env.NODE_ENV !== "production" && {
|
||||
// Enable browser hot reloading in development
|
||||
hmr: true,
|
||||
let server;
|
||||
|
||||
// Echo console logs from the browser to the server
|
||||
console: true,
|
||||
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 },
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`Server running at ${server.url}`);
|
||||
Reference in New Issue
Block a user