Remove esm.sh externals
This commit is contained in:
+1
-1
@@ -32,7 +32,7 @@ export default async function bunextRequestHandler({ req: initial_req, server, }
|
||||
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/")) {
|
||||
|
||||
+38
-7
@@ -2,22 +2,53 @@ 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();
|
||||
export default async function ({ req }) {
|
||||
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\//, ""));
|
||||
if (!file_path.startsWith(HYDRATION_DST_DIR + path.sep)) {
|
||||
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`, {
|
||||
|
||||
+3
@@ -2,4 +2,7 @@ type Params = {
|
||||
req: Request;
|
||||
};
|
||||
export default function ({ req }: Params): Promise<Response>;
|
||||
export declare function readFileResponse({ file_path }: {
|
||||
file_path: string;
|
||||
}): Response;
|
||||
export {};
|
||||
|
||||
+11
-8
@@ -11,14 +11,7 @@ export default async function ({ req }) {
|
||||
if (!file_path.startsWith(PUBLIC_DIR + path.sep)) {
|
||||
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 = {};
|
||||
return new Response(file, res_opts);
|
||||
return readFileResponse({ file_path });
|
||||
}
|
||||
catch (error) {
|
||||
return new Response(`Public File Not Found`, {
|
||||
@@ -26,3 +19,13 @@ export default async function ({ req }) {
|
||||
});
|
||||
}
|
||||
}
|
||||
export function readFileResponse({ file_path }) {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { watch, existsSync } from "fs";
|
||||
import path from "path";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import rebuildBundler from "./rebuild-bundler";
|
||||
import { log } from "../../utils/log";
|
||||
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
|
||||
import serverPostBuildFn from "./server-post-build-fn";
|
||||
|
||||
+39
-31
@@ -1,5 +1,5 @@
|
||||
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
||||
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 isDevelopment from "../../../utils/is-development";
|
||||
@@ -10,6 +10,8 @@ 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 {
|
||||
_reactVersion = JSON.parse(readFileSync(path.join(process.cwd(), "node_modules/react/package.json"), "utf-8")).version;
|
||||
@@ -45,40 +47,46 @@ export default async function genWebHTML({ component, pageProps, bundledMap, mod
|
||||
const RootHead = root_module?.Head;
|
||||
const dev = isDevelopment();
|
||||
const devSuffix = dev ? "?dev" : "";
|
||||
const browser_imports = {
|
||||
react: `https://esm.sh/react@${_reactVersion}`,
|
||||
"react-dom": `https://esm.sh/react-dom@${_reactVersion}`,
|
||||
"react-dom/client": `https://esm.sh/react-dom@${_reactVersion}/client`,
|
||||
"react/jsx-runtime": `https://esm.sh/react@${_reactVersion}/jsx-runtime`,
|
||||
"react/jsx-dev-runtime": `https://esm.sh/react@${_reactVersion}/jsx-dev-runtime`,
|
||||
};
|
||||
// const browser_imports: Record<string, string> = {
|
||||
// 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<string, string> = {
|
||||
// react: `https://esm.sh/react@${_reactVersion}`,
|
||||
// "react-dom": `https://esm.sh/react-dom@${_reactVersion}`,
|
||||
// "react-dom/client": `https://esm.sh/react-dom@${_reactVersion}/client`,
|
||||
// "react/jsx-runtime": `https://esm.sh/react@${_reactVersion}/jsx-runtime`,
|
||||
// "react/jsx-dev-runtime": `https://esm.sh/react@${_reactVersion}/jsx-dev-runtime`,
|
||||
// };
|
||||
// if (dev) {
|
||||
// browser_imports["react/jsx-dev-runtime"] =
|
||||
// `https://esm.sh/react@${_reactVersion}/jsx-dev-runtime`;
|
||||
// }
|
||||
const importMap = JSON.stringify({
|
||||
imports: browser_imports,
|
||||
});
|
||||
const final_meta = _.merge(root_meta, page_meta);
|
||||
let final_component = (_jsxs("html", { ...html_props, children: [_jsxs("head", { children: [_jsx("meta", { charSet: "utf-8" }), _jsx("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0" }), final_meta ? grabWebMetaHTML({ meta: final_meta }) : null, bundledMap?.css_path ? (_jsx("link", { rel: "stylesheet", href: `/${bundledMap.css_path}` })) : null, _jsx("script", { dangerouslySetInnerHTML: {
|
||||
__html: `window.${ClientWindowPagePropsName} = ${serializedProps}`,
|
||||
} }), bundledMap?.path ? (_jsxs(_Fragment, { children: [_jsx("script", { type: "importmap", dangerouslySetInnerHTML: {
|
||||
__html: importMap,
|
||||
}, defer: true }), _jsx("script", { src: `/${bundledMap.path}`, type: "module", id: AppData["BunextClientHydrationScriptID"], defer: true })] })) : null, is_dev ? (_jsx("script", { defer: true, dangerouslySetInnerHTML: {
|
||||
__html: page_hydration_script,
|
||||
} })) : null, RootHead ? (_jsx(RootHead, { serverRes: pageProps, ctx: routeParams })) : null, Head ? _jsx(Head, { serverRes: pageProps, ctx: routeParams }) : null] }), _jsx("body", { children: _jsx("div", { id: ClientRootElementIDName, suppressHydrationWarning: !dev, children: component }) })] }));
|
||||
let html = `<!DOCTYPE html>\n`;
|
||||
// const stream = await renderToReadableStream(final_component, {
|
||||
// onError(error: any) {
|
||||
// // This is where you "omit" or handle the errors
|
||||
// // You can log it silently or ignore it
|
||||
// if (error.message.includes('unique "key" prop')) return;
|
||||
// console.error(error);
|
||||
// },
|
||||
// const importMap = JSON.stringify({
|
||||
// imports: browser_imports,
|
||||
// });
|
||||
// // 2. Convert the Web Stream to a String (Bun-optimized)
|
||||
// const htmlBody = await new Response(stream).text();
|
||||
// html += htmlBody;
|
||||
html += renderToString(final_component);
|
||||
const final_meta = _.merge(root_meta, page_meta);
|
||||
let final_component = (_jsxs("html", { ...html_props, children: [_jsxs("head", { children: [_jsx("meta", { charSet: "utf-8", "data-bunext-head": true }), _jsx("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0", "data-bunext-head": true }), final_meta ? grabWebMetaHTML({ meta: final_meta }) : null, bundledMap?.css_path ? (_jsx("link", { rel: "stylesheet", href: `/${bundledMap.css_path}`, "data-bunext-head": true })) : null, _jsx("script", { dangerouslySetInnerHTML: {
|
||||
__html: `window.${ClientWindowPagePropsName} = ${serializedProps}`,
|
||||
}, "data-bunext-head": true }), RootHead ? (_jsx(RootHead, { serverRes: pageProps, ctx: routeParams })) : null, Head ? _jsx(Head, { serverRes: pageProps, ctx: routeParams }) : null, bundledMap?.path ? (_jsx(_Fragment, { children: _jsx("script", { src: `/${bundledMap.path}`, type: "module", id: AppData["BunextClientHydrationScriptID"], defer: true, "data-bunext-head": true }) })) : null, is_dev ? (_jsx("script", { defer: true, dangerouslySetInnerHTML: {
|
||||
__html: page_hydration_script,
|
||||
}, "data-bunext-head": true })) : null] }), _jsx("body", { children: _jsx("div", { id: ClientRootElementIDName, suppressHydrationWarning: !dev, children: component }) })] }));
|
||||
let html = `<!DOCTYPE html>\n`;
|
||||
const stream = await renderToReadableStream(final_component, {
|
||||
onError(error) {
|
||||
// This is where you "omit" or handle the errors
|
||||
// You can log it silently or ignore it
|
||||
if (error.message.includes('unique "key" prop'))
|
||||
return;
|
||||
console.error(error);
|
||||
},
|
||||
});
|
||||
// 2. Convert the Web Stream to a String (Bun-optimized)
|
||||
const htmlBody = await new Response(stream).text();
|
||||
html += htmlBody;
|
||||
// html += renderToString(final_component);
|
||||
return html;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user