This commit is contained in:
Benjamin Toby 2026-04-18 11:17:13 +01:00
parent 9a427412f3
commit b2e92e5792
24 changed files with 134 additions and 45 deletions

View File

@ -2,6 +2,8 @@ import { Command } from "commander";
import path from "path";
import grabDirNames from "../../utils/grab-dir-names";
import writeErrorFile from "../../functions/write-error-file";
let retries = 0;
let timeout;
export default function () {
return new Command("dev")
.description("Run development server")
@ -10,11 +12,15 @@ export default function () {
});
}
async function dev() {
clearTimeout(timeout);
if (retries == 1) {
process.exit(1);
}
const dev_spawn_file = path.resolve(__dirname, "dev-spawn.ts");
const spawn_options = {
cmd: ["bun", dev_spawn_file],
stdio: ["inherit", "inherit", "inherit"],
onExit(subprocess, exitCode, signalCode, error) {
async onExit(subprocess, exitCode, signalCode, error) {
writeErrorFile({ exitCode, error });
},
env: {
@ -23,6 +29,10 @@ async function dev() {
},
};
let dev_process = Bun.spawn(spawn_options);
retries++;
timeout = setTimeout(() => {
retries = 0;
}, 5000);
const exited = await dev_process.exited;
if (exited) {
return await dev();

View File

@ -20,8 +20,9 @@ export default async function allPagesESBuildContextBundler(params) {
const tsx = await grabClientHydrationScript({
page_local_path: page.local_path,
});
if (!tsx)
if (!tsx) {
continue;
}
const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`);
// await Bun.write(entryFile, txt, { createPath: true });
entryToPage.set(entryFile, { ...page, tsx });

View File

@ -16,7 +16,9 @@ export default async function pagesSSRBundler(params) {
for (const page of pages) {
if (page.local_path.match(/\/pages\/api\//)) {
const ts = await Bun.file(page.local_path).text();
entryToPage.set(page.local_path, { ...page, tsx: ts });
if (ts.match(/(export default)|(export \w+ handler)/)) {
entryToPage.set(page.local_path, { ...page, tsx: ts });
}
continue;
}
const tsx = grabPageReactComponentString({
@ -25,6 +27,8 @@ export default async function pagesSSRBundler(params) {
});
if (!tsx)
continue;
if (!tsx.match(/export default/))
continue;
entryToPage.set(page.local_path, { ...page, tsx });
}
const entryPoints = [...entryToPage.keys()].map((e) => `ssr-virtual:${e}`);

View File

@ -16,6 +16,9 @@ export default function ssrVirtualFilesPlugin({ entryToPage }) {
if (!target?.tsx)
return null;
const contents = target.tsx;
if (!contents.match(/export/)) {
return null;
}
return {
contents: contents || "",
loader: "tsx",

View File

@ -34,7 +34,7 @@ export default async function bunextRequestHandler({ req: initial_req, server, }
return new Response("Modules Rebuilt");
}
if (url.pathname === "/__hmr" && is_dev) {
response = await handleHmr({ req });
return handleHmr({ req });
}
else if (url.pathname.startsWith("/.bunext")) {
response = await handleBunextPublicAssets({ req });

View File

@ -1,5 +1,5 @@
type Params = {
req: Request;
};
export default function ({ req }: Params): Promise<Response | undefined>;
export default function ({ req }: Params): Promise<Response>;
export {};

View File

@ -81,7 +81,7 @@ export default async function ({ req }) {
}
return final_res;
}
return undefined;
return Response.json({ err: `Route handler error` });
}
// const relative_path = match.filePath.replace(API_DIR, "");
// const relative_module_js_file = relative_path.replace(/\.tsx?$/, ".js");

View File

@ -32,6 +32,7 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
url,
query,
routeParams,
props: rootServerRes?.props || null,
});
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
return { serverRes: mergedServerRes };

View File

@ -7,6 +7,9 @@ import grabPageCombinedServerRes from "./grab-page-combined-server-res";
import fullRebuild from "../full-rebuild";
import serverPostBuildFn from "../server-post-build-fn";
import isDevelopment from "../../../utils/is-development";
import { existsSync } from "fs";
import grabDirNames from "../../../utils/grab-dir-names";
const { BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
class NotFoundError extends Error {
status = 404;
constructor(message) {
@ -24,6 +27,7 @@ export default async function grabPageComponent(params) {
url.protocol = forwarded_proto;
}
let routeParams = undefined;
const does_error_file_exist = existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE);
try {
routeParams = req ? await grabRouteParams({ req }) : undefined;
let url_path = url ? url.pathname : undefined;
@ -46,11 +50,26 @@ export default async function grabPageComponent(params) {
// log.error(errMsg);
throw new Error(errMsg);
}
const bundledMap = global.BUNDLER_CTX_MAP[file_path];
let bundledMap = global.BUNDLER_CTX_MAP[file_path];
if (!bundledMap?.path) {
const errMsg = `No Bundled File Path for this request path!`;
log.error(errMsg);
throw new Error(errMsg);
if (does_error_file_exist) {
throw new Error(`Application Error. Please Check your components. ${match?.filePath} likely exists but has no exported module.`);
}
let retries = 0;
const MAX_RETRIES = 2;
while (retries < MAX_RETRIES) {
await fullRebuild({
msg: `Retrying Bundle map for file \`${file_path}\``,
});
bundledMap = global.BUNDLER_CTX_MAP[file_path];
if (bundledMap?.path)
break;
}
if (!bundledMap?.path) {
const errMsg = `No Bundled File Path for this request path!`;
log.error(errMsg);
throw new Error(errMsg);
}
}
if (req && !is_hydration) {
global.BUNDLER_CTX_MAP[file_path].req = req;

View File

@ -4,6 +4,7 @@ type Params = {
server_function?: BunextPageServerFn;
query?: Record<string, string>;
routeParams?: BunxRouteParams;
props?: Record<string, any> | null;
};
export default function grabPageServerRes({ url, query, routeParams, server_function, }: Params): Promise<BunextPageModuleServerReturn>;
export default function grabPageServerRes({ url, query, routeParams, server_function, props, }: Params): Promise<BunextPageModuleServerReturn>;
export {};

View File

@ -1,6 +1,6 @@
import _ from "lodash";
import { log } from "../../../utils/log";
export default async function grabPageServerRes({ url, query, routeParams, server_function, }) {
export default async function grabPageServerRes({ url, query, routeParams, server_function, props, }) {
const default_props = {
url: url
? {
@ -22,26 +22,22 @@ export default async function grabPageServerRes({ url, query, routeParams, serve
: null,
query,
};
const init_props = props || null;
try {
if (routeParams && server_function) {
const serverData = await server_function({
...routeParams,
query: { ...routeParams.query, ...query },
props: init_props,
});
return {
...serverData,
...default_props,
};
return _.merge(default_props, serverData);
}
return {
...default_props,
};
return _.merge(default_props);
}
catch (error) {
log.error(`Page ${url?.pathname} Server Error => ${error.message}\n`, error);
return {
...default_props,
return _.merge(default_props, {
error: error.message,
};
});
}
}

View File

@ -190,7 +190,9 @@ export type BunextPageServerFn<T extends {
[k: string]: any;
} = {
[k: string]: any;
}> = (ctx: Omit<BunxRouteParams, "body">) => Promise<BunextPageModuleServerReturn<T>>;
}> = (ctx: Omit<BunxRouteParams, "body"> & {
props?: any;
}) => Promise<BunextPageModuleServerReturn<T>>;
export type BunextRouteConfig = {
/**
* Whether to cache the current page

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/bunext",
"version": "1.0.82",
"version": "1.0.83",
"main": "dist/index.js",
"module": "index.ts",
"dependencies": {

View File

@ -4,6 +4,9 @@ import type { BunSpawnOptions } from "../../types";
import grabDirNames from "../../utils/grab-dir-names";
import writeErrorFile from "../../functions/write-error-file";
let retries = 0;
let timeout: any;
export default function () {
return new Command("dev")
.description("Run development server")
@ -13,12 +16,18 @@ export default function () {
}
async function dev() {
clearTimeout(timeout);
if (retries == 1) {
process.exit(1);
}
const dev_spawn_file = path.resolve(__dirname, "dev-spawn.ts");
const spawn_options: BunSpawnOptions = {
cmd: ["bun", dev_spawn_file],
stdio: ["inherit", "inherit", "inherit"],
onExit(subprocess, exitCode, signalCode, error) {
async onExit(subprocess, exitCode, signalCode, error) {
writeErrorFile({ exitCode, error });
},
env: {
@ -29,6 +38,12 @@ async function dev() {
let dev_process = Bun.spawn(spawn_options);
retries++;
timeout = setTimeout(() => {
retries = 0;
}, 5000);
const exited = await dev_process.exited;
if (exited) {
return await dev();

View File

@ -41,7 +41,9 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
page_local_path: page.local_path,
});
if (!tsx) continue;
if (!tsx) {
continue;
}
const entryFile = path.join(
BUNX_HYDRATION_SRC_DIR,

View File

@ -25,7 +25,9 @@ export default async function pagesSSRBundler(params?: Params) {
for (const page of pages) {
if (page.local_path.match(/\/pages\/api\//)) {
const ts = await Bun.file(page.local_path).text();
entryToPage.set(page.local_path, { ...page, tsx: ts });
if (ts.match(/(export default)|(export \w+ handler)/)) {
entryToPage.set(page.local_path, { ...page, tsx: ts });
}
continue;
}
@ -35,6 +37,7 @@ export default async function pagesSSRBundler(params?: Params) {
});
if (!tsx) continue;
if (!tsx.match(/export default/)) continue;
entryToPage.set(page.local_path, { ...page, tsx });
}

View File

@ -30,6 +30,10 @@ export default function ssrVirtualFilesPlugin({ entryToPage }: Params) {
const contents = target.tsx;
if (!contents.match(/export/)) {
return null;
}
return {
contents: contents || "",
loader: "tsx",

View File

@ -50,7 +50,7 @@ export default async function bunextRequestHandler({
}
if (url.pathname === "/__hmr" && is_dev) {
response = await handleHmr({ req });
return handleHmr({ req });
} else if (url.pathname.startsWith("/.bunext")) {
response = await handleBunextPublicAssets({ req });
} else if (url.pathname.startsWith("/api/")) {

View File

@ -17,7 +17,7 @@ type Params = {
req: Request;
};
export default async function ({ req }: Params): Promise<Response | undefined> {
export default async function ({ req }: Params): Promise<Response> {
const url = new URL(req.url);
const is_dev = isDevelopment();
@ -128,7 +128,7 @@ export default async function ({ req }: Params): Promise<Response | undefined> {
return final_res;
}
return undefined;
return Response.json({ err: `Route handler error` });
}
// const relative_path = match.filePath.replace(API_DIR, "");

View File

@ -62,6 +62,7 @@ export default async function grabPageCombinedServerRes({
url,
query,
routeParams,
props: rootServerRes?.props || null,
});
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});

View File

@ -8,6 +8,10 @@ import grabPageCombinedServerRes from "./grab-page-combined-server-res";
import fullRebuild from "../full-rebuild";
import serverPostBuildFn from "../server-post-build-fn";
import isDevelopment from "../../../utils/is-development";
import { existsSync } from "fs";
import grabDirNames from "../../../utils/grab-dir-names";
const { BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
class NotFoundError extends Error {
status = 404;
@ -52,6 +56,8 @@ export default async function grabPageComponent(
let routeParams: BunxRouteParams | undefined = undefined;
const does_error_file_exist = existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE);
try {
routeParams = req ? await grabRouteParams({ req }) : undefined;
@ -83,12 +89,31 @@ export default async function grabPageComponent(
throw new Error(errMsg);
}
const bundledMap = global.BUNDLER_CTX_MAP[file_path];
let bundledMap = global.BUNDLER_CTX_MAP[file_path];
if (!bundledMap?.path) {
const errMsg = `No Bundled File Path for this request path!`;
log.error(errMsg);
throw new Error(errMsg);
if (does_error_file_exist) {
throw new Error(
`Application Error. Please Check your components. ${match?.filePath} likely exists but has no exported module.`,
);
}
let retries = 0;
const MAX_RETRIES = 2;
while (retries < MAX_RETRIES) {
await fullRebuild({
msg: `Retrying Bundle map for file \`${file_path}\``,
});
bundledMap = global.BUNDLER_CTX_MAP[file_path];
if (bundledMap?.path) break;
}
if (!bundledMap?.path) {
const errMsg = `No Bundled File Path for this request path!`;
log.error(errMsg);
throw new Error(errMsg);
}
}
if (req && !is_hydration) {

View File

@ -13,6 +13,7 @@ type Params = {
server_function?: BunextPageServerFn;
query?: Record<string, string>;
routeParams?: BunxRouteParams;
props?: Record<string, any> | null;
};
export default async function grabPageServerRes({
@ -20,6 +21,7 @@ export default async function grabPageServerRes({
query,
routeParams,
server_function,
props,
}: Params): Promise<BunextPageModuleServerReturn> {
const default_props: BunextPageModuleServerReturn = {
url: url
@ -43,31 +45,28 @@ export default async function grabPageServerRes({
query,
};
const init_props = props || null;
try {
if (routeParams && server_function) {
const serverData = await server_function({
...routeParams,
query: { ...routeParams.query, ...query },
props: init_props,
});
return {
...serverData,
...default_props,
};
return _.merge(default_props, serverData);
}
return {
...default_props,
};
return _.merge(default_props);
} catch (error: any) {
log.error(
`Page ${url?.pathname} Server Error => ${error.message}\n`,
error,
);
return {
...default_props,
return _.merge(default_props, {
error: error.message,
};
});
}
}

View File

@ -223,7 +223,9 @@ export type BunextPageModuleMetaTwitter = {
export type BunextPageServerFn<
T extends { [k: string]: any } = { [k: string]: any },
> = (
ctx: Omit<BunxRouteParams, "body">,
ctx: Omit<BunxRouteParams, "body"> & {
props?: any;
},
) => Promise<BunextPageModuleServerReturn<T>>;
export type BunextRouteConfig = {

View File

@ -64,6 +64,7 @@ function grabPageDirRecursively({ page_dir }: { page_dir: string }) {
const new_page_files = grabPageDirRecursively({
page_dir: full_page_path,
});
pages_files.push(...new_page_files);
} else if (page.match(/\.(ts|js)x?$/)) {
const pages_file = grabPageFileObject({