Compare commits
3
Commits
1d0ac4aa80
...
a19863b3e9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a19863b3e9 | ||
|
|
a9cd51d71c | ||
|
|
e3a0f5fbeb |
Vendored
+13
-2
@@ -4,11 +4,22 @@ import bunextInit from "../../functions/bunext-init";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import { rmSync } from "fs";
|
||||
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
|
||||
process.on("uncaughtException", (error) => {
|
||||
log.error(`Uncaught exception: ${error}`);
|
||||
});
|
||||
process.on("unhandledRejection", (reason) => {
|
||||
log.error(`Unhandled rejection: ${reason}`);
|
||||
});
|
||||
log.info("Running development server ...");
|
||||
try {
|
||||
rmSync(HYDRATION_DST_DIR, { recursive: true });
|
||||
rmSync(BUNX_CWD_PAGES_REWRITE_DIR, { recursive: true });
|
||||
}
|
||||
catch (error) { }
|
||||
await bunextInit();
|
||||
await startServer();
|
||||
try {
|
||||
await bunextInit();
|
||||
await startServer();
|
||||
}
|
||||
catch (error) {
|
||||
log.error(`Failed to start development server: ${error}`);
|
||||
}
|
||||
|
||||
+2
-2
@@ -4,8 +4,8 @@ export default async function buildOnstartErrorHandler(params) {
|
||||
if (global.BUNDLER_CTX_DISPOSED) {
|
||||
return;
|
||||
}
|
||||
console.log(`Killing Bundler ...`);
|
||||
console.log(`global.BUNDLER_CTX_DISPOSED`, global.BUNDLER_CTX_DISPOSED);
|
||||
// console.log(`Killing Bundler ...`);
|
||||
// console.log(`global.BUNDLER_CTX_DISPOSED`, global.BUNDLER_CTX_DISPOSED);
|
||||
global.BUNDLER_CTX_DISPOSED = true;
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
|
||||
+6
-1
@@ -48,6 +48,7 @@ export default async function pagesSSRBundler(params) {
|
||||
writeFileSync(path.join(BUNX_TMP_DIR, "ssr-entrypoints.json"), JSON.stringify(entryPoints, null, 4));
|
||||
}
|
||||
catch (error) { }
|
||||
try {
|
||||
await esbuild.build({
|
||||
entryPoints,
|
||||
outdir: BUNX_CWD_MODULE_CACHE_DIR,
|
||||
@@ -83,6 +84,10 @@ export default async function pagesSSRBundler(params) {
|
||||
...(config.ssr_compiler_excludes || []),
|
||||
],
|
||||
splitting: true,
|
||||
// logLevel: "silent",
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,15 +56,13 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
|
||||
_.merge(global.BUNDLER_CTX_MAP[artifact.local_path], artifact);
|
||||
}
|
||||
}
|
||||
post_build_fn?.({ artifacts });
|
||||
}
|
||||
const elapsed = (performance.now() - build_start).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
global.MAIN_CTX_BUILD_STARTS = 0;
|
||||
global.BUNDLER_CTX_DISPOSED = false;
|
||||
const does_error_file_exist = existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE);
|
||||
// SSR must finish before HMR so server props are fresh
|
||||
if (build_only) {
|
||||
try {
|
||||
await pagesSSRBundler();
|
||||
@@ -82,17 +80,22 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
|
||||
}
|
||||
else {
|
||||
try {
|
||||
pagesSSRBundler();
|
||||
await pagesSSRBundler();
|
||||
}
|
||||
catch (error) {
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
}
|
||||
if (artifacts?.[0] && artifacts.length > 0) {
|
||||
try {
|
||||
await post_build_fn?.({ artifacts });
|
||||
}
|
||||
// if (global.SSR_BUNDLER_CTX) {
|
||||
// global.SSR_BUNDLER_CTX.rebuild();
|
||||
// } else {
|
||||
// pagesSSRContextBundler();
|
||||
// }
|
||||
catch (error) {
|
||||
log.error(`Post-build Error: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
@@ -23,9 +23,15 @@ export default function ssrCTXArtifactTracker({ entryToPage, post_build_fn, }) {
|
||||
build.onEnd(async (result) => {
|
||||
if (result.errors.length > 0) {
|
||||
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
||||
try {
|
||||
await global.SSR_BUNDLER_CTX?.dispose();
|
||||
}
|
||||
catch { }
|
||||
global.SSR_BUNDLER_CTX = undefined;
|
||||
build_starts = 0;
|
||||
console.log("SSR Build errors:", result.errors);
|
||||
for (const err of result.errors) {
|
||||
console.error(`SSR Build error: ${err.text}${err.location ? ` (${err.location.file}:${err.location.line}:${err.location.column})` : ""}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
|
||||
Vendored
+2
-2
@@ -40,8 +40,8 @@ export default async function bunextInit(params) {
|
||||
else if (is_dev) {
|
||||
log.build(`Building Modules ...`);
|
||||
await allPagesESBuildContextBundler({
|
||||
post_build_fn: () => {
|
||||
serverPostBuildFn();
|
||||
post_build_fn: async () => {
|
||||
await serverPostBuildFn();
|
||||
},
|
||||
});
|
||||
watcherEsbuildCTX();
|
||||
|
||||
+31
-13
@@ -1,17 +1,17 @@
|
||||
import chokidar from "chokidar";
|
||||
import path from "path";
|
||||
import { existsSync, statSync } from "fs";
|
||||
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() {
|
||||
// Define ignored patterns directly in Chokidar for better performance
|
||||
const watcher = chokidar.watch(ROOT_DIR, {
|
||||
ignored: [
|
||||
/(^|[\/\\])\../, // ignore dotfiles
|
||||
/(^|[\/\\])\../,
|
||||
/node_modules/,
|
||||
/public/,
|
||||
/\.bunext/,
|
||||
@@ -25,6 +25,8 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
depth: 99,
|
||||
});
|
||||
const handleEvent = async (event, filePath) => {
|
||||
let owns_recompile = false;
|
||||
try {
|
||||
const filename = path.relative(ROOT_DIR, filePath);
|
||||
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
||||
await fullRebuild();
|
||||
@@ -34,9 +36,12 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
await fullRebuild({ msg: `Restarting Bundler ...` });
|
||||
}
|
||||
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
||||
pagesSSRBundler();
|
||||
await pagesSSRBundler().catch((error) => {
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
});
|
||||
}
|
||||
if (filename.match(/\/styles$/) || filename === "styles") {
|
||||
owns_recompile = true;
|
||||
global.RECOMPILING = true;
|
||||
await Bun.sleep(1000);
|
||||
await fullRebuild({
|
||||
@@ -55,6 +60,7 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
if (filename.match(target_files_match)) {
|
||||
if (global.RECOMPILING)
|
||||
return;
|
||||
owns_recompile = true;
|
||||
global.RECOMPILING = true;
|
||||
if (filename.match(/.*\.server\.tsx?/)) {
|
||||
global.IS_SERVER_COMPONENT = true;
|
||||
@@ -62,30 +68,33 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
if (global.BUNDLER_CTX) {
|
||||
await global.BUNDLER_CTX.rebuild();
|
||||
}
|
||||
// HMR for error pages
|
||||
if (filename.match(/(404|500)\.tsx?/)) {
|
||||
global.HMR_CONTROLLERS.forEach((controller) => {
|
||||
for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
|
||||
const controller = global.HMR_CONTROLLERS[i];
|
||||
try {
|
||||
controller?.controller?.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`);
|
||||
});
|
||||
}
|
||||
catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Handle Structural Changes (Add/Delete)
|
||||
if (["add", "unlink", "addDir", "unlinkDir"].includes(event)) {
|
||||
const is_file_of_interest = !!filename.match(target_files_match) || event.includes("Dir");
|
||||
const is_file_of_interest = !!filename.match(target_files_match) ||
|
||||
event.includes("Dir");
|
||||
if (!is_file_of_interest)
|
||||
return;
|
||||
// Validation logic
|
||||
if (!filename.match(/^src\/pages\/|\.css$/) ||
|
||||
checkExcludedPatterns({ path: filename }) ||
|
||||
filename.includes(" ")) {
|
||||
// With chokidar, you rarely need to "reload" the whole watcher.
|
||||
// But we keep the logic for consistency.
|
||||
return reloadWatcher();
|
||||
}
|
||||
if (global.RECOMPILING)
|
||||
return;
|
||||
owns_recompile = true;
|
||||
const action = event.startsWith("add") ? "created" : "deleted";
|
||||
const type = filename.match(/\.css$/)
|
||||
? "Stylesheet"
|
||||
@@ -98,6 +107,16 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
log.error(`Watcher rebuild failed: ${error}`);
|
||||
}
|
||||
finally {
|
||||
if (owns_recompile) {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
watcher
|
||||
.on("add", (path) => handleEvent("add", path))
|
||||
@@ -105,7 +124,6 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
.on("unlink", (path) => handleEvent("unlink", path))
|
||||
.on("addDir", (path) => handleEvent("addDir", path))
|
||||
.on("unlinkDir", (path) => handleEvent("unlinkDir", path));
|
||||
// global.PAGES_SRC_WATCHER = watcher;
|
||||
}
|
||||
function reloadWatcher() {
|
||||
if (global.PAGES_SRC_WATCHER) {
|
||||
|
||||
+5
-4
@@ -17,17 +17,18 @@ export default async function fullRebuild(params) {
|
||||
global.SSR_BUNDLER_CTX = undefined;
|
||||
}
|
||||
catch (error) { }
|
||||
// await pagesSSRBundler();
|
||||
await allPagesESBuildContextBundler({
|
||||
post_build_fn: () => {
|
||||
serverPostBuildFn();
|
||||
post_build_fn: async () => {
|
||||
await serverPostBuildFn();
|
||||
},
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
finally {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
log.error(error);
|
||||
}
|
||||
if (global.PAGES_SRC_WATCHER) {
|
||||
global.PAGES_SRC_WATCHER.close();
|
||||
|
||||
+17
-9
@@ -22,28 +22,37 @@ export default async function serverPostBuildFn(params) {
|
||||
continue;
|
||||
}
|
||||
if (params?.reload_all_controllers) {
|
||||
try {
|
||||
controller.controller.enqueue(reload_enqueue);
|
||||
}
|
||||
catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
||||
if (!target_artifact.local_path) {
|
||||
if (!target_artifact?.local_path) {
|
||||
try {
|
||||
controller.controller.enqueue(reload_enqueue);
|
||||
}
|
||||
catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const mock_req = target_artifact.req_url
|
||||
? new Request(target_artifact.req_url)
|
||||
: new Request(controller.page_url);
|
||||
const page_component = global.IS_SERVER_COMPONENT
|
||||
? await grabPageComponent({
|
||||
// Always re-run server fns so fixed errors clear on the first HMR
|
||||
const page_component = await grabPageComponent({
|
||||
req: mock_req,
|
||||
return_server_res_only: true,
|
||||
is_hydration: true,
|
||||
})
|
||||
: {};
|
||||
});
|
||||
if (page_component instanceof Response) {
|
||||
continue;
|
||||
}
|
||||
const { serverRes } = page_component;
|
||||
const { serverRes } = page_component || {};
|
||||
const final_artifact = {
|
||||
..._.omit(controller, ["controller"]),
|
||||
target_map: target_artifact,
|
||||
@@ -51,9 +60,8 @@ export default async function serverPostBuildFn(params) {
|
||||
if (!target_artifact) {
|
||||
delete final_artifact.target_map;
|
||||
}
|
||||
if (serverRes) {
|
||||
final_artifact.page_props = serverRes;
|
||||
}
|
||||
// Always replace so prior error props cannot linger
|
||||
final_artifact.page_props = serverRes || {};
|
||||
try {
|
||||
let final_data = {};
|
||||
if (global.ROOT_FILE_UPDATED) {
|
||||
|
||||
+24
-2
@@ -5,12 +5,15 @@ 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 watcherEsbuildCTX() {
|
||||
const pages_src_watcher = watch(ROOT_DIR, {
|
||||
recursive: true,
|
||||
persistent: true,
|
||||
}, async (event, filename) => {
|
||||
let owns_recompile = false;
|
||||
try {
|
||||
if (!filename)
|
||||
return;
|
||||
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
||||
@@ -25,7 +28,9 @@ export default async function watcherEsbuildCTX() {
|
||||
return;
|
||||
}
|
||||
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
||||
pagesSSRBundler();
|
||||
await pagesSSRBundler().catch((error) => {
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
});
|
||||
}
|
||||
if (filename.endsWith(AppData["BunextTmpFileExt"])) {
|
||||
return;
|
||||
@@ -36,6 +41,7 @@ export default async function watcherEsbuildCTX() {
|
||||
? statSync(full_file_path)
|
||||
: undefined;
|
||||
if (full_file_path.match(/\/styles$/)) {
|
||||
owns_recompile = true;
|
||||
global.RECOMPILING = true;
|
||||
await Bun.sleep(1000);
|
||||
await fullRebuild({
|
||||
@@ -53,11 +59,11 @@ export default async function watcherEsbuildCTX() {
|
||||
return;
|
||||
}
|
||||
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
||||
// const rebuild_skip_paths = /\/pages\/api\//;
|
||||
if (event !== "rename") {
|
||||
if (filename.match(target_files_match)) {
|
||||
if (global.RECOMPILING)
|
||||
return;
|
||||
owns_recompile = true;
|
||||
global.RECOMPILING = true;
|
||||
if (filename.match(/.*\.server\.tsx?/)) {
|
||||
global.IS_SERVER_COMPONENT = true;
|
||||
@@ -68,8 +74,13 @@ export default async function watcherEsbuildCTX() {
|
||||
if (filename.match(/(404|500)\.tsx?/)) {
|
||||
for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
|
||||
const controller = global.HMR_CONTROLLERS[i];
|
||||
try {
|
||||
controller?.controller?.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`);
|
||||
}
|
||||
catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
@@ -87,6 +98,7 @@ export default async function watcherEsbuildCTX() {
|
||||
return reloadWatcher();
|
||||
if (global.RECOMPILING)
|
||||
return;
|
||||
owns_recompile = true;
|
||||
const action = does_file_exist ? "created" : "deleted";
|
||||
const type = filename.match(/\.css$/)
|
||||
? "Sylesheet"
|
||||
@@ -98,6 +110,16 @@ export default async function watcherEsbuildCTX() {
|
||||
await fullRebuild({
|
||||
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
log.error(`Watcher rebuild failed: ${error}`);
|
||||
}
|
||||
finally {
|
||||
if (owns_recompile) {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
global.PAGES_SRC_WATCHER = pages_src_watcher;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@ const { ROOT_DIR } = grabDirNames();
|
||||
export default async function grabPageBundledReactComponent({ file_path, return_tsx_only, }) {
|
||||
try {
|
||||
if (global.SSR_BUNDLER_CTX_MAP?.[file_path]) {
|
||||
const mod = await import(path.join(ROOT_DIR, global.SSR_BUNDLER_CTX_MAP[file_path].path));
|
||||
const abs = path.join(ROOT_DIR, global.SSR_BUNDLER_CTX_MAP[file_path].path);
|
||||
Loader.registry.delete(abs);
|
||||
const mod = await import(`${abs}?t=${Date.now()}`);
|
||||
const Main = mod.default;
|
||||
return { component: Main };
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
|
||||
const final_root_server_path = root_server_ctx_map?.local_path
|
||||
? path.join(ROOT_DIR, root_server_ctx_map.path)
|
||||
: root_server_file_path;
|
||||
if (final_root_server_path) {
|
||||
Loader.registry.delete(final_root_server_path);
|
||||
}
|
||||
const root_server_module = final_root_server_path
|
||||
? await import(`${final_root_server_path}?t=${now}`)
|
||||
: undefined;
|
||||
@@ -34,6 +37,9 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
|
||||
const final_page_server_path = page_server_ctx?.local_path
|
||||
? path.join(ROOT_DIR, page_server_ctx.path)
|
||||
: server_file_path;
|
||||
if (final_page_server_path) {
|
||||
Loader.registry.delete(final_page_server_path);
|
||||
}
|
||||
const server_module = final_page_server_path
|
||||
? await import(`${final_page_server_path}?t=${now}`)
|
||||
: undefined;
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/bunext",
|
||||
"version": "1.0.96",
|
||||
"version": "1.0.97",
|
||||
"main": "dist/index.js",
|
||||
"module": "index.ts",
|
||||
"dependencies": {
|
||||
|
||||
@@ -6,6 +6,14 @@ import { rmSync } from "fs";
|
||||
|
||||
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
|
||||
|
||||
process.on("uncaughtException", (error) => {
|
||||
log.error(`Uncaught exception: ${error}`);
|
||||
});
|
||||
|
||||
process.on("unhandledRejection", (reason) => {
|
||||
log.error(`Unhandled rejection: ${reason}`);
|
||||
});
|
||||
|
||||
log.info("Running development server ...");
|
||||
|
||||
try {
|
||||
@@ -13,6 +21,9 @@ try {
|
||||
rmSync(BUNX_CWD_PAGES_REWRITE_DIR, { recursive: true });
|
||||
} catch (error) {}
|
||||
|
||||
await bunextInit();
|
||||
|
||||
await startServer();
|
||||
try {
|
||||
await bunextInit();
|
||||
await startServer();
|
||||
} catch (error) {
|
||||
log.error(`Failed to start development server: ${error}`);
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ export default async function buildOnstartErrorHandler(params?: Params) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Killing Bundler ...`);
|
||||
console.log(`global.BUNDLER_CTX_DISPOSED`, global.BUNDLER_CTX_DISPOSED);
|
||||
// console.log(`Killing Bundler ...`);
|
||||
// console.log(`global.BUNDLER_CTX_DISPOSED`, global.BUNDLER_CTX_DISPOSED);
|
||||
|
||||
global.BUNDLER_CTX_DISPOSED = true;
|
||||
|
||||
|
||||
@@ -75,6 +75,7 @@ export default async function pagesSSRBundler(params?: Params) {
|
||||
);
|
||||
} catch (error) {}
|
||||
|
||||
try {
|
||||
await esbuild.build({
|
||||
entryPoints,
|
||||
outdir: BUNX_CWD_MODULE_CACHE_DIR,
|
||||
@@ -112,6 +113,9 @@ export default async function pagesSSRBundler(params?: Params) {
|
||||
...(config.ssr_compiler_excludes || []),
|
||||
],
|
||||
splitting: true,
|
||||
// logLevel: "silent",
|
||||
});
|
||||
} catch (error) {
|
||||
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,16 +97,11 @@ export default function esbuildCTXArtifactTracker({
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
post_build_fn?.({ artifacts });
|
||||
}
|
||||
|
||||
const elapsed = (performance.now() - build_start).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
|
||||
global.MAIN_CTX_BUILD_STARTS = 0;
|
||||
global.BUNDLER_CTX_DISPOSED = false;
|
||||
|
||||
@@ -114,6 +109,7 @@ export default function esbuildCTXArtifactTracker({
|
||||
BUNX_BUNDLER_ERROR_EXIT_FILE,
|
||||
);
|
||||
|
||||
// SSR must finish before HMR so server props are fresh
|
||||
if (build_only) {
|
||||
try {
|
||||
await pagesSSRBundler();
|
||||
@@ -131,17 +127,22 @@ export default function esbuildCTXArtifactTracker({
|
||||
await fullRebuild();
|
||||
} else {
|
||||
try {
|
||||
pagesSSRBundler();
|
||||
await pagesSSRBundler();
|
||||
} catch (error) {
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
}
|
||||
|
||||
if (artifacts?.[0] && artifacts.length > 0) {
|
||||
try {
|
||||
await post_build_fn?.({ artifacts });
|
||||
} catch (error) {
|
||||
log.error(`Post-build Error: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if (global.SSR_BUNDLER_CTX) {
|
||||
// global.SSR_BUNDLER_CTX.rebuild();
|
||||
// } else {
|
||||
// pagesSSRContextBundler();
|
||||
// }
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
@@ -41,9 +41,16 @@ export default function ssrCTXArtifactTracker({
|
||||
build.onEnd(async (result) => {
|
||||
if (result.errors.length > 0) {
|
||||
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
||||
try {
|
||||
await global.SSR_BUNDLER_CTX?.dispose();
|
||||
} catch {}
|
||||
global.SSR_BUNDLER_CTX = undefined;
|
||||
build_starts = 0;
|
||||
console.log("SSR Build errors:", result.errors);
|
||||
for (const err of result.errors) {
|
||||
console.error(
|
||||
`SSR Build error: ${err.text}${err.location ? ` (${err.location.file}:${err.location.line}:${err.location.column})` : ""}`,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -98,8 +98,8 @@ export default async function bunextInit(params?: Params) {
|
||||
} else if (is_dev) {
|
||||
log.build(`Building Modules ...`);
|
||||
await allPagesESBuildContextBundler({
|
||||
post_build_fn: () => {
|
||||
serverPostBuildFn();
|
||||
post_build_fn: async () => {
|
||||
await serverPostBuildFn();
|
||||
},
|
||||
});
|
||||
watcherEsbuildCTX();
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import chokidar from "chokidar";
|
||||
import path from "path";
|
||||
import { existsSync, statSync } from "fs";
|
||||
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() {
|
||||
// Define ignored patterns directly in Chokidar for better performance
|
||||
const watcher = chokidar.watch(ROOT_DIR, {
|
||||
ignored: [
|
||||
/(^|[\/\\])\../, // ignore dotfiles
|
||||
/(^|[\/\\])\../,
|
||||
/node_modules/,
|
||||
/public/,
|
||||
/\.bunext/,
|
||||
@@ -31,6 +31,9 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
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)) {
|
||||
@@ -43,10 +46,13 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
}
|
||||
|
||||
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
||||
pagesSSRBundler();
|
||||
await pagesSSRBundler().catch((error) => {
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
});
|
||||
}
|
||||
|
||||
if (filename.match(/\/styles$/) || filename === "styles") {
|
||||
owns_recompile = true;
|
||||
global.RECOMPILING = true;
|
||||
await Bun.sleep(1000);
|
||||
await fullRebuild({
|
||||
@@ -67,6 +73,7 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
if (event === "change") {
|
||||
if (filename.match(target_files_match)) {
|
||||
if (global.RECOMPILING) return;
|
||||
owns_recompile = true;
|
||||
global.RECOMPILING = true;
|
||||
|
||||
if (filename.match(/.*\.server\.tsx?/)) {
|
||||
@@ -77,38 +84,44 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
await global.BUNDLER_CTX.rebuild();
|
||||
}
|
||||
|
||||
// HMR for error pages
|
||||
if (filename.match(/(404|500)\.tsx?/)) {
|
||||
global.HMR_CONTROLLERS.forEach((controller) => {
|
||||
for (
|
||||
let i = global.HMR_CONTROLLERS.length - 1;
|
||||
i >= 0;
|
||||
i--
|
||||
) {
|
||||
const controller = global.HMR_CONTROLLERS[i];
|
||||
try {
|
||||
controller?.controller?.enqueue(
|
||||
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
|
||||
);
|
||||
});
|
||||
} catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle Structural Changes (Add/Delete)
|
||||
if (["add", "unlink", "addDir", "unlinkDir"].includes(event)) {
|
||||
const is_file_of_interest =
|
||||
!!filename.match(target_files_match) || event.includes("Dir");
|
||||
!!filename.match(target_files_match) ||
|
||||
event.includes("Dir");
|
||||
|
||||
if (!is_file_of_interest) return;
|
||||
|
||||
// Validation logic
|
||||
if (
|
||||
!filename.match(/^src\/pages\/|\.css$/) ||
|
||||
checkExcludedPatterns({ path: filename }) ||
|
||||
filename.includes(" ")
|
||||
) {
|
||||
// With chokidar, you rarely need to "reload" the whole watcher.
|
||||
// But we keep the logic for consistency.
|
||||
return reloadWatcher();
|
||||
}
|
||||
|
||||
if (global.RECOMPILING) return;
|
||||
|
||||
owns_recompile = true;
|
||||
const action = event.startsWith("add") ? "created" : "deleted";
|
||||
const type = filename.match(/\.css$/)
|
||||
? "Stylesheet"
|
||||
@@ -122,6 +135,14 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(`Watcher rebuild failed: ${error}`);
|
||||
} finally {
|
||||
if (owns_recompile) {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
watcher
|
||||
@@ -130,8 +151,6 @@ export default async function chokadirWatcherEsbuildCTX() {
|
||||
.on("unlink", (path) => handleEvent("unlink", path))
|
||||
.on("addDir", (path) => handleEvent("addDir", path))
|
||||
.on("unlinkDir", (path) => handleEvent("unlinkDir", path));
|
||||
|
||||
// global.PAGES_SRC_WATCHER = watcher;
|
||||
}
|
||||
|
||||
function reloadWatcher() {
|
||||
|
||||
@@ -23,17 +23,16 @@ export default async function fullRebuild(params?: { msg?: string }) {
|
||||
global.SSR_BUNDLER_CTX = undefined;
|
||||
} catch (error) {}
|
||||
|
||||
// await pagesSSRBundler();
|
||||
|
||||
await allPagesESBuildContextBundler({
|
||||
post_build_fn: () => {
|
||||
serverPostBuildFn();
|
||||
post_build_fn: async () => {
|
||||
await serverPostBuildFn();
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
log.error(error);
|
||||
} finally {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
log.error(error);
|
||||
}
|
||||
|
||||
if (global.PAGES_SRC_WATCHER) {
|
||||
|
||||
@@ -33,15 +33,23 @@ export default async function serverPostBuildFn(params?: Params) {
|
||||
}
|
||||
|
||||
if (params?.reload_all_controllers) {
|
||||
try {
|
||||
controller.controller.enqueue(reload_enqueue);
|
||||
} catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const target_artifact =
|
||||
global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
||||
|
||||
if (!target_artifact.local_path) {
|
||||
if (!target_artifact?.local_path) {
|
||||
try {
|
||||
controller.controller.enqueue(reload_enqueue);
|
||||
} catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -49,19 +57,18 @@ export default async function serverPostBuildFn(params?: Params) {
|
||||
? new Request(target_artifact.req_url)
|
||||
: new Request(controller.page_url);
|
||||
|
||||
const page_component = global.IS_SERVER_COMPONENT
|
||||
? await grabPageComponent({
|
||||
// Always re-run server fns so fixed errors clear on the first HMR
|
||||
const page_component = await grabPageComponent({
|
||||
req: mock_req,
|
||||
return_server_res_only: true,
|
||||
is_hydration: true,
|
||||
})
|
||||
: {};
|
||||
});
|
||||
|
||||
if (page_component instanceof Response) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const { serverRes } = page_component;
|
||||
const { serverRes } = page_component || {};
|
||||
|
||||
const final_artifact: Omit<GlobalHMRControllerObject, "controller"> = {
|
||||
..._.omit(controller, ["controller"]),
|
||||
@@ -72,9 +79,8 @@ export default async function serverPostBuildFn(params?: Params) {
|
||||
delete final_artifact.target_map;
|
||||
}
|
||||
|
||||
if (serverRes) {
|
||||
final_artifact.page_props = serverRes;
|
||||
}
|
||||
// Always replace so prior error props cannot linger
|
||||
final_artifact.page_props = serverRes || {};
|
||||
|
||||
try {
|
||||
let final_data: { [k: string]: any } = {};
|
||||
|
||||
@@ -5,6 +5,7 @@ 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();
|
||||
|
||||
@@ -16,6 +17,9 @@ export default async function watcherEsbuildCTX() {
|
||||
persistent: true,
|
||||
},
|
||||
async (event, filename) => {
|
||||
let owns_recompile = false;
|
||||
|
||||
try {
|
||||
if (!filename) return;
|
||||
|
||||
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
||||
@@ -33,7 +37,9 @@ export default async function watcherEsbuildCTX() {
|
||||
}
|
||||
|
||||
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
||||
pagesSSRBundler();
|
||||
await pagesSSRBundler().catch((error) => {
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
});
|
||||
}
|
||||
|
||||
if (filename.endsWith(AppData["BunextTmpFileExt"])) {
|
||||
@@ -47,6 +53,7 @@ export default async function watcherEsbuildCTX() {
|
||||
: undefined;
|
||||
|
||||
if (full_file_path.match(/\/styles$/)) {
|
||||
owns_recompile = true;
|
||||
global.RECOMPILING = true;
|
||||
await Bun.sleep(1000);
|
||||
await fullRebuild({
|
||||
@@ -68,11 +75,11 @@ export default async function watcherEsbuildCTX() {
|
||||
}
|
||||
|
||||
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
||||
// const rebuild_skip_paths = /\/pages\/api\//;
|
||||
|
||||
if (event !== "rename") {
|
||||
if (filename.match(target_files_match)) {
|
||||
if (global.RECOMPILING) return;
|
||||
owns_recompile = true;
|
||||
global.RECOMPILING = true;
|
||||
|
||||
if (filename.match(/.*\.server\.tsx?/)) {
|
||||
@@ -90,9 +97,13 @@ export default async function watcherEsbuildCTX() {
|
||||
i--
|
||||
) {
|
||||
const controller = global.HMR_CONTROLLERS[i];
|
||||
try {
|
||||
controller?.controller?.enqueue(
|
||||
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
|
||||
);
|
||||
} catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,13 +118,15 @@ export default async function watcherEsbuildCTX() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!filename.match(/^src\/pages\/|\.css$/)) return reloadWatcher();
|
||||
if (!filename.match(/^src\/pages\/|\.css$/))
|
||||
return reloadWatcher();
|
||||
if (checkExcludedPatterns({ path: filename }))
|
||||
return reloadWatcher();
|
||||
if (filename.match(/ /)) return reloadWatcher();
|
||||
|
||||
if (global.RECOMPILING) return;
|
||||
|
||||
owns_recompile = true;
|
||||
const action = does_file_exist ? "created" : "deleted";
|
||||
const type = filename.match(/\.css$/)
|
||||
? "Sylesheet"
|
||||
@@ -126,6 +139,14 @@ export default async function watcherEsbuildCTX() {
|
||||
await fullRebuild({
|
||||
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
||||
});
|
||||
} catch (error) {
|
||||
log.error(`Watcher rebuild failed: ${error}`);
|
||||
} finally {
|
||||
if (owns_recompile) {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -20,9 +20,12 @@ export default async function grabPageBundledReactComponent({
|
||||
}: Params): Promise<GrabPageReactBundledComponentRes | undefined> {
|
||||
try {
|
||||
if (global.SSR_BUNDLER_CTX_MAP?.[file_path]) {
|
||||
const mod = await import(
|
||||
path.join(ROOT_DIR, global.SSR_BUNDLER_CTX_MAP[file_path].path)
|
||||
const abs = path.join(
|
||||
ROOT_DIR,
|
||||
global.SSR_BUNDLER_CTX_MAP[file_path].path,
|
||||
);
|
||||
Loader.registry.delete(abs);
|
||||
const mod = await import(`${abs}?t=${Date.now()}`);
|
||||
|
||||
const Main = mod.default as FC;
|
||||
|
||||
|
||||
@@ -40,6 +40,9 @@ export default async function grabPageCombinedServerRes({
|
||||
? path.join(ROOT_DIR, root_server_ctx_map.path)
|
||||
: root_server_file_path;
|
||||
|
||||
if (final_root_server_path) {
|
||||
Loader.registry.delete(final_root_server_path);
|
||||
}
|
||||
const root_server_module: BunextPageServerModule = final_root_server_path
|
||||
? await import(`${final_root_server_path}?t=${now}`)
|
||||
: undefined;
|
||||
@@ -65,6 +68,9 @@ export default async function grabPageCombinedServerRes({
|
||||
? path.join(ROOT_DIR, page_server_ctx.path)
|
||||
: server_file_path;
|
||||
|
||||
if (final_page_server_path) {
|
||||
Loader.registry.delete(final_page_server_path);
|
||||
}
|
||||
const server_module: BunextPageServerModule = final_page_server_path
|
||||
? await import(`${final_page_server_path}?t=${now}`)
|
||||
: undefined;
|
||||
|
||||
Reference in New Issue
Block a user