Rename global variables

This commit is contained in:
2026-08-03 12:01:40 +01:00
parent 87948340b0
commit 78e86b3999
78 changed files with 431 additions and 408 deletions
+2 -2
View File
@@ -19,8 +19,8 @@ export default async function bunextRequestHandler({ req: initial_req, server, }
return Response.json({ success: false, msg: `Invalid Path` });
}
let response = undefined;
if (global.CONSTANTS.config?.middleware) {
const middleware_res = await global.CONSTANTS.config.middleware({
if (global.BUNEXT_CONSTANTS.config?.middleware) {
const middleware_res = await global.BUNEXT_CONSTANTS.config.middleware({
req: initial_req,
url,
});
+16 -16
View File
@@ -32,17 +32,17 @@ export default async function chokadirWatcherEsbuildCTX() {
await fullRebuild();
return;
}
if (global.BUNDLER_CTX_DISPOSED) {
if (global.BUNEXT_BUNDLER_CTX_DISPOSED) {
await fullRebuild({ msg: `Restarting Bundler ...` });
}
if (global.SSR_BUNDLER_CTX_DISPOSED) {
if (global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED) {
await pagesSSRBundler().catch((error) => {
log.error(`SSR Bundler Error: ${error}`);
});
}
if (filename.match(/\/styles$/) || filename === "styles") {
owns_recompile = true;
global.RECOMPILING = true;
global.BUNEXT_RECOMPILING = true;
await Bun.sleep(1000);
await fullRebuild({
msg: `Detected new \`styles\` directory. Rebuilding ...`,
@@ -58,24 +58,24 @@ export default async function chokadirWatcherEsbuildCTX() {
const target_files_match = /\.(tsx?|jsx?|css)$/;
if (event === "change") {
if (filename.match(target_files_match)) {
if (global.RECOMPILING)
if (global.BUNEXT_RECOMPILING)
return;
owns_recompile = true;
global.RECOMPILING = true;
global.BUNEXT_RECOMPILING = true;
if (filename.match(/.*\.server\.tsx?/)) {
global.IS_SERVER_COMPONENT = true;
global.BUNEXT_IS_SERVER_COMPONENT = true;
}
if (global.BUNDLER_CTX) {
await global.BUNDLER_CTX.rebuild();
if (global.BUNEXT_BUNDLER_CTX) {
await global.BUNEXT_BUNDLER_CTX.rebuild();
}
if (filename.match(/(404|500)\.tsx?/)) {
for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
const controller = global.HMR_CONTROLLERS[i];
for (let i = global.BUNEXT_HMR_CONTROLLERS.length - 1; i >= 0; i--) {
const controller = global.BUNEXT_HMR_CONTROLLERS[i];
try {
controller?.controller?.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`);
}
catch {
global.HMR_CONTROLLERS.splice(i, 1);
global.BUNEXT_HMR_CONTROLLERS.splice(i, 1);
}
}
}
@@ -92,7 +92,7 @@ export default async function chokadirWatcherEsbuildCTX() {
filename.includes(" ")) {
return reloadWatcher();
}
if (global.RECOMPILING)
if (global.BUNEXT_RECOMPILING)
return;
owns_recompile = true;
const action = event.startsWith("add") ? "created" : "deleted";
@@ -113,8 +113,8 @@ export default async function chokadirWatcherEsbuildCTX() {
}
finally {
if (owns_recompile) {
global.RECOMPILING = false;
global.IS_SERVER_COMPONENT = false;
global.BUNEXT_RECOMPILING = false;
global.BUNEXT_IS_SERVER_COMPONENT = false;
}
}
};
@@ -126,8 +126,8 @@ export default async function chokadirWatcherEsbuildCTX() {
.on("unlinkDir", (path) => handleEvent("unlinkDir", path));
}
function reloadWatcher() {
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
if (global.BUNEXT_PAGES_SRC_WATCHER) {
global.BUNEXT_PAGES_SRC_WATCHER.close();
chokadirWatcherEsbuildCTX();
}
}
+10 -10
View File
@@ -5,16 +5,16 @@ import watcherEsbuildCTX from "./watcher-esbuild-ctx";
export default async function fullRebuild(params) {
try {
const { msg } = params || {};
global.RECOMPILING = true;
global.BUNEXT_RECOMPILING = true;
if (msg) {
log.watch(msg);
}
global.ROUTER.reload();
global.BUNEXT_ROUTER.reload();
try {
await global.BUNDLER_CTX?.dispose();
global.BUNDLER_CTX = undefined;
await global.SSR_BUNDLER_CTX?.dispose();
global.SSR_BUNDLER_CTX = undefined;
await global.BUNEXT_BUNDLER_CTX?.dispose();
global.BUNEXT_BUNDLER_CTX = undefined;
await global.BUNEXT_SSR_BUNDLER_CTX?.dispose();
global.BUNEXT_SSR_BUNDLER_CTX = undefined;
}
catch (error) { }
await allPagesESBuildContextBundler({
@@ -27,11 +27,11 @@ export default async function fullRebuild(params) {
log.error(error);
}
finally {
global.RECOMPILING = false;
global.IS_SERVER_COMPONENT = false;
global.BUNEXT_RECOMPILING = false;
global.BUNEXT_IS_SERVER_COMPONENT = false;
}
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
if (global.BUNEXT_PAGES_SRC_WATCHER) {
global.BUNEXT_PAGES_SRC_WATCHER.close();
watcherEsbuildCTX();
}
}
+5 -5
View File
@@ -1,7 +1,7 @@
function removeController(controller) {
const idx = global.HMR_CONTROLLERS.findIndex((c) => c.controller == controller);
const idx = global.BUNEXT_HMR_CONTROLLERS.findIndex((c) => c.controller == controller);
if (typeof idx == "number" && idx >= 0) {
global.HMR_CONTROLLERS.splice(idx, 1);
global.BUNEXT_HMR_CONTROLLERS.splice(idx, 1);
}
}
export default async function ({ req }) {
@@ -17,16 +17,16 @@ export default async function ({ req }) {
catch {
return new Response("Invalid Referer Header", { status: 400 });
}
const match = global.ROUTER.match(referer_url.pathname);
const match = global.BUNEXT_ROUTER.match(referer_url.pathname);
const target_map = match?.filePath
? global.BUNDLER_CTX_MAP?.[match.filePath]
? global.BUNEXT_BUNDLER_CTX_MAP?.[match.filePath]
: undefined;
let controller;
let heartbeat;
const stream = new ReadableStream({
start(c) {
controller = c;
global.HMR_CONTROLLERS.push({
global.BUNEXT_HMR_CONTROLLERS.push({
controller: c,
page_url: referer_url.href,
target_map,
+2 -2
View File
@@ -30,8 +30,8 @@ export default async function ({ req }) {
});
let module;
const now = Date.now();
if (is_dev && global.SSR_BUNDLER_CTX_MAP?.[match.filePath]?.path) {
const target_import = path.join(ROOT_DIR, global.SSR_BUNDLER_CTX_MAP[match.filePath].path);
if (is_dev && global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[match.filePath]?.path) {
const target_import = path.join(ROOT_DIR, global.BUNEXT_SSR_BUNDLER_CTX_MAP[match.filePath].path);
module = await import(`${target_import}?t=${now}`);
}
else {
+9 -9
View File
@@ -1,13 +1,13 @@
import _ from "lodash";
import grabPageComponent from "./web-pages/grab-page-component";
export default async function serverPostBuildFn(params) {
if (!global.HMR_CONTROLLERS?.[0] || !global.BUNDLER_CTX_MAP) {
if (!global.BUNEXT_HMR_CONTROLLERS?.[0] || !global.BUNEXT_BUNDLER_CTX_MAP) {
return;
}
const reload_payload = { reload: true };
const reload_enqueue = `event: update\ndata: ${JSON.stringify(reload_payload)}\n\n`;
for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
const controller = global.HMR_CONTROLLERS[i];
for (let i = global.BUNEXT_HMR_CONTROLLERS.length - 1; i >= 0; i--) {
const controller = global.BUNEXT_HMR_CONTROLLERS[i];
if (!controller) {
continue;
}
@@ -19,17 +19,17 @@ export default async function serverPostBuildFn(params) {
controller.controller.enqueue(reload_enqueue);
}
catch {
global.HMR_CONTROLLERS.splice(i, 1);
global.BUNEXT_HMR_CONTROLLERS.splice(i, 1);
}
continue;
}
const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path];
const target_artifact = global.BUNEXT_BUNDLER_CTX_MAP[controller.target_map.local_path];
if (!target_artifact?.local_path) {
try {
controller.controller.enqueue(reload_enqueue);
}
catch {
global.HMR_CONTROLLERS.splice(i, 1);
global.BUNEXT_HMR_CONTROLLERS.splice(i, 1);
}
continue;
}
@@ -59,17 +59,17 @@ export default async function serverPostBuildFn(params) {
final_artifact.page_props = serverRes || {};
try {
let final_data = {};
if (global.ROOT_FILE_UPDATED) {
if (global.BUNEXT_ROOT_FILE_UPDATED) {
final_data = reload_payload;
}
else {
final_data = final_artifact;
}
controller.controller.enqueue(`event: update\ndata: ${JSON.stringify(final_data)}\n\n`);
global.ROOT_FILE_UPDATED = false;
global.BUNEXT_ROOT_FILE_UPDATED = false;
}
catch {
global.HMR_CONTROLLERS.splice(i, 1);
global.BUNEXT_HMR_CONTROLLERS.splice(i, 1);
}
}
}
+1 -1
View File
@@ -4,7 +4,7 @@ import serverParamsGen from "./server-params-gen";
export default async function startServer() {
const serverParams = await serverParamsGen();
const server = Bun.serve(serverParams);
global.SERVER = server;
global.BUNEXT_SERVER = server;
log.server(`http://${server.hostname}:${server.port}`);
return server;
}
+20 -20
View File
@@ -17,9 +17,9 @@ export default async function watcherEsbuildCTX() {
if (!filename)
return;
const full_file_path = path.join(ROOT_DIR, filename);
if (global.CONFIG.exclude_watch_patterns) {
for (let i = 0; i < global.CONFIG.exclude_watch_patterns.length; i++) {
const watch_pattern = global.CONFIG.exclude_watch_patterns[i];
if (global.BUNEXT_CONFIG.exclude_watch_patterns) {
for (let i = 0; i < global.BUNEXT_CONFIG.exclude_watch_patterns.length; i++) {
const watch_pattern = global.BUNEXT_CONFIG.exclude_watch_patterns[i];
if (watch_pattern instanceof RegExp) {
const is_path_excluded = watch_pattern.test(filename);
if (is_path_excluded) {
@@ -41,11 +41,11 @@ export default async function watcherEsbuildCTX() {
if (filename.match(/^\.\w+/)) {
return;
}
if (global.BUNDLER_CTX_DISPOSED) {
if (global.BUNEXT_BUNDLER_CTX_DISPOSED) {
await fullRebuild({ msg: `Restarting Bundler ...` });
return;
}
if (global.SSR_BUNDLER_CTX_DISPOSED) {
if (global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED) {
await pagesSSRBundler().catch((error) => {
log.error(`SSR Bundler Error: ${error}`);
});
@@ -59,7 +59,7 @@ export default async function watcherEsbuildCTX() {
: undefined;
if (full_file_path.match(/\/styles$/)) {
owns_recompile = true;
global.RECOMPILING = true;
global.BUNEXT_RECOMPILING = true;
await Bun.sleep(1000);
await fullRebuild({
msg: `Detected new \`styles\` directory. Rebuilding ...`,
@@ -78,24 +78,24 @@ export default async function watcherEsbuildCTX() {
const target_files_match = /\.(tsx?|jsx?|css)$/;
if (event !== "rename") {
if (filename.match(target_files_match)) {
if (global.RECOMPILING)
if (global.BUNEXT_RECOMPILING)
return;
owns_recompile = true;
global.RECOMPILING = true;
global.BUNEXT_RECOMPILING = true;
if (filename.match(/.*\.server\.tsx?/)) {
global.IS_SERVER_COMPONENT = true;
global.BUNEXT_IS_SERVER_COMPONENT = true;
}
if (global.BUNDLER_CTX) {
await global.BUNDLER_CTX.rebuild();
if (global.BUNEXT_BUNDLER_CTX) {
await global.BUNEXT_BUNDLER_CTX.rebuild();
}
if (filename.match(/(404|500)\.tsx?/)) {
for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
const controller = global.HMR_CONTROLLERS[i];
for (let i = global.BUNEXT_HMR_CONTROLLERS.length - 1; i >= 0; i--) {
const controller = global.BUNEXT_HMR_CONTROLLERS[i];
try {
controller?.controller?.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`);
}
catch {
global.HMR_CONTROLLERS.splice(i, 1);
global.BUNEXT_HMR_CONTROLLERS.splice(i, 1);
}
}
}
@@ -113,7 +113,7 @@ export default async function watcherEsbuildCTX() {
return reloadWatcher();
if (filename.match(/ /))
return reloadWatcher();
if (global.RECOMPILING)
if (global.BUNEXT_RECOMPILING)
return;
owns_recompile = true;
const action = does_file_exist ? "created" : "deleted";
@@ -133,16 +133,16 @@ export default async function watcherEsbuildCTX() {
}
finally {
if (owns_recompile) {
global.RECOMPILING = false;
global.IS_SERVER_COMPONENT = false;
global.BUNEXT_RECOMPILING = false;
global.BUNEXT_IS_SERVER_COMPONENT = false;
}
}
});
global.PAGES_SRC_WATCHER = pages_src_watcher;
global.BUNEXT_PAGES_SRC_WATCHER = pages_src_watcher;
}
function reloadWatcher() {
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
if (global.BUNEXT_PAGES_SRC_WATCHER) {
global.BUNEXT_PAGES_SRC_WATCHER.close();
watcherEsbuildCTX();
}
}
+2 -2
View File
@@ -48,13 +48,13 @@ export default async function genWebHTML({ component: Main, pageProps, bundledMa
env: {
NODE_ENV: dev ? "development" : "production",
...public_envs,
...global.CONFIG.public_envs,
...global.BUNEXT_CONFIG.public_envs,
},
};
let final_component = (_jsxs("html", { ...html_props, children: [_jsxs("head", { children: [_jsx("meta", { charSet: "utf-8", "data-bunext-head": true }), _jsx("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0", "data-bunext-head": true }), final_meta ? grabWebMetaHTML({ meta: final_meta }) : null, bundledMap?.css_path ? (_jsx("link", { rel: "stylesheet", href: `/${bundledMap.css_path}`, "data-bunext-head": true })) : null, _jsx("script", { dangerouslySetInnerHTML: {
__html: `window.${ClientWindowPagePropsName} = ${serializedProps};\nwindow.process = ${JSON.stringify(client_process)}`,
}, "data-bunext-head": true }), RootHead ? (_jsx(RootHead, { serverRes: pageProps, ctx: routeParams })) : null, Head ? _jsx(Head, { serverRes: pageProps, ctx: routeParams }) : null, bundledMap?.path ? (_jsxs(_Fragment, { children: [_jsx("script", { type: "importmap", dangerouslySetInnerHTML: {
__html: JSON.stringify(global.REACT_IMPORTS_MAP),
__html: JSON.stringify(global.BUNEXT_REACT_IMPORTS_MAP),
}, defer: true, "data-bunext-head": true }), _jsx("script", { src: `/${bundledMap.path}`, type: "module", id: AppData["BunextClientHydrationScriptID"], defer: true, "data-bunext-head": true })] })) : null, is_dev ? (_jsx("script", { defer: true, dangerouslySetInnerHTML: {
__html: page_hydration_script,
}, "data-bunext-head": true })) : null] }), _jsx("body", { children: _jsx("div", { id: ClientRootElementIDName, suppressHydrationWarning: !dev, children: _jsx(Main, { ...pageProps }) }) })] }));
@@ -7,8 +7,8 @@ import grabDirNames from "../../../utils/grab-dir-names";
const { ROOT_DIR } = grabDirNames();
export default async function grabPageBundledReactComponent({ file_path, return_tsx_only, }) {
try {
if (global.SSR_BUNDLER_CTX_MAP?.[file_path]) {
const abs = path.join(ROOT_DIR, global.SSR_BUNDLER_CTX_MAP[file_path].path);
if (global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[file_path]) {
const abs = path.join(ROOT_DIR, global.BUNEXT_SSR_BUNDLER_CTX_MAP[file_path].path);
// Loader.registry.delete(abs);
const mod = await import(`${abs}?t=${Date.now()}`);
const Main = mod.default;
@@ -12,7 +12,7 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
const { server_file_path: root_server_file_path } = root_file_path
? grabPageServerPath({ file_path: root_file_path })
: {};
const root_server_ctx_map = global.SSR_BUNDLER_CTX_MAP[root_server_file_path || ""];
const root_server_ctx_map = global.BUNEXT_SSR_BUNDLER_CTX_MAP[root_server_file_path || ""];
const final_root_server_path = root_server_ctx_map?.local_path
? path.join(ROOT_DIR, root_server_ctx_map.path)
: root_server_file_path;
@@ -33,7 +33,7 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
log.info(`rootServerRes:`, rootServerRes);
}
const { server_file_path } = grabPageServerPath({ file_path });
const page_server_ctx = global.SSR_BUNDLER_CTX_MAP[server_file_path || ""];
const page_server_ctx = global.BUNEXT_SSR_BUNDLER_CTX_MAP[server_file_path || ""];
const final_page_server_path = page_server_ctx?.local_path
? path.join(ROOT_DIR, page_server_ctx.path)
: server_file_path;
+10 -9
View File
@@ -20,7 +20,7 @@ class NotFoundError extends Error {
export default async function grabPageComponent(params) {
const { req, file_path: passed_file_path, debug, return_server_res_only, skip_server_res, is_hydration, } = params;
const url = req?.url ? new URL(req.url) : undefined;
const router = global.ROUTER;
const router = global.BUNEXT_ROUTER;
const is_dev = isDevelopment();
const forwarded_proto = req?.headers.get("x-forwarded-proto");
if (url && forwarded_proto) {
@@ -50,7 +50,7 @@ export default async function grabPageComponent(params) {
// log.error(errMsg);
throw new Error(errMsg);
}
let bundledMap = global.BUNDLER_CTX_MAP[file_path];
let bundledMap = global.BUNEXT_BUNDLER_CTX_MAP[file_path];
if (!bundledMap?.path) {
if (does_error_file_exist) {
throw new Error(`Application Error. Please Check your components. ${match?.filePath} likely exists but has no exported module.`);
@@ -62,7 +62,7 @@ export default async function grabPageComponent(params) {
msg: `Retrying Bundle map for file \`${file_path}\``,
});
await Bun.sleep(1000);
bundledMap = global.BUNDLER_CTX_MAP[file_path];
bundledMap = global.BUNEXT_BUNDLER_CTX_MAP[file_path];
if (bundledMap?.path)
break;
}
@@ -73,7 +73,7 @@ export default async function grabPageComponent(params) {
}
}
if (req && !is_hydration) {
global.BUNDLER_CTX_MAP[file_path].req_url = req.url;
global.BUNEXT_BUNDLER_CTX_MAP[file_path].req_url = req.url;
}
if (debug) {
log.info(`bundledMap:`, bundledMap);
@@ -115,8 +115,9 @@ export default async function grabPageComponent(params) {
error?.name === "NotFoundError" ||
error?.status === 404;
if (!params.retry && is_dev) {
while (global.REBUILD_RETRIES < 2) {
global.REBUILD_RETRIES = global.REBUILD_RETRIES + 1;
while (global.BUNEXT_REBUILD_RETRIES < 2) {
global.BUNEXT_REBUILD_RETRIES =
global.BUNEXT_REBUILD_RETRIES + 1;
await fullRebuild();
await Bun.sleep(200);
const component_retried = await grabPageComponent({
@@ -125,15 +126,15 @@ export default async function grabPageComponent(params) {
});
if (component_retried instanceof Response ||
component_retried.success) {
global.REBUILD_RETRIES = 0;
global.BUNEXT_REBUILD_RETRIES = 0;
await serverPostBuildFn();
return component_retried;
}
}
global.REBUILD_RETRIES = 0;
global.BUNEXT_REBUILD_RETRIES = 0;
}
if (is404) {
global.IS_404_PAGE = true;
global.BUNEXT_IS_404_PAGE = true;
}
else {
log.error(`Error Grabbing Page Component: ${error.message}`);
@@ -3,7 +3,7 @@ import grabDirNames from "../../../utils/grab-dir-names";
import grabPageModules from "./grab-page-modules";
import _ from "lodash";
export default async function grabPageErrorComponent({ error, routeParams, is404, url, }) {
const router = global.ROUTER;
const router = global.BUNEXT_ROUTER;
const { BUNX_ROOT_500_PRESET_COMPONENT, BUNX_ROOT_404_PRESET_COMPONENT } = grabDirNames();
const errorRoute = is404 ? "/404" : "/500";
const presetComponent = is404
@@ -30,7 +30,7 @@ export default async function grabPageErrorComponent({ error, routeParams, is404
};
}
const file_path = match.filePath;
const bundledMap = global.BUNDLER_CTX_MAP?.[file_path];
const bundledMap = global.BUNEXT_BUNDLER_CTX_MAP?.[file_path];
const page_component = await grabPageModules({
file_path: file_path,
query: match?.query,
+4 -3
View File
@@ -108,11 +108,12 @@ async function loadEntry(page_file_path) {
const now = Date.now();
const mod_file_path = toModPath(page_file_path);
const mod_css_path = mod_file_path.replace(/\.js$/, ".css");
if (global.REACT_DOM_MODULE_CACHE.has(page_file_path)) {
return global.REACT_DOM_MODULE_CACHE.get(page_file_path)?.main;
if (global.BUNEXT_REACT_DOM_MODULE_CACHE.has(page_file_path)) {
return global.BUNEXT_REACT_DOM_MODULE_CACHE.get(page_file_path)
?.main;
}
const mod = await import(`${mod_file_path}?t=${now}`);
global.REACT_DOM_MODULE_CACHE.set(page_file_path, {
global.BUNEXT_REACT_DOM_MODULE_CACHE.set(page_file_path, {
main: mod,
css: mod_css_path,
});