Files
bunext/src/functions/bundler/pages-ssr-context-bundler.ts
T
2026-10-16 14:25:18 +01:00

247 lines
8.4 KiB
TypeScript

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";
import cleanupArtifacts from "../server/cleanup-artifacts";
import { evictDeletedModules } from "../../utils/import-fresh";
import { writeFileSync } from "fs";
import path from "path";
import { log } from "../../utils/log";
import {
logBuildErrorsOnce,
} from "../../utils/build-error-log";
const { BUNX_CWD_MODULE_CACHE_DIR, BUNX_TMP_DIR } = grabDirNames();
type SSRVirtualEntry = PageFiles & { tsx: string };
// Current virtual entry contents. The SSR virtual plugin resolves its
// onLoad contents lazily from this map on every rebuild, so edits to
// `.server.ts`/api files (whose entries mirror their raw file content) are
// picked up without recreating the esbuild context.
let current_entry_map = new Map<string, SSRVirtualEntry>();
// Entry key set + root file presence of the last *successful* build. When
// this differs from the freshly computed set, the context must be
// recreated (esbuild cannot add/remove entry points on a live context).
let last_entry_keys: string[] | undefined = undefined;
let last_root_file_path: string | undefined = undefined;
let rebuild_in_flight: Promise<void> | undefined;
type RebuildParams = {
/**
* Force a context recreation even when the entry set is unchanged.
* Used by `fullRebuild` after it disposed the contexts.
*/
force?: boolean;
};
function arraysEqual(a: string[], b: string[]): boolean {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
async function computeEntryToPage(): Promise<{
entryToPage: Map<string, SSRVirtualEntry>;
root_file_path: string | undefined;
}> {
const pages = grabAllPages({
include_server: true,
});
const { root_file_path } = grabRootFilePath();
const entryToPage = new Map<string, SSRVirtualEntry>();
try {
writeFileSync(
path.join(BUNX_TMP_DIR, "ssr-pages.json"),
JSON.stringify(pages, null, 4),
);
} catch (error) {}
for (const page of pages) {
if (
page.local_path.match(/\/pages\/api\//) ||
page.local_path.match(/\.server\.tsx?$/)
) {
const ts = await Bun.file(page.local_path).text();
if (
ts.match(
/(export default)|(export \w+ handler)|(export \w+ server)/,
)
) {
entryToPage.set(page.local_path, { ...page, tsx: ts });
}
continue;
}
const tsx = grabPageReactComponentString({
file_path: page.local_path,
root_file_path,
});
if (!tsx) continue;
if (!tsx.match(/export default/)) continue;
entryToPage.set(page.local_path, { ...page, tsx });
}
return { entryToPage, root_file_path };
}
/**
* Single-flight SSR rebuild. Concurrent callers (each successful hydration
* rebuild triggers this, plus watcher/structural paths) join the in-flight
* rebuild instead of spawning overlapping esbuild builds.
*
* A fresh content change that lands while a rebuild is in flight is picked
* up by the next hydration rebuild cycle (each watcher event re-runs the
* hydration context rebuild, which re-enters this queue).
*/
export default async function enqueueSSRRebuild(params?: RebuildParams) {
if (rebuild_in_flight) {
return rebuild_in_flight;
}
const run = doSSRRebuild(params).finally(() => {
if (rebuild_in_flight === run) {
rebuild_in_flight = undefined;
}
});
rebuild_in_flight = run;
return run;
}
async function doSSRRebuild(params?: RebuildParams) {
const force = Boolean(params?.force);
const dev = isDevelopment();
const config = global.BUNEXT_CONFIG;
try {
const { entryToPage, root_file_path } = await computeEntryToPage();
const entry_keys = [...entryToPage.keys()].sort();
if (entry_keys.length === 0) {
current_entry_map = entryToPage;
last_entry_keys = entry_keys;
last_root_file_path = root_file_path;
return;
}
// Refresh the virtual contents BEFORE rebuilding so the plugin's
// onLoad sees the latest `.server.ts`/api contents.
current_entry_map = entryToPage;
const entryPoints = [...entryToPage.keys()].map(
(e) => `ssr-virtual:${e}`,
);
try {
writeFileSync(
path.join(BUNX_TMP_DIR, "ssr-entry-to-page.json"),
JSON.stringify(Object(entryToPage), null, 4),
);
writeFileSync(
path.join(BUNX_TMP_DIR, "ssr-entrypoints.json"),
JSON.stringify(entryPoints, null, 4),
);
} catch (error) {}
const structure_changed =
!global.BUNEXT_SSR_BUNDLER_CTX ||
global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED ||
force ||
!last_entry_keys ||
!arraysEqual(last_entry_keys, entry_keys) ||
last_root_file_path !== root_file_path;
if (structure_changed) {
try {
await global.BUNEXT_SSR_BUNDLER_CTX?.dispose();
} catch (error) {}
global.BUNEXT_SSR_BUNDLER_CTX = undefined;
global.BUNEXT_SSR_BUNDLER_CTX = await esbuild.context({
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({
getEntryToPage: () => current_entry_map,
}),
ssrCTXArtifactTracker({
entryToPage,
}),
],
jsx: "automatic",
external: [
"react",
"react-dom",
"react/jsx-runtime",
"react/jsx-dev-runtime",
"bun:*",
"bun",
"sqlite-vec",
"better-sqlite3",
...(config.ssr_compiler_excludes || []),
],
splitting: true,
logLevel: "silent",
});
}
await global.BUNEXT_SSR_BUNDLER_CTX!.rebuild();
// Only record the entry set after a successful build so a failed
// build retries with a fresh context next time.
last_entry_keys = entry_keys;
last_root_file_path = root_file_path;
global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED = false;
global.BUNEXT_BUILD_FAILED = false;
await cleanupArtifacts({ paradigm: "ssr" });
// esbuild has now replaced superseded outputs on disk; release the
// ones the server already imported.
evictDeletedModules(BUNX_CWD_MODULE_CACHE_DIR);
} catch (error: any) {
global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED = true;
global.BUNEXT_BUILD_FAILED = true;
// esbuild errors are reported (deduped) by the ssr artifact tracker's
// onEnd; only re-log unexpected failures.
if (!String(error?.message || "").startsWith("Build failed with")) {
logBuildErrorsOnce(
[
{
text: `SSR Bundler Error: ${
error?.message || String(error)
}`,
},
],
(line) => log.error(line),
);
}
}
}