Major Refactoring. Test update
This commit is contained in:
Vendored
+1
-4
@@ -1,11 +1,8 @@
|
||||
import grabRouteParams from "../../utils/grab-route-params";
|
||||
import grabConstants from "../../utils/grab-constants";
|
||||
import grabRouter from "../../utils/grab-router";
|
||||
export default async function ({ req }) {
|
||||
const referer_url = new URL(req.headers.get("referer") || "");
|
||||
const match = global.ROUTER.match(referer_url.pathname);
|
||||
const target_map = match?.filePath
|
||||
? global.BUNDLER_CTX_MAP?.find((m) => m.local_path == match.filePath)
|
||||
? global.BUNDLER_CTX_MAP[match.filePath]
|
||||
: undefined;
|
||||
let controller;
|
||||
let heartbeat;
|
||||
|
||||
+5
-1
@@ -1 +1,5 @@
|
||||
export default function rebuildBundler(): Promise<void>;
|
||||
type Params = {
|
||||
target_file_paths?: string[];
|
||||
};
|
||||
export default function rebuildBundler(params?: Params): Promise<void>;
|
||||
export {};
|
||||
|
||||
+5
-5
@@ -1,15 +1,15 @@
|
||||
import allPagesBundler from "../bundler/all-pages-bundler";
|
||||
import serverPostBuildFn from "./server-post-build-fn";
|
||||
import { log } from "../../utils/log";
|
||||
export default async function rebuildBundler() {
|
||||
export default async function rebuildBundler(params) {
|
||||
try {
|
||||
global.ROUTER.reload();
|
||||
await global.BUNDLER_CTX?.dispose();
|
||||
global.BUNDLER_CTX = undefined;
|
||||
// await global.BUNDLER_CTX?.dispose();
|
||||
// global.BUNDLER_CTX = undefined;
|
||||
await allPagesBundler({
|
||||
watch: true,
|
||||
post_build_fn: serverPostBuildFn,
|
||||
page_file_paths: params?.target_file_paths,
|
||||
});
|
||||
await serverPostBuildFn();
|
||||
}
|
||||
catch (error) {
|
||||
log.error(error);
|
||||
|
||||
+1
-6
@@ -1,6 +1 @@
|
||||
import type { BundlerCTXMap } from "../../types";
|
||||
type Params = {
|
||||
artifacts: BundlerCTXMap[];
|
||||
};
|
||||
export default function serverPostBuildFn({ artifacts }: Params): Promise<void>;
|
||||
export {};
|
||||
export default function serverPostBuildFn(): Promise<void>;
|
||||
|
||||
+9
-6
@@ -1,15 +1,18 @@
|
||||
import _ from "lodash";
|
||||
import grabPageComponent from "./web-pages/grab-page-component";
|
||||
export default async function serverPostBuildFn({ artifacts }) {
|
||||
if (!global.IS_FIRST_BUNDLE_READY) {
|
||||
global.IS_FIRST_BUNDLE_READY = true;
|
||||
}
|
||||
if (!global.HMR_CONTROLLERS?.[0]) {
|
||||
export default async function serverPostBuildFn() {
|
||||
// if (!global.IS_FIRST_BUNDLE_READY) {
|
||||
// global.IS_FIRST_BUNDLE_READY = true;
|
||||
// }
|
||||
if (!global.HMR_CONTROLLERS?.[0] || !global.BUNDLER_CTX_MAP) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < global.HMR_CONTROLLERS.length; i++) {
|
||||
const controller = global.HMR_CONTROLLERS[i];
|
||||
const target_artifact = artifacts.find((a) => controller.target_map?.local_path == a.local_path);
|
||||
if (!controller.target_map?.local_path) {
|
||||
continue;
|
||||
}
|
||||
const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
||||
const mock_req = new Request(controller.page_url);
|
||||
const { serverRes } = await grabPageComponent({
|
||||
req: mock_req,
|
||||
|
||||
Vendored
+8
-10
@@ -1,4 +1,4 @@
|
||||
import { watch, existsSync, statSync } from "fs";
|
||||
import { watch, existsSync } from "fs";
|
||||
import path from "path";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import rebuildBundler from "./rebuild-bundler";
|
||||
@@ -33,16 +33,11 @@ export default async function watcher() {
|
||||
}
|
||||
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
||||
if (event !== "rename") {
|
||||
if (filename.match(target_files_match) && global.BUNDLER_CTX) {
|
||||
if (filename.match(target_files_match)) {
|
||||
if (global.RECOMPILING)
|
||||
return;
|
||||
global.RECOMPILING = true;
|
||||
if (full_file_path.match(/\_\_root\.tsx?$/)) {
|
||||
// log.watch(`__root.tsx file updated. Reloading window.`);
|
||||
global.ROOT_FILE_UPDATED = true;
|
||||
}
|
||||
await rewritePagesModule({ page_url: full_file_path });
|
||||
await global.BUNDLER_CTX.rebuild();
|
||||
await fullRebuild();
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -64,13 +59,16 @@ export default async function watcher() {
|
||||
});
|
||||
global.PAGES_SRC_WATCHER = pages_src_watcher;
|
||||
}
|
||||
async function fullRebuild({ msg }) {
|
||||
async function fullRebuild(params) {
|
||||
try {
|
||||
const { msg } = params || {};
|
||||
global.RECOMPILING = true;
|
||||
const target_file_paths = global.HMR_CONTROLLERS.map((hmr) => hmr.target_map?.local_path).filter((f) => typeof f == "string");
|
||||
await rewritePagesModule({ page_file_path: target_file_paths });
|
||||
if (msg) {
|
||||
log.watch(msg);
|
||||
}
|
||||
await rebuildBundler();
|
||||
await rebuildBundler({ target_file_paths });
|
||||
}
|
||||
catch (error) {
|
||||
log.error(error);
|
||||
|
||||
@@ -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
@@ -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;
|
||||
};
|
||||
+3
-3
@@ -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 };
|
||||
}
|
||||
@@ -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`;
|
||||
|
||||
Reference in New Issue
Block a user