Refactor bundler function
This commit is contained in:
@@ -5,17 +5,21 @@ import type {
|
||||
BunxRouteParams,
|
||||
GrabPageComponentRes,
|
||||
} from "../../../types";
|
||||
import grabPageModules from "./grab-page-modules";
|
||||
import _ from "lodash";
|
||||
|
||||
type Params = {
|
||||
error?: any;
|
||||
routeParams?: BunxRouteParams;
|
||||
is404?: boolean;
|
||||
url?: URL;
|
||||
};
|
||||
|
||||
export default async function grabPageErrorComponent({
|
||||
error,
|
||||
routeParams,
|
||||
is404,
|
||||
url,
|
||||
}: Params): Promise<GrabPageComponentRes> {
|
||||
const router = global.ROUTER;
|
||||
|
||||
@@ -27,28 +31,51 @@ export default async function grabPageErrorComponent({
|
||||
? 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;
|
||||
if (!match?.filePath) {
|
||||
const default_module: BunextPageModule = await import(
|
||||
presetComponent
|
||||
);
|
||||
const Component = default_module.default as FC<any>;
|
||||
const default_jsx = (
|
||||
<Component>{<span>{error.message}</span>}</Component>
|
||||
);
|
||||
|
||||
const module: BunextPageModule = await import(filePath);
|
||||
const Component = module.default as FC<any>;
|
||||
const component = <Component>{<span>{error.message}</span>}</Component>;
|
||||
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,
|
||||
},
|
||||
} as any,
|
||||
serverRes: _.merge(serverRes, default_server_res),
|
||||
root_module,
|
||||
};
|
||||
} catch {
|
||||
const DefaultNotFound: FC = () => (
|
||||
@@ -71,12 +98,7 @@ export default async function grabPageErrorComponent({
|
||||
component: <DefaultNotFound />,
|
||||
routeParams,
|
||||
module: { default: DefaultNotFound },
|
||||
bundledMap: undefined,
|
||||
serverRes: {
|
||||
responseOptions: {
|
||||
status: is404 ? 404 : 500,
|
||||
},
|
||||
} as any,
|
||||
serverRes: default_server_res,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user