Refactor bundler function

This commit is contained in:
2026-03-24 20:42:47 +01:00
parent 99b319f5af
commit f6c7f6b78c
14 changed files with 337 additions and 181 deletions
@@ -14,6 +14,7 @@ import { log } from "../../../utils/log";
import grabRootFilePath from "./grab-root-file-path";
import grabPageServerRes from "./grab-page-server-res";
import grabPageServerPath from "./grab-page-server-path";
import grabPageModules from "./grab-page-modules";
class NotFoundError extends Error {}
@@ -30,7 +31,6 @@ export default async function grabPageComponent({
}: Params): Promise<GrabPageComponentRes> {
const url = req?.url ? new URL(req.url) : undefined;
const router = global.ROUTER;
const now = Date.now();
let routeParams: BunxRouteParams | undefined = undefined;
@@ -78,79 +78,18 @@ export default async function grabPageComponent({
log.info(`bundledMap:`, bundledMap);
}
const { root_file_path } = grabRootFilePath();
const root_module: BunextRootModule | undefined = root_file_path
? await import(`${root_file_path}?t=${now}`)
: undefined;
const { server_file_path: root_server_file_path } = root_file_path
? grabPageServerPath({ file_path: root_file_path })
: {};
const root_server_module: BunextPageServerModule = root_server_file_path
? await import(`${root_server_file_path}?t=${now}`)
: undefined;
const root_server_fn =
root_server_module?.default || root_server_module?.server;
const rootServerRes: BunextPageModuleServerReturn | undefined =
root_server_fn
? await grabPageServerRes({
server_function: root_server_fn,
url,
query: match?.query,
routeParams,
})
: undefined;
if (debug) {
log.info(`rootServerRes:`, rootServerRes);
}
const module: BunextPageModule = await import(`${file_path}?t=${now}`);
const { server_file_path } = grabPageServerPath({ file_path });
const server_module: BunextPageServerModule = server_file_path
? await import(`${server_file_path}?t=${now}`)
: undefined;
if (debug) {
log.info(`module:`, module);
}
const server_fn = server_module?.default || server_module?.server;
const serverRes: BunextPageModuleServerReturn | undefined = server_fn
? await grabPageServerRes({
server_function: server_fn,
url,
query: match?.query,
routeParams,
})
: undefined;
if (debug) {
log.info(`serverRes:`, serverRes);
}
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
const { component } =
(await grabPageBundledReactComponent({
const { component, module, serverRes, root_module } =
await grabPageModules({
file_path,
root_file_path,
server_res: mergedServerRes,
})) || {};
if (!component) {
throw new Error(`Couldn't grab page component`);
}
if (debug) {
log.info(`component:`, component);
}
debug,
query: match?.query,
routeParams,
url,
});
return {
component,
serverRes: mergedServerRes,
serverRes,
routeParams,
module,
bundledMap,
@@ -163,6 +102,7 @@ export default async function grabPageComponent({
error,
routeParams,
is404: error instanceof NotFoundError,
url,
});
}
}