This commit is contained in:
Benjamin Toby 2026-04-14 15:50:21 +01:00
parent f0aae8a8fa
commit d6f0a7962e
12 changed files with 202 additions and 20 deletions

View File

@ -0,0 +1,7 @@
type Params = {
post_build_fn?: (params: {
artifacts: any[];
}) => Promise<void> | void;
};
export default function pagesSSRBundler(params?: Params): Promise<void>;
export {};

View File

@ -0,0 +1,65 @@
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 tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin";
import grabPageReactComponentString from "../server/web-pages/grab-page-react-component-string";
import grabRootFilePath from "../server/web-pages/grab-root-file-path";
import ssrVirtualFilesPlugin from "./plugins/ssr-virtual-files-plugin";
import ssrCTXArtifactTracker from "./plugins/ssr-ctx-artifact-tracker";
const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames();
export default async function pagesSSRBundler(params) {
const pages = grabAllPages();
const dev = isDevelopment();
const entryToPage = new Map();
const { root_file_path } = grabRootFilePath();
for (const page of pages) {
if (page.local_path.match(/\/pages\/api\//)) {
const ts = await Bun.file(page.local_path).text();
entryToPage.set(page.local_path, { ...page, tsx: ts });
continue;
}
const tsx = grabPageReactComponentString({
file_path: page.local_path,
root_file_path,
});
if (!tsx)
continue;
entryToPage.set(page.local_path, { ...page, tsx });
}
const entryPoints = [...entryToPage.keys()].map((e) => `ssr-virtual:${e}`);
await esbuild.build({
entryPoints,
outdir: BUNX_CWD_MODULE_CACHE_DIR,
bundle: true,
minify: !dev,
format: "esm",
target: "esnext",
platform: "node",
define: {
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
},
entryNames: "[dir]/[hash]",
metafile: true,
plugins: [
tailwindEsbuildPlugin,
ssrVirtualFilesPlugin({
entryToPage,
}),
ssrCTXArtifactTracker({
entryToPage,
post_build_fn: params?.post_build_fn,
}),
],
jsx: "automatic",
external: [
"react",
"react-dom",
"react/jsx-runtime",
"react/jsx-dev-runtime",
"bun:*",
],
splitting: true,
// logLevel: "silent",
});
}

View File

@ -1,10 +1,9 @@
import {} from "esbuild";
import { log } from "../../../utils/log";
import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result";
import pagesSSRContextBundler from "../pages-ssr-context-bundler";
import buildOnstartErrorHandler from "../build-on-start-error-handler";
import apiRoutesContextBundler from "../api-routes-context-bundler";
import _ from "lodash";
import pagesSSRBundler from "../pages-ssr-bundler";
let build_start = 0;
let build_starts = 0;
const MAX_BUILD_STARTS = 2;
@ -42,12 +41,12 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
global.RECOMPILING = false;
global.IS_SERVER_COMPONENT = false;
build_starts = 0;
if (global.SSR_BUNDLER_CTX) {
global.SSR_BUNDLER_CTX.rebuild();
}
else {
pagesSSRContextBundler();
}
pagesSSRBundler();
// if (global.SSR_BUNDLER_CTX) {
// global.SSR_BUNDLER_CTX.rebuild();
// } else {
// pagesSSRContextBundler();
// }
});
},
};

View File

@ -48,3 +48,9 @@ export default async function bunextInit() {
cron();
}
}
process.on("exit", (code) => {
Bun.spawn([process.execPath, ...process.argv.slice(1)], {
stdio: ["inherit", "inherit", "inherit"],
env: process.env,
});
});

View File

@ -43,8 +43,14 @@ export default async function genWebHTML({ component: Main, pageProps, bundledMa
const RootHead = root_module?.Head;
const dev = isDevelopment();
const final_meta = _.merge(root_meta, page_meta);
// const public_envs = Object.keys(process.env).filter((e) =>
// e.startsWith(`NEXT_PUBLIC_`),
// );
const client_process = {
env: {},
};
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}`,
__html: `window.${ClientWindowPagePropsName} = ${serializedProps};\nwindow.process = ${JSON.stringify(client_process)}`,
}, "data-bunext-head": true }), RootHead ? (_jsx(RootHead, { serverRes: pageProps, ctx: routeParams })) : null, Head ? _jsx(Head, { serverRes: pageProps, ctx: routeParams }) : null, bundledMap?.path ? (_jsxs(_Fragment, { children: [_jsx("script", { type: "importmap", dangerouslySetInnerHTML: {
__html: JSON.stringify(global.REACT_IMPORTS_MAP),
}, defer: true, "data-bunext-head": true }), _jsx("script", { src: `/${bundledMap.path}`, type: "module", id: AppData["BunextClientHydrationScriptID"], defer: true, "data-bunext-head": true })] })) : null, is_dev ? (_jsx("script", { defer: true, dangerouslySetInnerHTML: {

View File

@ -6,7 +6,8 @@ import grabPageComponent from "./grab-page-component";
import grabPageErrorComponent from "./grab-page-error-component";
export default async function handleWebPages({ req, }) {
try {
if (!isDevelopment()) {
const is_dev = isDevelopment();
if (!is_dev) {
const url = new URL(req.url);
const key = url.pathname + (url.search || "");
const existing_cache = getCache({ key, paradigm: "html" });

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/bunext",
"version": "1.0.77",
"version": "1.0.78",
"main": "dist/index.js",
"module": "index.ts",
"dependencies": {

View File

@ -0,0 +1,80 @@
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 tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin";
import type { PageFiles } from "../../types";
import grabPageReactComponentString from "../server/web-pages/grab-page-react-component-string";
import grabRootFilePath from "../server/web-pages/grab-root-file-path";
import ssrVirtualFilesPlugin from "./plugins/ssr-virtual-files-plugin";
import ssrCTXArtifactTracker from "./plugins/ssr-ctx-artifact-tracker";
const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames();
type Params = {
post_build_fn?: (params: { artifacts: any[] }) => Promise<void> | void;
};
export default async function pagesSSRBundler(params?: Params) {
const pages = grabAllPages();
const dev = isDevelopment();
const entryToPage = new Map<string, PageFiles & { tsx: string }>();
const { root_file_path } = grabRootFilePath();
for (const page of pages) {
if (page.local_path.match(/\/pages\/api\//)) {
const ts = await Bun.file(page.local_path).text();
entryToPage.set(page.local_path, { ...page, tsx: ts });
continue;
}
const tsx = grabPageReactComponentString({
file_path: page.local_path,
root_file_path,
});
if (!tsx) continue;
entryToPage.set(page.local_path, { ...page, tsx });
}
const entryPoints = [...entryToPage.keys()].map((e) => `ssr-virtual:${e}`);
await esbuild.build({
entryPoints,
outdir: BUNX_CWD_MODULE_CACHE_DIR,
bundle: true,
minify: !dev,
format: "esm",
target: "esnext",
platform: "node",
define: {
"process.env.NODE_ENV": JSON.stringify(
dev ? "development" : "production",
),
},
entryNames: "[dir]/[hash]",
metafile: true,
plugins: [
tailwindEsbuildPlugin,
ssrVirtualFilesPlugin({
entryToPage,
}),
ssrCTXArtifactTracker({
entryToPage,
post_build_fn: params?.post_build_fn,
}),
],
jsx: "automatic",
external: [
"react",
"react-dom",
"react/jsx-runtime",
"react/jsx-dev-runtime",
"bun:*",
],
splitting: true,
// logLevel: "silent",
});
}

View File

@ -2,10 +2,9 @@ import { type Plugin } from "esbuild";
import type { PageFiles } from "../../../types";
import { log } from "../../../utils/log";
import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result";
import pagesSSRContextBundler from "../pages-ssr-context-bundler";
import buildOnstartErrorHandler from "../build-on-start-error-handler";
import apiRoutesContextBundler from "../api-routes-context-bundler";
import _ from "lodash";
import pagesSSRBundler from "../pages-ssr-bundler";
let build_start = 0;
let build_starts = 0;
@ -70,11 +69,13 @@ export default function esbuildCTXArtifactTracker({
build_starts = 0;
if (global.SSR_BUNDLER_CTX) {
global.SSR_BUNDLER_CTX.rebuild();
} else {
pagesSSRContextBundler();
}
pagesSSRBundler();
// if (global.SSR_BUNDLER_CTX) {
// global.SSR_BUNDLER_CTX.rebuild();
// } else {
// pagesSSRContextBundler();
// }
});
},
};

View File

@ -100,3 +100,10 @@ export default async function bunextInit() {
cron();
}
}
process.on("exit", (code) => {
Bun.spawn([process.execPath, ...process.argv.slice(1)], {
stdio: ["inherit", "inherit", "inherit"],
env: process.env,
});
});

View File

@ -70,6 +70,14 @@ export default async function genWebHTML({
const final_meta = _.merge(root_meta, page_meta);
// const public_envs = Object.keys(process.env).filter((e) =>
// e.startsWith(`NEXT_PUBLIC_`),
// );
const client_process = {
env: {},
};
let final_component = (
<html {...html_props}>
<head>
@ -92,7 +100,7 @@ export default async function genWebHTML({
<script
dangerouslySetInnerHTML={{
__html: `window.${ClientWindowPagePropsName} = ${serializedProps}`,
__html: `window.${ClientWindowPagePropsName} = ${serializedProps};\nwindow.process = ${JSON.stringify(client_process)}`,
}}
data-bunext-head
/>

View File

@ -13,7 +13,9 @@ export default async function handleWebPages({
req,
}: Params): Promise<Response> {
try {
if (!isDevelopment()) {
const is_dev = isDevelopment();
if (!is_dev) {
const url = new URL(req.url);
const key = url.pathname + (url.search || "");