Fix build lags

This commit is contained in:
2026-10-02 14:48:38 +01:00
parent 17a5c3df01
commit 8fa5b64b48
38 changed files with 1390 additions and 1238 deletions
@@ -1,161 +0,0 @@
import chokidar from "chokidar";
import path from "path";
import { existsSync } from "fs";
import grabDirNames from "../../utils/grab-dir-names";
import fullRebuild from "./full-rebuild";
import { AppData } from "../../data/app-data";
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
import { log } from "../../utils/log";
const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
export default async function chokadirWatcherEsbuildCTX() {
const watcher = chokidar.watch(ROOT_DIR, {
ignored: [
/(^|[\/\\])\../,
/node_modules/,
/public/,
/\.bunext/,
/\.git/,
/dist/,
/bun\.lockb/,
(path: string) => path.endsWith(AppData["BunextTmpFileExt"]),
],
persistent: true,
ignoreInitial: true,
depth: 99,
});
const handleEvent = async (
event: "add" | "change" | "unlink" | "addDir" | "unlinkDir",
filePath: string,
) => {
let owns_recompile = false;
try {
const filename = path.relative(ROOT_DIR, filePath);
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
await fullRebuild();
return;
}
if (global.BUNEXT_BUNDLER_CTX_DISPOSED) {
await fullRebuild({ msg: `Restarting Bundler ...` });
}
if (global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED) {
await pagesSSRBundler().catch((error) => {
log.error(`SSR Bundler Error: ${error}`);
});
}
if (filename.match(/\/styles$/) || filename === "styles") {
owns_recompile = true;
global.BUNEXT_RECOMPILING = true;
await Bun.sleep(1000);
await fullRebuild({
msg: `Detected new \`styles\` directory. Rebuilding ...`,
});
return;
}
if (filename.match(/bunext.config\.ts/)) {
await fullRebuild({
msg: `bunext.config.ts file changed. Rebuilding server ...`,
});
return;
}
const target_files_match = /\.(tsx?|jsx?|css)$/;
if (event === "change") {
if (filename.match(target_files_match)) {
if (global.BUNEXT_RECOMPILING) return;
owns_recompile = true;
global.BUNEXT_RECOMPILING = true;
if (filename.match(/.*\.server\.tsx?/)) {
global.BUNEXT_IS_SERVER_COMPONENT = true;
}
if (global.BUNEXT_BUNDLER_CTX) {
await global.BUNEXT_BUNDLER_CTX.rebuild();
}
if (filename.match(/(404|500)\.tsx?/)) {
for (
let i = global.BUNEXT_HMR_CONTROLLERS.length - 1;
i >= 0;
i--
) {
const controller = global.BUNEXT_HMR_CONTROLLERS[i];
try {
controller?.controller?.enqueue(
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
);
} catch {
global.BUNEXT_HMR_CONTROLLERS.splice(i, 1);
}
}
}
}
return;
}
if (["add", "unlink", "addDir", "unlinkDir"].includes(event)) {
const is_file_of_interest =
!!filename.match(target_files_match) ||
event.includes("Dir");
if (!is_file_of_interest) return;
if (
!filename.match(/^src\/pages\/|\.css$/) ||
checkExcludedPatterns({ path: filename }) ||
filename.includes(" ")
) {
return reloadWatcher();
}
if (global.BUNEXT_RECOMPILING) return;
owns_recompile = true;
const action = event.startsWith("add") ? "created" : "deleted";
const type = filename.match(/\.css$/)
? "Stylesheet"
: event.includes("Dir")
? "Directory"
: filename.match(/\/pages\/api\//)
? "API Route"
: "Page";
await fullRebuild({
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
});
}
} catch (error) {
log.error(`Watcher rebuild failed: ${error}`);
} finally {
if (owns_recompile) {
global.BUNEXT_RECOMPILING = false;
global.BUNEXT_IS_SERVER_COMPONENT = false;
}
}
};
watcher
.on("add", (path) => handleEvent("add", path))
.on("change", (path) => handleEvent("change", path))
.on("unlink", (path) => handleEvent("unlink", path))
.on("addDir", (path) => handleEvent("addDir", path))
.on("unlinkDir", (path) => handleEvent("unlinkDir", path));
}
function reloadWatcher() {
if (global.BUNEXT_PAGES_SRC_WATCHER) {
global.BUNEXT_PAGES_SRC_WATCHER.close();
chokadirWatcherEsbuildCTX();
}
}
+93 -33
View File
@@ -2,51 +2,111 @@ import { log } from "../../utils/log";
import path from "path";
import grabDirNames from "../../utils/grab-dir-names";
import { existsSync, readdirSync, statSync, unlinkSync } from "fs";
import type { BundlerCTXMap } from "../../types";
type Params = {
new_artifacts: BundlerCTXMap[];
paradigm: "hydration" | "ssr";
};
const { ROOT_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE_NAME } = grabDirNames();
const { ROOT_DIR, HYDRATION_DST_DIR, BUNX_CWD_MODULE_CACHE_DIR } =
grabDirNames();
// esbuild's content hashes are 8-char base64-ish (e.g. `CMSI7VSQ`). Entry
// outputs are `<dir>/<hash>.js` / `<dir>/<hash>.css`. Shared chunks are
// `chunk-*.js` and MUST be kept: any current entry may still reference an
// older chunk (esbuild reuses unchanged chunks across rebuilds), and
// verifying references would require parsing every entry's imports.
const ENTRY_HASH_FILE = /^[A-Za-z0-9]{8}\.(js|css)$/;
/**
* Deletes orphaned entry outputs left behind by previous rebuilds.
*
* After a rebuild the page/SSR artifact maps only reference the newest
* hashed entry files; every previously hashed entry for the same directory
* is provably unreferenced (generated HTML always uses the current map) and
* is removed. Chunks, non-hashed fallback module files and `map.json` are
* left untouched.
*/
export default async function cleanupArtifacts({ paradigm }: Params) {
const is_hydration = paradigm === "hydration";
const root_dir = is_hydration
? HYDRATION_DST_DIR
: BUNX_CWD_MODULE_CACHE_DIR;
const map = is_hydration
? global.BUNEXT_BUNDLER_CTX_MAP
: global.BUNEXT_SSR_BUNDLER_CTX_MAP;
if (!map || !existsSync(root_dir)) {
return;
}
export default async function cleanupArtifacts({ new_artifacts }: Params) {
try {
for (let i = 0; i < new_artifacts.length; i++) {
const new_artifact = new_artifacts[i];
const keep_by_dir = new Map<string, Set<string>>();
const artifact_public_dir = path.dirname(
path.join(ROOT_DIR, new_artifact.path),
);
for (const entry of Object.values(map)) {
if (!entry?.path) continue;
const dir_content = readdirSync(artifact_public_dir);
const full_path = path.join(ROOT_DIR, entry.path);
const dir_path = path.dirname(full_path);
let keep = keep_by_dir.get(dir_path);
if (!keep) {
keep = new Set<string>();
keep_by_dir.set(dir_path, keep);
}
for (let d = 0; d < dir_content.length; d++) {
const dir_or_file = dir_content[d];
const full_path = path.join(artifact_public_dir, dir_or_file);
keep.add(path.basename(full_path));
const file_or_path_stats = statSync(full_path);
if (
file_or_path_stats.isDirectory() ||
dir_or_file == HYDRATION_DST_DIR_MAP_JSON_FILE_NAME
) {
continue;
}
if (
new_artifact.path.includes(dir_or_file) ||
new_artifact.css_path?.includes(dir_or_file)
) {
continue;
}
if (existsSync(full_path)) {
unlinkSync(full_path);
}
if (entry.css_path) {
keep.add(path.basename(entry.css_path));
}
}
sweepDir(root_dir, keep_by_dir);
} catch (error: any) {
log.error(error);
log.error(`Cleanup Artifacts Error: ${error?.message || error}`);
}
}
function sweepDir(
dir_path: string,
keep_by_dir: Map<string, Set<string>>,
): void {
let entries: string[];
try {
entries = readdirSync(dir_path);
} catch (error) {
return;
}
for (const name of entries) {
const full_path = path.join(dir_path, name);
let is_dir = false;
try {
is_dir = statSync(full_path).isDirectory();
} catch (error) {
continue;
}
if (is_dir) {
sweepDir(full_path, keep_by_dir);
continue;
}
if (!name.match(ENTRY_HASH_FILE)) {
continue;
}
if (name.startsWith("chunk-")) {
continue;
}
const keep = keep_by_dir.get(path.dirname(full_path));
if (keep?.has(name)) {
continue;
}
try {
unlinkSync(full_path);
} catch (error) {}
}
}
+2 -2
View File
@@ -51,8 +51,8 @@ async function doFullRebuild(params?: { msg?: string }) {
} catch (error) {}
await allPagesESBuildContextBundler({
post_build_fn: async () => {
await serverPostBuildFn();
post_build_fn: async (params) => {
await serverPostBuildFn(params);
},
});
} catch (error: any) {
@@ -4,6 +4,15 @@ import grabPageComponent from "./web-pages/grab-page-component";
type Params = {
reload_all_controllers?: boolean;
/**
* Relative page paths whose client entry outputs changed in the last
* build. When provided with at least one entry, only controllers
* targeting those pages are re-rendered/re-pushed. When empty or
* omitted, every controller is refreshed (covers server-file-only
* changes, where hydration outputs are untouched but server props must
* still be re-fetched).
*/
changed_pages?: string[];
};
export default async function serverPostBuildFn(params?: Params) {
@@ -11,6 +20,11 @@ export default async function serverPostBuildFn(params?: Params) {
return;
}
const changed_set =
params?.changed_pages && params.changed_pages.length > 0
? new Set(params.changed_pages)
: undefined;
const reload_payload = { reload: true };
const reload_enqueue = `event: update\ndata: ${JSON.stringify(reload_payload)}\n\n`;
@@ -25,6 +39,11 @@ export default async function serverPostBuildFn(params?: Params) {
continue;
}
// Only touch controllers whose page actually changed.
if (changed_set && !changed_set.has(controller.target_map.local_path)) {
continue;
}
if (params?.reload_all_controllers) {
try {
controller.controller.enqueue(reload_enqueue);
+253 -181
View File
@@ -1,10 +1,12 @@
import { watch, existsSync, statSync, glob } from "fs";
import chokidar from "chokidar";
import path from "path";
import { existsSync } from "fs";
import grabDirNames from "../../utils/grab-dir-names";
import fullRebuild from "./full-rebuild";
import { AppData } from "../../data/app-data";
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "./server-post-build-fn";
import { log } from "../../utils/log";
import { logBuildErrorsOnce } from "../../utils/build-error-log";
@@ -24,7 +26,9 @@ export function restartWatcher() {
if (Date.now() - last_watcher_restart < 1000) return;
global.BUNEXT_PAGES_SRC_WATCHER.close();
try {
global.BUNEXT_PAGES_SRC_WATCHER.close();
} catch (error) {}
global.BUNEXT_PAGES_SRC_WATCHER = undefined;
last_watcher_restart = Date.now();
watcherEsbuildCTX();
@@ -38,196 +42,264 @@ export function watcherRestartAvailable() {
return Date.now() - last_watcher_restart > 6000;
}
export default async function watcherEsbuildCTX() {
const pages_src_watcher = watch(
ROOT_DIR,
{
recursive: true,
persistent: true,
},
async (event, filename) => {
let owns_recompile = false;
const TARGET_FILES_MATCH = /\.(tsx?|jsx?|css)$/;
try {
if (!filename) return;
const full_file_path = path.join(ROOT_DIR, filename);
// Rebuild coalescing: editors emit bursts of events per save (change +
// atomic-swap noise), and edits can land while a rebuild is running.
// Events are debounced into a single runner; the runner loops until the
// event queue drains, so a mid-build edit is never silently dropped.
const REBUILD_DEBOUNCE_MS = 200;
let pending_structural = false;
let pending_content_file: string | undefined = undefined;
let debounce_timer: ReturnType<typeof setTimeout> | undefined = undefined;
if (global.BUNEXT_CONFIG.exclude_watch_patterns) {
for (
let i = 0;
i < global.BUNEXT_CONFIG.exclude_watch_patterns.length;
i++
) {
const watch_pattern =
global.BUNEXT_CONFIG.exclude_watch_patterns[i];
function scheduleRebuild() {
if (debounce_timer) {
clearTimeout(debounce_timer);
}
debounce_timer = setTimeout(async () => {
debounce_timer = undefined;
try {
await runRebuilds();
} catch (error: any) {
const msg = String(error?.message || error || "");
if (!msg.startsWith("Build failed with")) {
logBuildErrorsOnce(
[{ text: `Watcher rebuild failed: ${msg}` }],
(line) => log.error(line),
);
}
} finally {
global.BUNEXT_RECOMPILING = false;
global.BUNEXT_IS_SERVER_COMPONENT = false;
}
}, REBUILD_DEBOUNCE_MS);
}
if (watch_pattern instanceof RegExp) {
const is_path_excluded =
watch_pattern.test(filename);
if (is_path_excluded) {
return;
}
} else {
const excluded_path = path.resolve(
ROOT_DIR,
watch_pattern,
);
/**
* Processes the pending event batch. Structural changes go through the
* all-pages bundler (which re-creates esbuild contexts only when the entry
* set really changed, so atomic-swap saves resolve to incremental rebuilds).
* Content changes rebuild the hydration context incrementally; its artifact
* tracker then queues the single-flighted SSR rebuild and pushes HMR updates.
*/
async function runRebuilds() {
if (global.BUNEXT_RECOMPILING) {
return;
}
global.BUNEXT_RECOMPILING = true;
global.BUNEXT_IS_SERVER_COMPONENT = false;
if (excluded_path == full_file_path) {
return;
}
}
}
}
try {
while (true) {
const structural = pending_structural;
const content_file = pending_content_file;
pending_structural = false;
pending_content_file = undefined;
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
await fullRebuild();
return;
}
if (!structural && !content_file) {
break;
}
if (filename.match(/^\.\w+/)) {
return;
}
if (global.BUNEXT_BUNDLER_CTX_DISPOSED) {
await fullRebuild({ msg: `Restarting Bundler ...` });
return;
}
if (global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED) {
await pagesSSRBundler().catch((error) => {
log.error(`SSR Bundler Error: ${error}`);
});
}
if (filename.endsWith(AppData["BunextTmpFileExt"])) {
return;
}
const does_file_exist = existsSync(full_file_path);
const file_stat = does_file_exist
? statSync(full_file_path)
: undefined;
if (full_file_path.match(/\/styles$/)) {
owns_recompile = true;
global.BUNEXT_RECOMPILING = true;
await Bun.sleep(1000);
await fullRebuild({
msg: `Detected new \`styles\` directory. Rebuilding ...`,
});
return;
}
const excluded_match =
/node_modules\/|^public\/|^\.bunext\/|^\.git\/|^\.?dist\/|bun\.lockb$/;
if (filename.match(excluded_match)) return;
if (filename.match(/bunext.config\.ts/)) {
await fullRebuild({
msg: `bunext.config.ts file changed. Rebuilding server ...`,
});
return;
}
const target_files_match = /\.(tsx?|jsx?|css)$/;
if (event !== "rename") {
if (filename.match(target_files_match)) {
if (global.BUNEXT_RECOMPILING) return;
owns_recompile = true;
global.BUNEXT_RECOMPILING = true;
if (filename.match(/.*\.server\.tsx?/)) {
global.BUNEXT_IS_SERVER_COMPONENT = true;
}
if (
global.BUNEXT_BUNDLER_CTX &&
!global.BUNEXT_BUNDLER_CTX_DISPOSED
) {
await global.BUNEXT_BUNDLER_CTX.rebuild();
} else if (global.BUNEXT_BUNDLER_CTX_DISPOSED) {
await fullRebuild({
msg: `Restarting Bundler ...`,
});
return;
}
if (filename.match(/(404|500)\.tsx?/)) {
for (
let i =
global.BUNEXT_HMR_CONTROLLERS.length - 1;
i >= 0;
i--
) {
const controller =
global.BUNEXT_HMR_CONTROLLERS[i];
try {
controller?.controller?.enqueue(
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
);
} catch {
global.BUNEXT_HMR_CONTROLLERS.splice(i, 1);
}
}
}
}
return;
}
const is_file_of_interest =
Boolean(filename.match(target_files_match)) ||
file_stat?.isDirectory();
if (!is_file_of_interest) {
return;
}
if (!filename.match(/^src\/pages\/|\.css$/))
return reloadWatcher();
if (checkExcludedPatterns({ path: filename }))
return reloadWatcher();
if (filename.match(/ /)) return reloadWatcher();
if (global.BUNEXT_RECOMPILING) return;
owns_recompile = true;
const action = does_file_exist ? "created" : "deleted";
const type = filename.match(/\.css$/)
? "Sylesheet"
: file_stat?.isDirectory()
? "Directory"
: filename.match(/\/pages\/api\//)
? "API Route"
: "Page";
await fullRebuild({
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
if (structural) {
global.BUNEXT_ROUTER.reload();
await allPagesESBuildContextBundler({
post_build_fn: async (params) => {
await serverPostBuildFn(params);
},
});
} catch (error: any) {
// A "Build failed with N errors" rejection is the same
// failure the tracker's onEnd already reported (deduped).
const msg = String(error?.message || error || "");
if (!msg.startsWith("Build failed with")) {
logBuildErrorsOnce(
[{ text: `Watcher rebuild failed: ${msg}` }],
(line) => log.error(line),
);
} else if (content_file) {
if (
global.BUNEXT_BUNDLER_CTX &&
!global.BUNEXT_BUNDLER_CTX_DISPOSED
) {
await global.BUNEXT_BUNDLER_CTX.rebuild();
} else if (global.BUNEXT_BUNDLER_CTX_DISPOSED) {
await fullRebuild({
msg: `Restarting Bundler ...`,
});
break;
}
} finally {
if (owns_recompile) {
global.BUNEXT_RECOMPILING = false;
global.BUNEXT_IS_SERVER_COMPONENT = false;
if (content_file.match(/(404|500)\.tsx?$/)) {
for (
let i = global.BUNEXT_HMR_CONTROLLERS.length - 1;
i >= 0;
i--
) {
const controller = global.BUNEXT_HMR_CONTROLLERS[i];
try {
controller?.controller?.enqueue(
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
);
} catch {
global.BUNEXT_HMR_CONTROLLERS.splice(i, 1);
}
}
}
}
},
}
} finally {
global.BUNEXT_RECOMPILING = false;
global.BUNEXT_IS_SERVER_COMPONENT = false;
}
}
function isPathExcluded(filename: string, full_file_path: string): boolean {
const user_patterns = global.BUNEXT_CONFIG?.exclude_watch_patterns;
if (!user_patterns) return false;
for (let i = 0; i < user_patterns.length; i++) {
const watch_pattern = user_patterns[i];
if (watch_pattern instanceof RegExp) {
if (watch_pattern.test(filename)) return true;
} else {
const excluded_path = path.resolve(ROOT_DIR, watch_pattern);
if (excluded_path == full_file_path) return true;
}
}
return false;
}
// Heavy directories are filtered at the watcher level so inotify doesn't
// install watches (and deliver events) for trees the bundler never reads.
function buildIgnoredPatterns() {
const ignored: Array<RegExp | ((p: string) => boolean)> = [
/(^|[\/\\])\../,
/(^|[\/\\])node_modules([\/\\]|$)/,
/(^|[\/\\])public([\/\\]|$)/,
/(^|[\/\\])\.bunext([\/\\]|$)/,
/(^|[\/\\])\.git([\/\\]|$)/,
/(^|[\/\\])dist([\/\\]|$)/,
/bun\.lockb$/,
(p: string) => p.endsWith(AppData["BunextTmpFileExt"]),
];
const user_patterns = global.BUNEXT_CONFIG?.exclude_watch_patterns;
if (user_patterns) {
for (let i = 0; i < user_patterns.length; i++) {
const pattern = user_patterns[i];
if (pattern instanceof RegExp) {
ignored.push((p: string) =>
pattern.test(path.relative(ROOT_DIR, p)),
);
} else {
const excluded_path = path.resolve(ROOT_DIR, pattern);
ignored.push((p: string) => p === excluded_path);
}
}
}
return ignored;
}
export default async function watcherEsbuildCTX() {
const pages_src_watcher = chokidar.watch(ROOT_DIR, {
ignored: buildIgnoredPatterns(),
persistent: true,
ignoreInitial: true,
});
watcherHandlers(
pages_src_watcher,
(event, filePath) => handleEvent(event, filePath),
);
global.BUNEXT_PAGES_SRC_WATCHER = pages_src_watcher;
global.BUNEXT_PAGES_SRC_WATCHER = pages_src_watcher as any;
}
function reloadWatcher() {
restartWatcher();
function handleEvent(
event: "add" | "change" | "unlink" | "addDir" | "unlinkDir",
filePath: string,
) {
try {
const filename = path.relative(ROOT_DIR, filePath);
const full_file_path = filePath;
if (isPathExcluded(filename, full_file_path)) {
return;
}
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
fullRebuild();
return;
}
if (filename.endsWith(AppData["BunextTmpFileExt"])) {
return;
}
if (full_file_path.match(/\/styles$/)) {
pending_structural = false;
pending_content_file = undefined;
if (debounce_timer) clearTimeout(debounce_timer);
setTimeout(() => {
fullRebuild({
msg: `Detected new \`styles\` directory. Rebuilding ...`,
});
}, 1000);
return;
}
if (filename.match(/bunext\.config\.ts/)) {
pending_structural = false;
pending_content_file = undefined;
if (debounce_timer) clearTimeout(debounce_timer);
fullRebuild({
msg: `bunext.config.ts file changed. Rebuilding server ...`,
});
return;
}
if (event === "change") {
if (filename.match(TARGET_FILES_MATCH)) {
pending_content_file = full_file_path;
scheduleRebuild();
}
return;
}
// add / unlink / addDir / unlinkDir
const is_dir_event = event === "addDir" || event === "unlinkDir";
const is_file_of_interest =
Boolean(filename.match(TARGET_FILES_MATCH)) || is_dir_event;
if (!is_file_of_interest) {
return;
}
// Only page routes and stylesheets are structural; anything else
// (e.g. a component being added) is covered by the next content
// rebuild and needs no context recreation.
if (
!filename.match(/^src\/pages\/|\.css$/) ||
checkExcludedPatterns({ path: filename }) ||
filename.match(/ /)
) {
return;
}
pending_structural = true;
scheduleRebuild();
} catch (error: any) {
logBuildErrorsOnce(
[{ text: `Watcher error: ${error?.message || error}` }],
(line) => log.error(line),
);
}
}
function watcherHandlers(
watcher: ReturnType<typeof chokidar.watch>,
handleEvent: (
event: "add" | "change" | "unlink" | "addDir" | "unlinkDir",
filePath: string,
) => void,
) {
watcher
.on("add", (p) => handleEvent("add", p))
.on("change", (p) => handleEvent("change", p))
.on("unlink", (p) => handleEvent("unlink", p))
.on("addDir", (p) => handleEvent("addDir", p))
.on("unlinkDir", (p) => handleEvent("unlinkDir", p));
}
@@ -34,6 +34,15 @@ export default async function handleWebPages({
}
}
// The dev server port opens before the one-time startup build
// finishes. First page render waits for it so we never serve HTML
// against half-built hydration/SSR modules.
if (is_dev && global.BUNEXT_INITIAL_BUILD_PROMISE) {
try {
await global.BUNEXT_INITIAL_BUILD_PROMISE;
} catch (error) {}
}
const componentRes = await grabPageComponent({
req,
});