Files
bunext/dist/functions/bundler/all-pages-esbuild-context-bundler.js
T
2026-10-02 07:04:22 +01:00

108 lines
4.2 KiB
JavaScript

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 grabClientHydrationScript from "./grab-client-hydration-script";
import path from "path";
import virtualFilesPlugin from "./plugins/virtual-files-plugin";
import esbuildCTXArtifactTracker from "./plugins/esbuild-ctx-artifact-tracker";
import { logBuildErrorsOnce, } from "../../utils/build-error-log";
import { log } from "../../utils/log";
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames();
export default async function allPagesESBuildContextBundler(params) {
const _t0 = Date.now();
try {
const pages = grabAllPages({ exclude_api: true });
global.BUNEXT_PAGE_FILES = pages;
const dev = isDevelopment();
const entryToPage = new Map();
for (const page of pages) {
const tsx = await grabClientHydrationScript({
page_local_path: page.local_path,
});
if (!tsx) {
continue;
}
const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`);
// await Bun.write(entryFile, txt, { createPath: true });
entryToPage.set(entryFile, { ...page, tsx });
}
const entryPoints = [...entryToPage.keys()].map((e) => `hydration-virtual:${e}`);
// This is a brand-new context: wipe the previous context's failure
// history so the "2 failed starts => dispose" guard starts fresh.
global.BUNEXT_MAIN_CTX_BUILD_STARTS = 0;
global.BUNEXT_BUNDLER_CTX = await esbuild.context({
entryPoints,
outdir: HYDRATION_DST_DIR,
bundle: true,
minify: !dev,
format: "esm",
target: "es2020",
platform: "browser",
define: {
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
},
entryNames: "[dir]/[hash]",
metafile: true,
plugins: [
forceExternalReact(),
tailwindEsbuildPlugin,
virtualFilesPlugin({
entryToPage,
}),
esbuildCTXArtifactTracker({
entryToPage,
post_build_fn: params?.post_build_fn,
build_only: params?.build_only || params?.start,
}),
],
jsx: "automatic",
splitting: true,
treeShaking: true,
external: [
"react",
"react-dom",
"react-dom/client",
"react/jsx-runtime",
"react/jsx-dev-runtime",
...(global.BUNEXT_CONFIG.page_compiler_excludes || []),
],
// esbuild's own error logging prints per attempt; the
// artifact-tracker plugin reports errors (deduped, once) itself.
logLevel: "silent",
});
await global.BUNEXT_BUNDLER_CTX.rebuild();
}
catch (error) {
global.BUNEXT_BUILD_FAILED = true;
// When the build fails with esbuild errors, the artifact-tracker's
// onEnd already reported them (deduped). "Build failed with N errors"
// is that same failure rethrown by esbuild's API — re-logging it here
// would double every incident.
if (String(error?.message || "").startsWith("Build failed with")) {
return;
}
logBuildErrorsOnce([
{
text: `ESBUILD Error => ${error?.message || String(error)}`,
},
], (line) => log.error(line));
}
}
function forceExternalReact() {
return {
name: "force-external-react",
setup(build) {
build.onResolve({ filter: /^react(-dom)?(\/.*)?$/ }, (args) => {
if (args.pluginData?.externalReact)
return null;
return {
path: args.path,
external: true,
};
});
},
};
}