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 esbuildCTXArtifactTracker from "./plugins/esbuild-ctx-artifact-tracker"; const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames(); export default async function allPagesESBuildContextBundlerFiles(params) { const pages = grabAllPages({ exclude_api: true }); global.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, tsx, { createPath: true }); entryToPage.set(entryFile, { ...page, tsx }); } const entryPoints = [...entryToPage.keys()]; const 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: [ tailwindEsbuildPlugin, esbuildCTXArtifactTracker({ entryToPage, post_build_fn: params?.post_build_fn, }), ], jsx: "automatic", splitting: true, logLevel: "silent", external: [ "react", "react-dom", "react-dom/client", "react/jsx-runtime", ], }); await ctx.rebuild(); global.BUNDLER_CTX = ctx; }