Updates
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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/")) {
|
||||
|
||||
@@ -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, "");
|
||||
|
||||
@@ -62,6 +62,7 @@ export default async function grabPageCombinedServerRes({
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
props: rootServerRes?.props || null,
|
||||
});
|
||||
|
||||
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -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 = {
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user