First Commit

This commit is contained in:
2025-11-05 07:12:15 +01:00
commit 8177df7dd3
22 changed files with 807 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
import { existsSync } from "fs";
import type { BunextConfig } from "../types";
import grabDirNames from "../utils/grab-dir-names";
import exitWithError from "../utils/exit-with-error";
export default async function grabConfig(): Promise<BunextConfig> {
const { configFile } = grabDirNames();
if (!existsSync(configFile)) {
exitWithError(`Config file \`${configFile}\` doesn't exist!`);
}
const config = (await import(configFile)).default as BunextConfig;
if (!config) {
exitWithError(
`Config file \`${configFile}\` is invalid! Please provide a valid default export in your config file.`
);
}
return config;
}
+47
View File
@@ -0,0 +1,47 @@
import grabDirNames from "../../utils/grab-dir-names";
import type { GetRouteReturn } from "../../types";
import grabAssetsPrefix from "../../utils/grab-assets-prefix";
import grabOrigin from "../../utils/grab-origin";
type Params = {
route: string;
};
export default async function getRoute({
route,
}: Params): Promise<GetRouteReturn | null> {
const { pagesDir } = grabDirNames();
if (route.match(/\(/)) {
return null;
}
const assetPrefix = grabAssetsPrefix();
const origin = grabOrigin();
const router = new Bun.FileSystemRouter({
style: "nextjs",
dir: pagesDir,
origin,
assetPrefix,
});
const match = router.match(route);
if (!match?.filePath) {
console.error(`Route ${route} not found`);
return null;
}
const module = await import(match.filePath);
return {
match,
module,
component: module.default,
serverProps: module.serverProps,
staticProps: module.staticProps,
staticPaths: module.staticPaths,
staticParams: module.staticParams,
};
}
+31
View File
@@ -0,0 +1,31 @@
import type { GetRouteReturn } from "../../types";
import AppNames from "../../utils/grab-app-names";
import ReactDOMServer from "react-dom/server";
type Params = {
url: URL;
route: GetRouteReturn;
req: Request;
};
export default async function grabRouteContent({
url,
route,
req,
}: Params): Promise<Response> {
const config = global.CONFIG;
const { name } = AppNames;
let html = `Welcome to ${name} ...`;
if (route.component) {
html = ReactDOMServer.renderToString(<route.component />);
return new Response(html, {
headers: {
"Content-Type": "text/html; charset=utf-8",
},
});
}
return new Response(html);
}
+47
View File
@@ -0,0 +1,47 @@
import AppNames from "../../utils/grab-app-names";
import grabAppPort from "../../utils/grab-app-port";
import getRoute from "../router/get-route";
import grabRouteContent from "./grab-route-content";
export default async function startServer() {
const config = global.CONFIG;
const port = grabAppPort();
const { name } = AppNames;
const server = Bun.serve({
async fetch(req) {
try {
const url = new URL(req.url);
const route = await getRoute({ route: url.pathname });
if (!route) {
return new Response(`Route ${url.pathname} not Found!`, {
status: 404,
});
}
const response = await grabRouteContent({ req, route, url });
if (response) {
return response;
}
return new Response(`No Response!`, {
status: 404,
});
} catch (error: any) {
return new Response(`Server Error: ${error.message}`, {
status: 500,
});
}
},
port,
development: true,
});
global.SERVER = server;
console.log(`${name} Server Running on Port ${server.port} ...`);
return server;
}