Update watcher function. Add tests
This commit is contained in:
@@ -40,8 +40,6 @@ declare global {
|
||||
}
|
||||
|
||||
export default async function bunextInit() {
|
||||
log.banner();
|
||||
|
||||
global.ORA_SPINNER = ora();
|
||||
global.ORA_SPINNER.clear();
|
||||
global.HMR_CONTROLLERS = [];
|
||||
@@ -50,6 +48,7 @@ export default async function bunextInit() {
|
||||
global.PAGE_FILES = [];
|
||||
|
||||
await init();
|
||||
log.banner();
|
||||
|
||||
const { PAGES_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
||||
|
||||
|
||||
@@ -12,9 +12,11 @@ export default async function () {
|
||||
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) {}
|
||||
|
||||
@@ -7,15 +7,15 @@ import handleHmr from "./handle-hmr";
|
||||
import handleHmrUpdate from "./handle-hmr-update";
|
||||
import handlePublic from "./handle-public";
|
||||
import handleFiles from "./handle-files";
|
||||
|
||||
type Params = {
|
||||
req: Request;
|
||||
};
|
||||
|
||||
export default async function bunextRequestHandler({
|
||||
req,
|
||||
req: initial_req,
|
||||
}: Params): Promise<Response> {
|
||||
const is_dev = isDevelopment();
|
||||
let req = initial_req.clone();
|
||||
|
||||
try {
|
||||
const url = new URL(req.url);
|
||||
@@ -26,13 +26,17 @@ export default async function bunextRequestHandler({
|
||||
|
||||
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"]}`) {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { watch, existsSync } from "fs";
|
||||
import path from "path";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import rebuildBundler from "./rebuild-bundler";
|
||||
import { log } from "../../utils/log";
|
||||
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
|
||||
export default function watcher() {
|
||||
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
|
||||
) {
|
||||
if (global.RECOMPILING) return;
|
||||
global.RECOMPILING = true;
|
||||
await global.BUNDLER_CTX.rebuild();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!filename.match(/^src\/pages\//)) return;
|
||||
if (filename.match(/\/(--|\()/)) return;
|
||||
|
||||
if (global.RECOMPILING) return;
|
||||
|
||||
const fullPath = path.join(ROOT_DIR, filename);
|
||||
const action = existsSync(fullPath) ? "created" : "deleted";
|
||||
|
||||
await fullRebuild({
|
||||
msg: `Page ${action}: ${filename}. Rebuilding ...`,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
global.PAGES_SRC_WATCHER = pages_src_watcher;
|
||||
}
|
||||
|
||||
async function fullRebuild({ msg }: { msg?: string }) {
|
||||
try {
|
||||
global.RECOMPILING = true;
|
||||
|
||||
if (msg) {
|
||||
log.watch(msg);
|
||||
}
|
||||
|
||||
await rebuildBundler();
|
||||
} catch (error: any) {
|
||||
log.error(error);
|
||||
} finally {
|
||||
global.RECOMPILING = false;
|
||||
}
|
||||
|
||||
if (global.PAGES_SRC_WATCHER) {
|
||||
global.PAGES_SRC_WATCHER.close();
|
||||
watcher();
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
import { watch, existsSync } from "fs";
|
||||
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();
|
||||
|
||||
export default function watcher() {
|
||||
const pages_src_watcher = watch(
|
||||
SRC_DIR,
|
||||
{
|
||||
recursive: true,
|
||||
persistent: true,
|
||||
},
|
||||
async (event, filename) => {
|
||||
if (!filename) return;
|
||||
|
||||
if (event !== "rename") {
|
||||
if (
|
||||
filename.match(/\.(tsx?|jsx?|css)$/) &&
|
||||
global.BUNDLER_CTX
|
||||
) {
|
||||
if (global.RECOMPILING) return;
|
||||
global.RECOMPILING = true;
|
||||
await global.BUNDLER_CTX.rebuild();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!filename.match(/^pages\//)) return;
|
||||
if (filename.match(/\/(--|\()/)) return;
|
||||
|
||||
if (global.RECOMPILING) return;
|
||||
|
||||
const fullPath = path.join(SRC_DIR, filename);
|
||||
const action = existsSync(fullPath) ? "created" : "deleted";
|
||||
|
||||
try {
|
||||
global.RECOMPILING = true;
|
||||
|
||||
log.watch(`Page ${action}: ${filename}. Rebuilding ...`);
|
||||
|
||||
await rebuildBundler();
|
||||
} catch (error: any) {
|
||||
log.error(error);
|
||||
} finally {
|
||||
global.RECOMPILING = false;
|
||||
}
|
||||
|
||||
if (global.PAGES_SRC_WATCHER) {
|
||||
global.PAGES_SRC_WATCHER.close();
|
||||
watcher();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
global.PAGES_SRC_WATCHER = pages_src_watcher;
|
||||
}
|
||||
@@ -59,7 +59,7 @@ export default async function (params?: 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