From 68d88bce4aedd212a2494e04275a565435b4c3b3 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Tue, 31 Mar 2026 06:22:26 +0100 Subject: [PATCH] Try local react imports --- .gitignore | 3 +- src/commands/build/index.ts | 3 - src/functions/bunext-init.ts | 5 +- src/functions/server/bunext-req-handler.ts | 2 +- .../server/handle-bunext-public-assets.ts | 49 ++++++++++-- src/functions/server/handle-public.ts | 26 ++++--- .../server/web-pages/generate-web-html.tsx | 56 ++++++------- src/types/index.ts | 38 +++++---- src/utils/grab-dir-names.ts | 78 +++++++++++++++++++ 9 files changed, 191 insertions(+), 69 deletions(-) diff --git a/.gitignore b/.gitignore index 5f7af7b..fa324a2 100644 --- a/.gitignore +++ b/.gitignore @@ -180,4 +180,5 @@ __fixtures__ /public /.data /.dump -/.vscode \ No newline at end of file +/.vscode +/source.md \ No newline at end of file diff --git a/src/commands/build/index.ts b/src/commands/build/index.ts index 6efa0c1..f1b43d0 100644 --- a/src/commands/build/index.ts +++ b/src/commands/build/index.ts @@ -1,11 +1,8 @@ import { Command } from "commander"; import { log } from "../../utils/log"; import init from "../../functions/init"; -// import rewritePagesModule from "../../utils/rewrite-pages-module"; -import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler"; import grabDirNames from "../../utils/grab-dir-names"; import { rmSync } from "fs"; -import allPagesBundler from "../../functions/bundler/all-pages-bundler"; import allPagesESBuildContextBundler from "../../functions/bundler/all-pages-esbuild-context-bundler"; const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames(); diff --git a/src/functions/bunext-init.ts b/src/functions/bunext-init.ts index 0b46904..c39d70e 100644 --- a/src/functions/bunext-init.ts +++ b/src/functions/bunext-init.ts @@ -33,9 +33,11 @@ declare global { var ROOT_FILE_UPDATED: boolean; var SKIPPED_BROWSER_MODULES: Set; var BUNDLER_CTX: BuildContext | undefined; + var DIR_NAMES: ReturnType; } -const { PAGES_DIR } = grabDirNames(); +const dirNames = grabDirNames(); +const { PAGES_DIR } = dirNames; export default async function bunextInit() { global.HMR_CONTROLLERS = []; @@ -43,6 +45,7 @@ export default async function bunextInit() { global.BUNDLER_REBUILDS = 0; global.PAGE_FILES = []; global.SKIPPED_BROWSER_MODULES = new Set(); + global.DIR_NAMES = dirNames; await init(); log.banner(); diff --git a/src/functions/server/bunext-req-handler.ts b/src/functions/server/bunext-req-handler.ts index f5c7e6f..b231251 100644 --- a/src/functions/server/bunext-req-handler.ts +++ b/src/functions/server/bunext-req-handler.ts @@ -48,7 +48,7 @@ export default async function bunextRequestHandler({ if (url.pathname === "/__hmr" && is_dev) { response = await handleHmr({ req }); - } else if (url.pathname.startsWith("/.bunext/public/pages")) { + } else if (url.pathname.startsWith("/.bunext")) { response = await handleBunextPublicAssets({ req }); } else if (url.pathname.startsWith("/api/")) { response = await handleRoutes({ req }); diff --git a/src/functions/server/handle-bunext-public-assets.ts b/src/functions/server/handle-bunext-public-assets.ts index 74c53e7..83f3fd0 100644 --- a/src/functions/server/handle-bunext-public-assets.ts +++ b/src/functions/server/handle-bunext-public-assets.ts @@ -2,6 +2,7 @@ import grabDirNames from "../../utils/grab-dir-names"; import path from "path"; import isDevelopment from "../../utils/is-development"; import { existsSync } from "fs"; +import { readFileResponse } from "./handle-public"; const { HYDRATION_DST_DIR } = grabDirNames(); @@ -13,6 +14,45 @@ export default async function ({ req }: Params): Promise { try { const is_dev = isDevelopment(); const url = new URL(req.url); + + // switch (url.pathname) { + // case "/.bunext/react": + // return readFileResponse({ + // file_path: is_dev + // ? global.DIR_NAMES.REACT_DEVELOPMENT_MODULE + // : global.DIR_NAMES.REACT_PRODUCTION_MODULE, + // }); + // case "/.bunext/react-dom": + // return readFileResponse({ + // file_path: is_dev + // ? global.DIR_NAMES.REACT_DOM_DEVELOPMENT_MODULE + // : global.DIR_NAMES.REACT_DOM_PRODUCTION_MODULE, + // }); + // case "/.bunext/react-dom-client": + // return readFileResponse({ + // file_path: is_dev + // ? global.DIR_NAMES.REACT_DOM_CLIENT_DEVELOPMENT_MODULE + // : global.DIR_NAMES.REACT_DOM_CLIENT_PRODUCTION_MODULE, + // }); + // case "/.bunext/react-jsx-runtime": + // return readFileResponse({ + // file_path: is_dev + // ? global.DIR_NAMES.REACT_JSX_RUNTIME_DEVELOPMENT_MODULE + // : global.DIR_NAMES.REACT_JSX_RUNTIME_PRODUCTION_MODULE, + // }); + // case "/.bunext/react-jsx-dev-runtime": + // return readFileResponse({ + // file_path: is_dev + // ? global.DIR_NAMES + // .REACT_JSX_DEVELOPMENT_RUNTIME_DEVELOPMENT_MODULE + // : global.DIR_NAMES + // .REACT_JSX_DEVELOPMENT_RUNTIME_PRODUCTION_MODULE, + // }); + + // default: + // break; + // } + const file_path = path.join( HYDRATION_DST_DIR, url.pathname.replace(/\/\.bunext\/public\/pages\//, ""), @@ -22,14 +62,7 @@ export default async function ({ req }: Params): Promise { return new Response("Forbidden", { status: 403 }); } - if (!existsSync(file_path)) { - return new Response(`File Doesn't Exist`, { - status: 404, - }); - } - - const file = Bun.file(file_path); - return new Response(file); + return readFileResponse({ file_path }); } catch (error) { return new Response(`File Not Found`, { status: 404, diff --git a/src/functions/server/handle-public.ts b/src/functions/server/handle-public.ts index 4bff2ea..c345d33 100644 --- a/src/functions/server/handle-public.ts +++ b/src/functions/server/handle-public.ts @@ -23,20 +23,24 @@ export default async function ({ req }: Params): Promise { return new Response("Forbidden", { status: 403 }); } - if (!existsSync(file_path)) { - return new Response(`Public File Doesn't Exist`, { - status: 404, - }); - } - - const file = Bun.file(file_path); - - let res_opts: ResponseInit = {}; - - return new Response(file, res_opts); + return readFileResponse({ file_path }); } catch (error) { return new Response(`Public File Not Found`, { status: 404, }); } } + +export function readFileResponse({ file_path }: { file_path: string }) { + if (!existsSync(file_path)) { + return new Response(`Public File Doesn't Exist`, { + status: 404, + }); + } + + const file = Bun.file(file_path); + + // let res_opts: ResponseInit = {}; + + return new Response(file); +} diff --git a/src/functions/server/web-pages/generate-web-html.tsx b/src/functions/server/web-pages/generate-web-html.tsx index 0b65222..a8dc8a2 100644 --- a/src/functions/server/web-pages/generate-web-html.tsx +++ b/src/functions/server/web-pages/generate-web-html.tsx @@ -1,4 +1,4 @@ -import { renderToString } from "react-dom/server"; +import { renderToReadableStream, renderToString } from "react-dom/server"; import grabContants from "../../../utils/grab-constants"; import EJSON from "../../../utils/ejson"; import type { LivePageDistGenParams } from "../../../types"; @@ -10,6 +10,9 @@ import { AppData } from "../../../data/app-data"; import { readFileSync } from "fs"; import path from "path"; import _ from "lodash"; +import grabDirNames from "../../../utils/grab-dir-names"; + +const {} = grabDirNames(); let _reactVersion = "19"; try { @@ -70,6 +73,15 @@ export default async function genWebHTML({ const dev = isDevelopment(); const devSuffix = dev ? "?dev" : ""; + + // const browser_imports: Record = { + // react: `/.bunext/react`, + // "react-dom": `/.bunext/react-dom`, + // "react-dom/client": `/.bunext/react-dom-client`, + // "react/jsx-runtime": `/.bunext/react-jsx-runtime`, + // "react/jsx-dev-runtime": `/.bunext/react-jsx-dev-runtime`, + // }; + const browser_imports: Record = { react: `https://esm.sh/react@${_reactVersion}`, "react-dom": `https://esm.sh/react-dom@${_reactVersion}`, @@ -110,15 +122,10 @@ export default async function genWebHTML({ }} /> - {/* {global.SKIPPED_BROWSER_MODULES ? ( -