Fix build lags
This commit is contained in:
@@ -8,29 +8,77 @@ import grabPageReactComponentString from "../server/web-pages/grab-page-react-co
|
||||
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 { 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 } = grabDirNames();
|
||||
const { BUNX_CWD_MODULE_CACHE_DIR, BUNX_TMP_DIR } = grabDirNames();
|
||||
|
||||
type Params = {
|
||||
post_build_fn?: (params: { artifacts: any[] }) => Promise<void> | void;
|
||||
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;
|
||||
};
|
||||
|
||||
export default async function pagesSSRContextBundler(params?: Params) {
|
||||
const pages = grabAllPages();
|
||||
const dev = isDevelopment();
|
||||
|
||||
if (global.BUNEXT_SSR_BUNDLER_CTX) {
|
||||
await global.BUNEXT_SSR_BUNDLER_CTX.dispose();
|
||||
global.BUNEXT_SSR_BUNDLER_CTX = undefined;
|
||||
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;
|
||||
}
|
||||
|
||||
const entryToPage = new Map<string, PageFiles & { tsx: string }>();
|
||||
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\//)) {
|
||||
if (
|
||||
page.local_path.match(/\/pages\/api\//) ||
|
||||
page.local_path.match(/\.server\.tsx?$/)
|
||||
) {
|
||||
const ts = await Bun.file(page.local_path).text();
|
||||
entryToPage.set(page.local_path, { ...page, tsx: ts });
|
||||
if (
|
||||
ts.match(
|
||||
/(export default)|(export \w+ handler)|(export \w+ server)/,
|
||||
)
|
||||
) {
|
||||
entryToPage.set(page.local_path, { ...page, tsx: ts });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -40,47 +88,155 @@ export default async function pagesSSRContextBundler(params?: Params) {
|
||||
});
|
||||
|
||||
if (!tsx) continue;
|
||||
if (!tsx.match(/export default/)) continue;
|
||||
|
||||
entryToPage.set(page.local_path, { ...page, tsx });
|
||||
}
|
||||
|
||||
const entryPoints = [...entryToPage.keys()].map((e) => `ssr-virtual:${e}`);
|
||||
return { entryToPage, root_file_path };
|
||||
}
|
||||
|
||||
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({
|
||||
entryToPage,
|
||||
}),
|
||||
ssrCTXArtifactTracker({
|
||||
entryToPage,
|
||||
post_build_fn: params?.post_build_fn,
|
||||
}),
|
||||
],
|
||||
jsx: "automatic",
|
||||
external: [
|
||||
"react",
|
||||
"react-dom",
|
||||
"react/jsx-runtime",
|
||||
"react/jsx-dev-runtime",
|
||||
"bun:*",
|
||||
],
|
||||
// logLevel: "silent",
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
});
|
||||
|
||||
await global.BUNEXT_SSR_BUNDLER_CTX.rebuild();
|
||||
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" });
|
||||
} 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),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user