Update HMR. Make it true HMR. Add URL to page server props
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import type { FC } from "react";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
import grabRouteParams from "../../../utils/grab-route-params";
|
||||
import type {
|
||||
BunextPageModule,
|
||||
@@ -7,29 +6,28 @@ import type {
|
||||
BunxRouteParams,
|
||||
GrabPageComponentRes,
|
||||
} from "../../../types";
|
||||
import path from "path";
|
||||
import AppNames from "../../../utils/grab-app-names";
|
||||
import { existsSync } from "fs";
|
||||
import grabPageErrorComponent from "./grab-page-error-component";
|
||||
import grabPageBundledReactComponent from "./grab-page-bundled-react-component";
|
||||
import _ from "lodash";
|
||||
import { log } from "../../../utils/log";
|
||||
import grabRootFile from "./grab-root-file";
|
||||
|
||||
class NotFoundError extends Error {}
|
||||
|
||||
type Params = {
|
||||
req?: Request;
|
||||
file_path?: string;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
export default async function grabPageComponent({
|
||||
req,
|
||||
file_path: passed_file_path,
|
||||
debug,
|
||||
}: Params): Promise<GrabPageComponentRes> {
|
||||
const url = req?.url ? new URL(req.url) : undefined;
|
||||
const router = global.ROUTER;
|
||||
|
||||
const { PAGES_DIR } = grabDirNames();
|
||||
|
||||
let routeParams: BunxRouteParams | undefined = undefined;
|
||||
|
||||
try {
|
||||
@@ -41,6 +39,10 @@ export default async function grabPageComponent({
|
||||
url_path += url.search;
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
log.info(`url_path:`, url_path);
|
||||
}
|
||||
|
||||
const match = url_path ? router.match(url_path) : undefined;
|
||||
|
||||
if (!match?.filePath && url?.pathname) {
|
||||
@@ -49,6 +51,10 @@ export default async function grabPageComponent({
|
||||
|
||||
const file_path = match?.filePath || passed_file_path;
|
||||
|
||||
if (debug) {
|
||||
log.info(`file_path:`, file_path);
|
||||
}
|
||||
|
||||
if (!file_path) {
|
||||
const errMsg = `No File Path (\`file_path\`) or Request Object (\`req\`) provided not found`;
|
||||
// console.error(errMsg);
|
||||
@@ -65,25 +71,18 @@ export default async function grabPageComponent({
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
const root_pages_component_ts_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.ts`;
|
||||
const root_pages_component_tsx_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.tsx`;
|
||||
const root_pages_component_js_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.js`;
|
||||
const root_pages_component_jsx_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.jsx`;
|
||||
if (debug) {
|
||||
log.info(`bundledMap:`, bundledMap);
|
||||
}
|
||||
|
||||
const root_file = existsSync(root_pages_component_tsx_file)
|
||||
? root_pages_component_tsx_file
|
||||
: existsSync(root_pages_component_ts_file)
|
||||
? root_pages_component_ts_file
|
||||
: existsSync(root_pages_component_jsx_file)
|
||||
? root_pages_component_jsx_file
|
||||
: existsSync(root_pages_component_js_file)
|
||||
? root_pages_component_js_file
|
||||
: undefined;
|
||||
|
||||
const now = Date.now();
|
||||
const { root_file } = grabRootFile();
|
||||
|
||||
const module: BunextPageModule = await import(file_path);
|
||||
|
||||
if (debug) {
|
||||
log.info(`module:`, module);
|
||||
}
|
||||
|
||||
const serverRes: BunextPageModuleServerReturn = await (async () => {
|
||||
const default_props: BunextPageModuleServerReturn = {
|
||||
url: {
|
||||
@@ -123,6 +122,10 @@ export default async function grabPageComponent({
|
||||
}
|
||||
})();
|
||||
|
||||
if (debug) {
|
||||
log.info(`serverRes:`, serverRes);
|
||||
}
|
||||
|
||||
const meta = module.meta
|
||||
? typeof module.meta == "function" && routeParams
|
||||
? await module.meta({
|
||||
@@ -134,6 +137,10 @@ export default async function grabPageComponent({
|
||||
: undefined
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`meta:`, meta);
|
||||
}
|
||||
|
||||
const Head = module.Head as FC<any>;
|
||||
|
||||
const { component } =
|
||||
@@ -147,6 +154,10 @@ export default async function grabPageComponent({
|
||||
throw new Error(`Couldn't grab page component`);
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
log.info(`component:`, component);
|
||||
}
|
||||
|
||||
return {
|
||||
component,
|
||||
serverRes,
|
||||
@@ -157,6 +168,8 @@ export default async function grabPageComponent({
|
||||
head: Head,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error(`Error Grabbing Page Component: ${error.message}`);
|
||||
|
||||
return await grabPageErrorComponent({
|
||||
error,
|
||||
routeParams,
|
||||
|
||||
Reference in New Issue
Block a user