Major Refactoring. Test update

This commit is contained in:
2026-03-22 16:45:12 +01:00
parent e047539a11
commit 1634eeb213
51 changed files with 389 additions and 319 deletions
@@ -1,8 +1,8 @@
import type { GrabPageReactBundledComponentRes } from "../../../types";
type Params = {
file_path: string;
root_file?: string;
root_file_path?: string;
server_res?: any;
};
export default function grabPageBundledReactComponent({ file_path, root_file, server_res, }: Params): Promise<GrabPageReactBundledComponentRes | undefined>;
export default function grabPageBundledReactComponent({ file_path, root_file_path, server_res, }: Params): Promise<GrabPageReactBundledComponentRes | undefined>;
export {};
@@ -1,11 +1,11 @@
import { jsx as _jsx } from "react/jsx-runtime";
import grabPageReactComponentString from "./grab-page-react-component-string";
import grabTsxStringModule from "./grab-tsx-string-module";
export default async function grabPageBundledReactComponent({ file_path, root_file, server_res, }) {
export default async function grabPageBundledReactComponent({ file_path, root_file_path, server_res, }) {
try {
let tsx = grabPageReactComponentString({
file_path,
root_file,
root_file_path,
server_res,
});
if (!tsx) {
+4 -4
View File
@@ -3,7 +3,7 @@ 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";
import grabRootFilePath from "./grab-root-file-path";
class NotFoundError extends Error {
}
export default async function grabPageComponent({ req, file_path: passed_file_path, debug, }) {
@@ -33,7 +33,7 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
// log.error(errMsg);
throw new Error(errMsg);
}
const bundledMap = global.BUNDLER_CTX_MAP?.find((m) => m.local_path == file_path);
const bundledMap = global.BUNDLER_CTX_MAP[file_path];
if (!bundledMap?.path) {
const errMsg = `No Bundled File Path for this request path!`;
log.error(errMsg);
@@ -42,7 +42,7 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
if (debug) {
log.info(`bundledMap:`, bundledMap);
}
const { root_file } = grabRootFile();
const { root_file_path } = grabRootFilePath();
const module = await import(`${file_path}?t=${now}`);
if (debug) {
log.info(`module:`, module);
@@ -107,7 +107,7 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
const Head = module.Head;
const { component } = (await grabPageBundledReactComponent({
file_path,
root_file,
root_file_path,
server_res: serverRes,
})) || {};
if (!component) {
@@ -11,7 +11,7 @@ export default async function grabPageErrorComponent({ error, routeParams, is404
const match = router.match(errorRoute);
const filePath = match?.filePath || presetComponent;
const bundledMap = match?.filePath
? (global.BUNDLER_CTX_MAP?.find((m) => m.local_path === match.filePath) ?? {})
? global.BUNDLER_CTX_MAP[match.filePath]
: {};
const module = await import(filePath);
const Component = module.default;
@@ -23,9 +23,9 @@ export default async function grabPageErrorComponent({ error, routeParams, is404
bundledMap,
serverRes: {
responseOptions: {
status: is404 ? 404 : 500
}
}
status: is404 ? 404 : 500,
},
},
};
}
catch {
@@ -44,9 +44,9 @@ export default async function grabPageErrorComponent({ error, routeParams, is404
bundledMap: {},
serverRes: {
responseOptions: {
status: is404 ? 404 : 500
}
}
status: is404 ? 404 : 500,
},
},
};
}
}
@@ -1,7 +1,7 @@
type Params = {
file_path: string;
root_file?: string;
root_file_path?: string;
server_res?: any;
};
export default function grabPageReactComponentString({ file_path, root_file, server_res, }: Params): string | undefined;
export default function grabPageReactComponentString({ file_path, root_file_path, server_res, }: Params): string | undefined;
export {};
@@ -1,18 +1,18 @@
import EJSON from "../../../utils/ejson";
import pagePathTransform from "../../../utils/page-path-transform";
export default function grabPageReactComponentString({ file_path, root_file, server_res, }) {
export default function grabPageReactComponentString({ file_path, root_file_path, server_res, }) {
try {
const target_path = pagePathTransform({ page_path: file_path });
let tsx = ``;
const server_res_json = JSON.stringify(EJSON.stringify(server_res || {}) ?? "{}");
if (root_file) {
tsx += `import Root from "${root_file}"\n`;
if (root_file_path) {
tsx += `import Root from "${root_file_path}"\n`;
}
tsx += `import Page from "${target_path}"\n`;
tsx += `export default function Main() {\n\n`;
tsx += `const props = JSON.parse(${server_res_json})\n\n`;
tsx += ` return (\n`;
if (root_file) {
if (root_file_path) {
tsx += ` <Root suppressHydrationWarning={true} {...props}><Page {...props} /></Root>\n`;
}
else {
@@ -0,0 +1,3 @@
export default function grabRootFilePath(): {
root_file_path: string | undefined;
};
@@ -2,13 +2,13 @@ import grabDirNames from "../../../utils/grab-dir-names";
import path from "path";
import AppNames from "../../../utils/grab-app-names";
import { existsSync } from "fs";
export default function grabRootFile() {
export default function grabRootFilePath() {
const { PAGES_DIR } = grabDirNames();
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`;
const root_file = existsSync(root_pages_component_tsx_file)
const root_file_path = existsSync(root_pages_component_tsx_file)
? root_pages_component_tsx_file
: existsSync(root_pages_component_ts_file)
? root_pages_component_ts_file
@@ -17,5 +17,5 @@ export default function grabRootFile() {
: existsSync(root_pages_component_js_file)
? root_pages_component_js_file
: undefined;
return { root_file };
return { root_file_path };
}
-3
View File
@@ -1,3 +0,0 @@
export default function grabRootFile(): {
root_file: string | undefined;
};
@@ -18,7 +18,9 @@ export default async function (params) {
script += ` overlay.innerHTML = \`<div style="max-width:900px;margin:0 auto"><div style="font-size:18px;font-weight:bold;margin-bottom:12px;color:#ff4444">Runtime Error</div><div style="color:#fff;margin-bottom:16px">\${message}</div>\${source ? \`<div style="color:#888;margin-bottom:16px">\${source}</div>\` : ""}\${stack ? \`<pre style="background:#111;padding:16px;border-radius:6px;overflow:auto;color:#ffa07a;white-space:pre-wrap">\${stack}</pre>\` : ""}<button onclick="this.closest('#__bunext_error_overlay').remove()" style="margin-top:16px;padding:8px 16px;background:#333;color:#fff;border:none;border-radius:4px;cursor:pointer">Dismiss</button></div>\`;\n`;
script += ` document.body.appendChild(overlay);\n`;
script += `}\n\n`;
script += `window.addEventListener("error", (e) => __bunext_show_error(e.message, e.filename ? e.filename + ":" + e.lineno + ":" + e.colno : "", e.error?.stack ?? ""));\n`;
script += `window.addEventListener("error", (e) => {\n`;
script += ` __bunext_show_error(e.message, e.filename ? e.filename + ":" + e.lineno + ":" + e.colno : "", e.error?.stack ?? "");\n`;
script += `});\n`;
script += `window.addEventListener("unhandledrejection", (e) => __bunext_show_error(String(e.reason?.message ?? e.reason), "", e.reason?.stack ?? ""));\n\n`;
script += `const hmr = new EventSource("/__hmr");\n`;
script += `window.BUNEXT_HMR = hmr;\n`;
@@ -57,6 +59,9 @@ export default async function (params) {
script += ` newScript.id = "${AppData["BunextClientHydrationScriptID"]}";\n`;
script += ` newScript.type = "module";\n`;
script += ` newScript.src = newScriptPath;\n`;
// script += ` newScript.onerror = (e) => {\n`;
// script += ` window.location.reload();\n`;
// script += ` }\n`;
// script += ` console.log("newScript", newScript);\n`;
script += ` document.head.appendChild(newScript);\n\n`;
script += ` } catch (err) {\n`;