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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user