Update watcher function. Add tests

This commit is contained in:
2026-03-21 09:03:26 +01:00
parent 5c53e94a3e
commit cf010ad4f5
53 changed files with 1329 additions and 97 deletions
+1 -1
View File
@@ -10,7 +10,6 @@ import EJSON from "../utils/ejson";
import { log } from "../utils/log";
import cron from "./server/cron";
export default async function bunextInit() {
log.banner();
global.ORA_SPINNER = ora();
global.ORA_SPINNER.clear();
global.HMR_CONTROLLERS = [];
@@ -18,6 +17,7 @@ export default async function bunextInit() {
global.BUNDLER_REBUILDS = 0;
global.PAGE_FILES = [];
await init();
log.banner();
const { PAGES_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
const router = new Bun.FileSystemRouter({
style: "nextjs",
+2 -1
View File
@@ -8,7 +8,8 @@ export default async function () {
execSync(`rm -rf ${dirNames.BUNEXT_CACHE_DIR}`);
execSync(`rm -rf ${dirNames.BUNX_CWD_MODULE_CACHE_DIR}`);
try {
const current_version = (await Bun.file(path.resolve(__dirname, "../../package.json")).json()).version;
const package_json = await Bun.file(path.resolve(__dirname, "../../package.json")).json();
const current_version = package_json.version;
global.CURRENT_VERSION = current_version;
}
catch (error) { }
+1 -1
View File
@@ -1,5 +1,5 @@
type Params = {
req: Request;
};
export default function bunextRequestHandler({ req, }: Params): Promise<Response>;
export default function bunextRequestHandler({ req: initial_req, }: Params): Promise<Response>;
export {};
+7 -3
View File
@@ -7,20 +7,24 @@ import handleHmr from "./handle-hmr";
import handleHmrUpdate from "./handle-hmr-update";
import handlePublic from "./handle-public";
import handleFiles from "./handle-files";
export default async function bunextRequestHandler({ req, }) {
export default async function bunextRequestHandler({ req: initial_req, }) {
const is_dev = isDevelopment();
let req = initial_req.clone();
try {
const url = new URL(req.url);
const { config } = grabConstants();
let response = undefined;
if (config?.middleware) {
const middleware_res = await config.middleware({
req,
req: initial_req,
url,
});
if (typeof middleware_res == "object") {
if (middleware_res instanceof Response) {
return middleware_res;
}
if (middleware_res instanceof Request) {
req = middleware_res;
}
}
if (url.pathname == `/${AppData["ClientHMRPath"]}`) {
response = await handleHmrUpdate({ req });
+35 -19
View File
@@ -3,14 +3,23 @@ import path from "path";
import grabDirNames from "../../utils/grab-dir-names";
import rebuildBundler from "./rebuild-bundler";
import { log } from "../../utils/log";
const { SRC_DIR } = grabDirNames();
const { ROOT_DIR } = grabDirNames();
export default function watcher() {
const pages_src_watcher = watch(SRC_DIR, {
const pages_src_watcher = watch(ROOT_DIR, {
recursive: true,
persistent: true,
}, async (event, filename) => {
if (!filename)
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;
}
if (event !== "rename") {
if (filename.match(/\.(tsx?|jsx?|css)$/) &&
global.BUNDLER_CTX) {
@@ -21,29 +30,36 @@ export default function watcher() {
}
return;
}
if (!filename.match(/^pages\//))
if (!filename.match(/^src\/pages\//))
return;
if (filename.match(/\/(--|\()/))
return;
if (global.RECOMPILING)
return;
const fullPath = path.join(SRC_DIR, filename);
const fullPath = path.join(ROOT_DIR, filename);
const action = existsSync(fullPath) ? "created" : "deleted";
try {
global.RECOMPILING = true;
log.watch(`Page ${action}: ${filename}. Rebuilding ...`);
await rebuildBundler();
}
catch (error) {
log.error(error);
}
finally {
global.RECOMPILING = false;
}
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
watcher();
}
await fullRebuild({
msg: `Page ${action}: ${filename}. Rebuilding ...`,
});
});
global.PAGES_SRC_WATCHER = pages_src_watcher;
}
async function fullRebuild({ msg }) {
try {
global.RECOMPILING = true;
if (msg) {
log.watch(msg);
}
await rebuildBundler();
}
catch (error) {
log.error(error);
}
finally {
global.RECOMPILING = false;
}
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
watcher();
}
}
@@ -48,7 +48,7 @@ export default async function (params) {
script += ` document.head.appendChild(newScript);\n\n`;
script += ` } catch (err) {\n`;
script += ` console.error("HMR update failed, falling back to reload:", err.message);\n`;
// script += ` window.location.reload();\n`;
script += ` window.location.reload();\n`;
script += ` }\n`;
script += ` }\n`;
script += `});\n`;