Fix watcher rebuild on page add and delete
This commit is contained in:
@@ -32,6 +32,17 @@ type HydrationVirtualEntry = PageFiles & { tsx: string };
|
||||
// be recreated (entry points are fixed at context creation).
|
||||
let last_entry_keys: string[] | undefined = undefined;
|
||||
let last_root_file_path: string | undefined = undefined;
|
||||
// Local paths of the pages that made it into the current entry set. Pages
|
||||
// skipped at build time (e.g. created empty, no default export yet) are
|
||||
// absent, so the watcher can treat their later edits as structural.
|
||||
let last_entry_pages = new Set<string>();
|
||||
|
||||
/**
|
||||
* True when `local_path` is a hydration entry of the current esbuild context.
|
||||
*/
|
||||
export function isHydrationEntryPage(local_path: string): boolean {
|
||||
return last_entry_pages.has(local_path);
|
||||
}
|
||||
|
||||
function arraysEqual(a: string[], b: string[]): boolean {
|
||||
if (a.length !== b.length) return false;
|
||||
@@ -133,6 +144,9 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
|
||||
|
||||
last_entry_keys = entry_keys;
|
||||
last_root_file_path = root_file_path;
|
||||
last_entry_pages = new Set(
|
||||
[...entryToPage.values()].map((p) => p.local_path),
|
||||
);
|
||||
}
|
||||
|
||||
await global.BUNEXT_BUNDLER_CTX!.rebuild();
|
||||
|
||||
@@ -5,7 +5,10 @@ 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 allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
|
||||
import allPagesESBuildContextBundler, {
|
||||
isHydrationEntryPage,
|
||||
} from "../bundler/all-pages-esbuild-context-bundler";
|
||||
import grabAllPages from "../../utils/grab-all-pages";
|
||||
import serverPostBuildFn from "./server-post-build-fn";
|
||||
import { log } from "../../utils/log";
|
||||
import { logBuildErrorsOnce } from "../../utils/build-error-log";
|
||||
@@ -26,12 +29,28 @@ export function restartWatcher() {
|
||||
|
||||
if (Date.now() - last_watcher_restart < 1000) return;
|
||||
|
||||
// Files created between close() and the new watcher's initial scan
|
||||
// completing emit no `add` (ignoreInitial treats them as pre-existing).
|
||||
// Snapshot the page set so the new watcher can detect them on `ready`.
|
||||
const pages_snapshot = grabPagesSnapshot();
|
||||
|
||||
try {
|
||||
global.BUNEXT_PAGES_SRC_WATCHER.close();
|
||||
} catch (error) {}
|
||||
global.BUNEXT_PAGES_SRC_WATCHER = undefined;
|
||||
last_watcher_restart = Date.now();
|
||||
watcherEsbuildCTX();
|
||||
watcherEsbuildCTX({ pages_snapshot });
|
||||
}
|
||||
|
||||
function grabPagesSnapshot(): string | undefined {
|
||||
try {
|
||||
return grabAllPages()
|
||||
.map((p) => p.local_path)
|
||||
.sort()
|
||||
.join("\n");
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,6 +71,9 @@ const REBUILD_DEBOUNCE_MS = 200;
|
||||
let pending_structural = false;
|
||||
let pending_content_file: string | undefined = undefined;
|
||||
let debounce_timer: ReturnType<typeof setTimeout> | undefined = undefined;
|
||||
// Guards against overlapping runners: BUNEXT_RECOMPILING alone is not enough
|
||||
// since the artifact tracker clears it mid-run.
|
||||
let runner_active = false;
|
||||
|
||||
function scheduleRebuild() {
|
||||
if (debounce_timer) {
|
||||
@@ -59,6 +81,14 @@ function scheduleRebuild() {
|
||||
}
|
||||
debounce_timer = setTimeout(async () => {
|
||||
debounce_timer = undefined;
|
||||
|
||||
// Another rebuild (fullRebuild, or a runner already looping) owns the
|
||||
// bundler. Retry later rather than dropping the pending events.
|
||||
if (runner_active || global.BUNEXT_RECOMPILING) {
|
||||
scheduleRebuild();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await runRebuilds();
|
||||
} catch (error: any) {
|
||||
@@ -69,9 +99,6 @@ function scheduleRebuild() {
|
||||
(line) => log.error(line),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
global.BUNEXT_RECOMPILING = false;
|
||||
global.BUNEXT_IS_SERVER_COMPONENT = false;
|
||||
}
|
||||
}, REBUILD_DEBOUNCE_MS);
|
||||
}
|
||||
@@ -84,9 +111,7 @@ function scheduleRebuild() {
|
||||
* tracker then queues the single-flighted SSR rebuild and pushes HMR updates.
|
||||
*/
|
||||
async function runRebuilds() {
|
||||
if (global.BUNEXT_RECOMPILING) {
|
||||
return;
|
||||
}
|
||||
runner_active = true;
|
||||
global.BUNEXT_RECOMPILING = true;
|
||||
global.BUNEXT_IS_SERVER_COMPONENT = false;
|
||||
|
||||
@@ -140,6 +165,7 @@ async function runRebuilds() {
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
runner_active = false;
|
||||
global.BUNEXT_RECOMPILING = false;
|
||||
global.BUNEXT_IS_SERVER_COMPONENT = false;
|
||||
}
|
||||
@@ -193,7 +219,9 @@ function buildIgnoredPatterns() {
|
||||
return ignored;
|
||||
}
|
||||
|
||||
export default async function watcherEsbuildCTX() {
|
||||
export default async function watcherEsbuildCTX(params?: {
|
||||
pages_snapshot?: string;
|
||||
}) {
|
||||
const pages_src_watcher = chokidar.watch(ROOT_DIR, {
|
||||
ignored: buildIgnoredPatterns(),
|
||||
persistent: true,
|
||||
@@ -205,6 +233,17 @@ export default async function watcherEsbuildCTX() {
|
||||
(event, filePath) => handleEvent(event, filePath),
|
||||
);
|
||||
|
||||
const pages_snapshot = params?.pages_snapshot;
|
||||
if (pages_snapshot !== undefined) {
|
||||
pages_src_watcher.on("ready", () => {
|
||||
const current = grabPagesSnapshot();
|
||||
if (current !== undefined && current !== pages_snapshot) {
|
||||
pending_structural = true;
|
||||
scheduleRebuild();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
global.BUNEXT_PAGES_SRC_WATCHER = pages_src_watcher as any;
|
||||
}
|
||||
|
||||
@@ -252,10 +291,24 @@ function handleEvent(
|
||||
}
|
||||
|
||||
if (event === "change") {
|
||||
if (filename.match(TARGET_FILES_MATCH)) {
|
||||
pending_content_file = full_file_path;
|
||||
scheduleRebuild();
|
||||
if (!filename.match(TARGET_FILES_MATCH)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// A page that isn't a hydration entry yet (e.g. created empty,
|
||||
// then given its default export) needs the entry set recomputed;
|
||||
// an incremental rebuild of the existing context would miss it.
|
||||
if (
|
||||
filename.match(/^src\/pages\//) &&
|
||||
!filename.match(/^src\/pages\/api\/|\.server\.tsx?$/) &&
|
||||
!checkExcludedPatterns({ path: filename }) &&
|
||||
!isHydrationEntryPage(full_file_path)
|
||||
) {
|
||||
pending_structural = true;
|
||||
} else {
|
||||
pending_content_file = full_file_path;
|
||||
}
|
||||
scheduleRebuild();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user