This commit is contained in:
2026-04-14 15:50:21 +01:00
parent f0aae8a8fa
commit d6f0a7962e
12 changed files with 202 additions and 20 deletions
@@ -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",
});
}
@@ -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();
// }
});
},
};
+7
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,
});
});
@@ -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
/>
@@ -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 || "");