Update watcher function. Add tests
This commit is contained in:
+1
-1
@@ -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
@@ -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 });
|
||||
|
||||
Vendored
+35
-19
@@ -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`;
|
||||
|
||||
Reference in New Issue
Block a user