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
+5
View File
@@ -0,0 +1,5 @@
type Params = {
target?: "bun" | "browser";
};
export default function allPagesBunBundler(params?: Params): Promise<void>;
export {};
+47
View File
@@ -0,0 +1,47 @@
import grabAllPages from "../../utils/grab-all-pages";
import grabDirNames from "../../utils/grab-dir-names";
import isDevelopment from "../../utils/is-development";
import { log } from "../../utils/log";
import tailwindcss from "bun-plugin-tailwind";
const { HYDRATION_DST_DIR } = grabDirNames();
export default async function allPagesBunBundler(params) {
const { target = "browser" } = params || {};
const pages = grabAllPages({ exclude_api: true });
const dev = isDevelopment();
let buildStart = 0;
buildStart = performance.now();
const build = await Bun.build({
entrypoints: pages.map((p) => p.transformed_path),
outdir: HYDRATION_DST_DIR,
minify: true,
format: "esm",
define: {
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
},
naming: {
entry: "[name]/[hash].[ext]",
chunk: "chunks/[name]-[hash].[ext]",
},
plugins: [
tailwindcss,
{
name: "post-build",
setup(build) {
build.onEnd((result) => {
console.log("result", result);
});
},
},
],
// plugins: [
// ],
splitting: true,
target,
external: ["bun"],
});
console.log("build", build);
if (build.success) {
const elapsed = (performance.now() - buildStart).toFixed(0);
log.success(`[Built] in ${elapsed}ms`);
}
}
+4 -6
View File
@@ -1,10 +1,8 @@
import type { BundlerCTXMap } from "../../types";
type Params = {
watch?: boolean;
exit_after_first_build?: boolean;
post_build_fn?: (params: {
artifacts: BundlerCTXMap[];
}) => Promise<void>;
/**
* Locations of the pages Files.
*/
page_file_paths?: string[];
};
export default function allPagesBundler(params?: Params): Promise<void>;
export {};
+22 -38
View File
@@ -1,23 +1,28 @@
import { readFileSync, writeFileSync } from "fs";
import * as esbuild from "esbuild";
import grabAllPages from "../../utils/grab-all-pages";
import grabDirNames from "../../utils/grab-dir-names";
import isDevelopment from "../../utils/is-development";
import { execSync } from "child_process";
import { log } from "../../utils/log";
import tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin";
import grabClientHydrationScript from "./grab-client-hydration-script";
import grabArtifactsFromBundledResults from "./grab-artifacts-from-bundled-result";
import stripServerSideLogic from "./strip-server-side-logic";
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE, ROOT_DIR } = grabDirNames();
import { writeFileSync } from "fs";
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
let build_starts = 0;
const MAX_BUILD_STARTS = 10;
export default async function allPagesBundler(params) {
const { page_file_paths } = params || {};
const pages = grabAllPages({ exclude_api: true });
const target_pages = page_file_paths?.[0]
? pages.filter((p) => page_file_paths.includes(p.local_path))
: pages;
if (!page_file_paths) {
global.PAGE_FILES = pages;
}
const virtualEntries = {};
const dev = isDevelopment();
for (const page of pages) {
const key = page.local_path;
for (const page of target_pages) {
const key = page.transformed_path;
const txt = await grabClientHydrationScript({
page_local_path: page.local_path,
});
@@ -37,22 +42,6 @@ export default async function allPagesBundler(params) {
loader: "tsx",
resolveDir: process.cwd(),
}));
build.onLoad({ filter: /\.tsx$/ }, (args) => {
if (args.path.includes("node_modules"))
return;
const source = readFileSync(args.path, "utf8");
if (!source.includes("server")) {
return { contents: source, loader: "tsx" };
}
const strippedCode = stripServerSideLogic({
txt_code: source,
file_path: args.path,
});
return {
contents: strippedCode,
loader: "tsx",
};
});
},
};
const artifactTracker = {
@@ -65,6 +54,7 @@ export default async function allPagesBundler(params) {
if (build_starts == MAX_BUILD_STARTS) {
const error_msg = `Build Failed. Please check all your components and imports.`;
log.error(error_msg);
process.exit(1);
}
});
build.onEnd((result) => {
@@ -79,28 +69,27 @@ export default async function allPagesBundler(params) {
return;
}
const artifacts = grabArtifactsFromBundledResults({
pages,
pages: target_pages,
result,
});
if (artifacts?.[0] && artifacts.length > 0) {
global.BUNDLER_CTX_MAP = artifacts;
global.PAGE_FILES = pages;
params?.post_build_fn?.({ artifacts });
for (let i = 0; i < artifacts.length; i++) {
const artifact = artifacts[i];
global.BUNDLER_CTX_MAP[artifact.local_path] = artifact;
}
// params?.post_build_fn?.({ artifacts });
writeFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, JSON.stringify(artifacts));
}
const elapsed = (performance.now() - buildStart).toFixed(0);
log.success(`[Built] in ${elapsed}ms`);
global.RECOMPILING = false;
if (params?.exit_after_first_build) {
process.exit();
}
build_starts = 0;
});
},
};
execSync(`rm -rf ${HYDRATION_DST_DIR}`);
const ctx = await esbuild.context({
entryPoints: Object.keys(virtualEntries).map((k) => `virtual:${k}`),
const entryPoints = Object.keys(virtualEntries).map((k) => `virtual:${k}`);
await esbuild.build({
entryPoints,
outdir: HYDRATION_DST_DIR,
bundle: true,
minify: true,
@@ -115,11 +104,6 @@ export default async function allPagesBundler(params) {
plugins: [tailwindEsbuildPlugin, virtualPlugin, artifactTracker],
jsx: "automatic",
splitting: true,
logLevel: "silent",
// logLevel: "silent",
});
await ctx.rebuild();
if (params?.watch) {
global.BUNDLER_CTX = ctx;
// global.BUNDLER_CTX.watch();
}
}
@@ -7,12 +7,12 @@ export default function grabArtifactsFromBundledResults({ result, pages, }) {
.filter(([, meta]) => meta.entryPoint)
.map(([outputPath, meta]) => {
const target_page = pages.find((p) => {
return meta.entryPoint === `virtual:${p.local_path}`;
return meta.entryPoint === `virtual:${p.transformed_path}`;
});
if (!target_page || !meta.entryPoint) {
return undefined;
}
const { file_name, local_path, url_path } = target_page;
const { file_name, local_path, url_path, transformed_path } = target_page;
const cssPath = meta.cssBundle || undefined;
return {
path: outputPath,
@@ -25,6 +25,7 @@ export default function grabArtifactsFromBundledResults({ result, pages, }) {
file_name,
local_path,
url_path,
transformed_path,
};
});
if (artifacts.length > 0) {
+3 -4
View File
@@ -1,7 +1,6 @@
import { type Ora } from "ora";
import type { BundlerCTXMap, BunextConfig, GlobalHMRControllerObject, PageFiles } from "../types";
import type { FileSystemRouter, Server } from "bun";
import type { BuildContext } from "esbuild";
import { type FSWatcher } from "fs";
/**
* # Declare Global Variables
@@ -15,9 +14,9 @@ declare global {
var ROUTER: FileSystemRouter;
var HMR_CONTROLLERS: GlobalHMRControllerObject[];
var LAST_BUILD_TIME: number;
var BUNDLER_CTX: BuildContext | undefined;
var BUNDLER_CTX_MAP: BundlerCTXMap[] | undefined;
var IS_FIRST_BUNDLE_READY: boolean;
var BUNDLER_CTX_MAP: {
[k: string]: BundlerCTXMap;
};
var BUNDLER_REBUILDS: 0;
var PAGES_SRC_WATCHER: FSWatcher | undefined;
var CURRENT_VERSION: string | undefined;
+4 -25
View File
@@ -4,21 +4,20 @@ import { readFileSync } from "fs";
import init from "./init";
import isDevelopment from "../utils/is-development";
import allPagesBundler from "./bundler/all-pages-bundler";
import serverPostBuildFn from "./server/server-post-build-fn";
import watcher from "./server/watcher";
import EJSON from "../utils/ejson";
import { log } from "../utils/log";
import cron from "./server/cron";
import EJSON from "../utils/ejson";
const { PAGES_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
export default async function bunextInit() {
global.ORA_SPINNER = ora();
global.ORA_SPINNER.clear();
global.HMR_CONTROLLERS = [];
global.IS_FIRST_BUNDLE_READY = false;
global.BUNDLER_CTX_MAP = {};
global.BUNDLER_REBUILDS = 0;
global.PAGE_FILES = [];
await init();
log.banner();
const { PAGES_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
const router = new Bun.FileSystemRouter({
style: "nextjs",
dir: PAGES_DIR,
@@ -26,10 +25,7 @@ export default async function bunextInit() {
global.ROUTER = router;
const is_dev = isDevelopment();
if (is_dev) {
await allPagesBundler({
watch: true,
post_build_fn: serverPostBuildFn,
});
await allPagesBundler();
watcher();
}
else {
@@ -39,23 +35,6 @@ export default async function bunextInit() {
process.exit(1);
}
global.BUNDLER_CTX_MAP = artifacts;
global.IS_FIRST_BUNDLE_READY = true;
cron();
}
let bundle_ready_retries = 0;
const MAX_BUNDLE_READY_RETRIES = 10;
while (!global.IS_FIRST_BUNDLE_READY) {
if (bundle_ready_retries > MAX_BUNDLE_READY_RETRIES) {
log.error("Couldn't grab first bundle for dev environment");
process.exit(1);
}
bundle_ready_retries++;
await Bun.sleep(500);
}
/**
* First Rebuild to Avoid errors
*/
if (is_dev && global.BUNDLER_CTX) {
await global.BUNDLER_CTX.rebuild();
}
}
+1 -4
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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,
+8 -10
View File
@@ -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
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`;