Refactor Bundler. Fix bugs.
This commit is contained in:
+9
-55
@@ -6,12 +6,12 @@ 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 {
|
||||
}
|
||||
export default async function grabPageComponent({ req, file_path: passed_file_path, debug, }) {
|
||||
const url = req?.url ? new URL(req.url) : undefined;
|
||||
const router = global.ROUTER;
|
||||
const now = Date.now();
|
||||
let routeParams = undefined;
|
||||
try {
|
||||
routeParams = req ? await grabRouteParams({ req }) : undefined;
|
||||
@@ -45,63 +45,16 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
|
||||
if (debug) {
|
||||
log.info(`bundledMap:`, bundledMap);
|
||||
}
|
||||
const { root_file_path } = grabRootFilePath();
|
||||
const root_module = 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 = 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 = root_server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: root_server_fn,
|
||||
url,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
if (debug) {
|
||||
log.info(`rootServerRes:`, rootServerRes);
|
||||
}
|
||||
const module = await import(`${file_path}?t=${now}`);
|
||||
const { server_file_path } = grabPageServerPath({ file_path });
|
||||
const server_module = 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 = 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,
|
||||
@@ -114,6 +67,7 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
|
||||
error,
|
||||
routeParams,
|
||||
is404: error instanceof NotFoundError,
|
||||
url,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ type Params = {
|
||||
error?: any;
|
||||
routeParams?: BunxRouteParams;
|
||||
is404?: boolean;
|
||||
url?: URL;
|
||||
};
|
||||
export default function grabPageErrorComponent({ error, routeParams, is404, }: Params): Promise<GrabPageComponentRes>;
|
||||
export default function grabPageErrorComponent({ error, routeParams, is404, url, }: Params): Promise<GrabPageComponentRes>;
|
||||
export {};
|
||||
|
||||
+30
-19
@@ -1,31 +1,47 @@
|
||||
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
export default async function grabPageErrorComponent({ error, routeParams, is404, }) {
|
||||
import grabPageModules from "./grab-page-modules";
|
||||
import _ from "lodash";
|
||||
export default async function grabPageErrorComponent({ error, routeParams, is404, url, }) {
|
||||
const router = global.ROUTER;
|
||||
const { BUNX_ROOT_500_PRESET_COMPONENT, BUNX_ROOT_404_PRESET_COMPONENT } = grabDirNames();
|
||||
const errorRoute = is404 ? "/404" : "/500";
|
||||
const presetComponent = is404
|
||||
? BUNX_ROOT_404_PRESET_COMPONENT
|
||||
: BUNX_ROOT_500_PRESET_COMPONENT;
|
||||
const default_server_res = {
|
||||
responseOptions: {
|
||||
status: is404 ? 404 : 500,
|
||||
},
|
||||
};
|
||||
try {
|
||||
const match = router.match(errorRoute);
|
||||
const filePath = match?.filePath || presetComponent;
|
||||
const bundledMap = match?.filePath
|
||||
? global.BUNDLER_CTX_MAP?.[match.filePath]
|
||||
: undefined;
|
||||
const module = await import(filePath);
|
||||
const Component = module.default;
|
||||
const component = _jsx(Component, { children: _jsx("span", { children: error.message }) });
|
||||
if (!match?.filePath) {
|
||||
const default_module = await import(presetComponent);
|
||||
const Component = default_module.default;
|
||||
const default_jsx = (_jsx(Component, { children: _jsx("span", { children: error.message }) }));
|
||||
return {
|
||||
component: default_jsx,
|
||||
module: default_module,
|
||||
routeParams,
|
||||
serverRes: default_server_res,
|
||||
};
|
||||
}
|
||||
const file_path = match.filePath;
|
||||
const bundledMap = global.BUNDLER_CTX_MAP?.[file_path];
|
||||
const { component, module, serverRes, root_module } = await grabPageModules({
|
||||
file_path: file_path,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
url,
|
||||
});
|
||||
return {
|
||||
component,
|
||||
routeParams,
|
||||
module,
|
||||
bundledMap,
|
||||
serverRes: {
|
||||
responseOptions: {
|
||||
status: is404 ? 404 : 500,
|
||||
},
|
||||
},
|
||||
serverRes: _.merge(serverRes, default_server_res),
|
||||
root_module,
|
||||
};
|
||||
}
|
||||
catch {
|
||||
@@ -41,12 +57,7 @@ export default async function grabPageErrorComponent({ error, routeParams, is404
|
||||
component: _jsx(DefaultNotFound, {}),
|
||||
routeParams,
|
||||
module: { default: DefaultNotFound },
|
||||
bundledMap: undefined,
|
||||
serverRes: {
|
||||
responseOptions: {
|
||||
status: is404 ? 404 : 500,
|
||||
},
|
||||
},
|
||||
serverRes: default_server_res,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { BunextPageModule, BunextPageModuleServerReturn, BunxRouteParams } from "../../../types";
|
||||
import type { JSX } from "react";
|
||||
type Params = {
|
||||
file_path: string;
|
||||
debug?: boolean;
|
||||
url?: URL;
|
||||
query?: any;
|
||||
routeParams?: BunxRouteParams;
|
||||
};
|
||||
export default function grabPageModules({ file_path, debug, url, query, routeParams, }: Params): Promise<{
|
||||
component: JSX.Element;
|
||||
serverRes: BunextPageModuleServerReturn;
|
||||
module: BunextPageModule;
|
||||
root_module: BunextPageModule | undefined;
|
||||
}>;
|
||||
export {};
|
||||
@@ -0,0 +1,69 @@
|
||||
import grabPageBundledReactComponent from "./grab-page-bundled-react-component";
|
||||
import _ from "lodash";
|
||||
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";
|
||||
export default async function grabPageModules({ file_path, debug, url, query, routeParams, }) {
|
||||
const now = Date.now();
|
||||
const { root_file_path } = grabRootFilePath();
|
||||
const root_module = 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 = 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 = root_server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: root_server_fn,
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
if (debug) {
|
||||
log.info(`rootServerRes:`, rootServerRes);
|
||||
}
|
||||
const module = await import(`${file_path}?t=${now}`);
|
||||
const { server_file_path } = grabPageServerPath({ file_path });
|
||||
const server_module = 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 = server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: server_fn,
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
if (debug) {
|
||||
log.info(`serverRes:`, serverRes);
|
||||
}
|
||||
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
|
||||
const { component } = (await grabPageBundledReactComponent({
|
||||
file_path,
|
||||
root_file_path,
|
||||
server_res: mergedServerRes,
|
||||
})) || {};
|
||||
if (!component) {
|
||||
throw new Error(`Couldn't grab page component`);
|
||||
}
|
||||
if (debug) {
|
||||
log.info(`component:`, component);
|
||||
}
|
||||
return {
|
||||
component,
|
||||
serverRes: mergedServerRes,
|
||||
module,
|
||||
root_module,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user