Switch back to esbuild

This commit is contained in:
2026-03-24 18:28:56 +01:00
parent 976ff5fec9
commit 47c262392c
25 changed files with 315 additions and 153 deletions
+19 -28
View File
@@ -7,40 +7,25 @@ import tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin";
import grabClientHydrationScript from "./grab-client-hydration-script";
import grabArtifactsFromBundledResults from "./grab-artifacts-from-bundled-result";
import { writeFileSync } from "fs";
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
import path from "path";
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE, BUNX_HYDRATION_SRC_DIR, } = grabDirNames();
let build_starts = 0;
const MAX_BUILD_STARTS = 10;
export default async function allPagesESBuildContextBundler(params) {
const pages = grabAllPages({ exclude_api: true });
global.PAGE_FILES = pages;
const virtualEntries = {};
const dev = isDevelopment();
const entryToPage = new Map();
for (const page of pages) {
const key = page.transformed_path;
const txt = await grabClientHydrationScript({
page_local_path: page.local_path,
});
// if (page.url_path == "/index") {
// console.log("txt", txt);
// }
if (!txt)
continue;
virtualEntries[key] = txt;
const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`);
await Bun.write(entryFile, txt, { createPath: true });
entryToPage.set(path.resolve(entryFile), page);
}
const virtualPlugin = {
name: "virtual-entrypoints",
setup(build) {
build.onResolve({ filter: /^virtual:/ }, (args) => ({
path: args.path.replace("virtual:", ""),
namespace: "virtual",
}));
build.onLoad({ filter: /.*/, namespace: "virtual" }, (args) => ({
contents: virtualEntries[args.path],
loader: "tsx",
resolveDir: process.cwd(),
}));
},
};
let buildStart = 0;
const artifactTracker = {
name: "artifact-tracker",
@@ -66,8 +51,8 @@ export default async function allPagesESBuildContextBundler(params) {
return;
}
const artifacts = grabArtifactsFromBundledResults({
pages,
result,
entryToPage,
});
if (artifacts?.[0] && artifacts.length > 0) {
for (let i = 0; i < artifacts.length; i++) {
@@ -77,8 +62,11 @@ export default async function allPagesESBuildContextBundler(params) {
artifact;
}
}
params?.post_build_fn?.({ artifacts });
writeFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, JSON.stringify(artifacts, null, 4));
// params?.post_build_fn?.({ artifacts });
// writeFileSync(
// HYDRATION_DST_DIR_MAP_JSON_FILE,
// JSON.stringify(artifacts, null, 4),
// );
}
const elapsed = (performance.now() - buildStart).toFixed(0);
log.success(`[Built] in ${elapsed}ms`);
@@ -87,12 +75,12 @@ export default async function allPagesESBuildContextBundler(params) {
});
},
};
const entryPoints = Object.keys(virtualEntries).map((k) => `virtual:${k}`);
const entryPoints = [...entryToPage.keys()];
const ctx = await esbuild.context({
entryPoints,
outdir: HYDRATION_DST_DIR,
bundle: true,
minify: true,
minify: !dev,
format: "esm",
target: "es2020",
platform: "browser",
@@ -101,7 +89,7 @@ export default async function allPagesESBuildContextBundler(params) {
},
entryNames: "[dir]/[hash]",
metafile: true,
plugins: [tailwindEsbuildPlugin, virtualPlugin, artifactTracker],
plugins: [tailwindEsbuildPlugin, artifactTracker],
jsx: "automatic",
splitting: true,
// logLevel: "silent",
@@ -113,5 +101,8 @@ export default async function allPagesESBuildContextBundler(params) {
],
});
await ctx.rebuild();
// global.BUNDLER_CTX = ctx;
// if (params?.watch) {
// await ctx.watch();
// }
global.BUNDLER_CTX = ctx;
}