Add features: custom server and websocket. Minor refactoring

This commit is contained in:
2026-03-21 07:42:38 +01:00
parent 1611c845b9
commit 98d3cf7da7
29 changed files with 321 additions and 286 deletions
+26
View File
@@ -0,0 +1,26 @@
import { type Ora } from "ora";
import type { BundlerCTXMap, BunextConfig, GlobalHMRControllerObject, PageFiles } from "../types";
import type { FileSystemRouter, Server } from "bun";
import type { BuildContext } from "esbuild";
import { type FSWatcher } from "fs";
/**
* # Declare Global Variables
*/
declare global {
var ORA_SPINNER: Ora;
var CONFIG: BunextConfig;
var SERVER: Server | undefined;
var RECOMPILING: boolean;
var WATCHER_TIMEOUT: any;
var ROUTER: FileSystemRouter;
var HMR_CONTROLLERS: GlobalHMRControllerObject[];
var LAST_BUILD_TIME: number;
var BUNDLER_CTX: BuildContext | undefined;
var BUNDLER_CTX_MAP: BundlerCTXMap[] | undefined;
var IS_FIRST_BUNDLE_READY: boolean;
var BUNDLER_REBUILDS: 0;
var PAGES_SRC_WATCHER: FSWatcher | undefined;
var CURRENT_VERSION: string | undefined;
var PAGE_FILES: PageFiles[];
}
export default function bunextInit(): Promise<void>;
+61
View File
@@ -0,0 +1,61 @@
import ora, {} from "ora";
import grabDirNames from "../utils/grab-dir-names";
import { readFileSync } from "fs";
import init from "./init";
import isDevelopment from "../utils/is-development";
import allPagesBundler from "./bundler/all-pages-bundler";
import serverPostBuildFn from "./server/server-post-build-fn";
import watcher from "./server/watcher";
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 = [];
global.IS_FIRST_BUNDLE_READY = false;
global.BUNDLER_REBUILDS = 0;
global.PAGE_FILES = [];
await init();
const { PAGES_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
const router = new Bun.FileSystemRouter({
style: "nextjs",
dir: PAGES_DIR,
});
global.ROUTER = router;
const is_dev = isDevelopment();
if (is_dev) {
await allPagesBundler({
watch: true,
post_build_fn: serverPostBuildFn,
});
watcher();
}
else {
const artifacts = EJSON.parse(readFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, "utf-8"));
if (!artifacts?.[0]) {
log.error("Please build first.");
process.exit(1);
}
global.BUNDLER_CTX_MAP = artifacts;
global.IS_FIRST_BUNDLE_READY = true;
cron();
}
let bundle_ready_retries = 0;
const MAX_BUNDLE_READY_RETRIES = 10;
while (!global.IS_FIRST_BUNDLE_READY) {
if (bundle_ready_retries > MAX_BUNDLE_READY_RETRIES) {
log.error("Couldn't grab first bundle for dev environment");
process.exit(1);
}
bundle_ready_retries++;
await Bun.sleep(500);
}
/**
* First Rebuild to Avoid errors
*/
if (is_dev && global.BUNDLER_CTX) {
await global.BUNDLER_CTX.rebuild();
}
}
+7 -1
View File
@@ -1,7 +1,8 @@
import { existsSync, mkdirSync, statSync, writeFileSync } from "fs";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import grabDirNames from "../utils/grab-dir-names";
import { execSync } from "child_process";
import path from "path";
import grabConfig from "./grab-config";
export default async function () {
const dirNames = grabDirNames();
execSync(`rm -rf ${dirNames.BUNEXT_CACHE_DIR}`);
@@ -26,4 +27,9 @@ export default async function () {
writeFileSync(dir, basicConfig);
}
}
const config = (await grabConfig()) || {};
global.CONFIG = {
...config,
development: true,
};
}
+1 -5
View File
@@ -1,6 +1,2 @@
import type { ServeOptions } from "bun";
type Params = {
dev?: boolean;
};
export default function (params?: Params): Promise<ServeOptions>;
export {};
export default function (): Promise<ServeOptions>;
+9 -6
View File
@@ -1,17 +1,20 @@
import grabAppPort from "../../utils/grab-app-port";
import isDevelopment from "../../utils/is-development";
import bunextRequestHandler from "./bunext-req-handler";
export default async function (params) {
import grabConfig from "../grab-config";
import _ from "lodash";
export default async function () {
const port = grabAppPort();
const is_dev = isDevelopment();
const development = isDevelopment();
const config = await grabConfig();
return {
async fetch(req, server) {
return await bunextRequestHandler({ req });
},
port,
// idleTimeout: 0,
development: {
hmr: true,
},
idleTimeout: development ? 0 : undefined,
development,
websocket: config?.websocket,
..._.omit(config?.serverOptions || {}, ["fetch"]),
};
}
+1 -5
View File
@@ -1,5 +1 @@
type Params = {
dev?: boolean;
};
export default function startServer(params?: Params): Promise<import("bun").Server>;
export {};
export default function startServer(): Promise<import("bun").Server>;
+1 -44
View File
@@ -1,53 +1,10 @@
import _ from "lodash";
import AppNames from "../../utils/grab-app-names";
import { log } from "../../utils/log";
import allPagesBundler from "../bundler/all-pages-bundler";
import serverParamsGen from "./server-params-gen";
import watcher from "./watcher";
import serverPostBuildFn from "./server-post-build-fn";
import grabDirNames from "../../utils/grab-dir-names";
import EJSON from "../../utils/ejson";
import { readFileSync } from "fs";
import cron from "./cron";
const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
export default async function startServer(params) {
const { name } = AppNames;
export default async function startServer() {
const serverParams = await serverParamsGen();
if (params?.dev) {
await allPagesBundler({
watch: true,
post_build_fn: serverPostBuildFn,
});
watcher();
}
else {
const artifacts = EJSON.parse(readFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, "utf-8"));
if (!artifacts?.[0]) {
log.error("Please build first.");
process.exit(1);
}
global.BUNDLER_CTX_MAP = artifacts;
global.IS_FIRST_BUNDLE_READY = true;
cron();
}
let bundle_ready_retries = 0;
const MAX_BUNDLE_READY_RETRIES = 10;
while (!global.IS_FIRST_BUNDLE_READY) {
if (bundle_ready_retries > MAX_BUNDLE_READY_RETRIES) {
log.error("Couldn't grab first bundle for dev environment");
process.exit(1);
}
bundle_ready_retries++;
await Bun.sleep(500);
}
const server = Bun.serve(serverParams);
global.SERVER = server;
log.server(`http://localhost:${server.port}`);
/**
* First Rebuild to Avoid errors
*/
if (params?.dev && global.BUNDLER_CTX) {
await global.BUNDLER_CTX.rebuild();
}
return server;
}
+1 -1
View File
@@ -3,5 +3,5 @@ type Params = {
tsx: string;
out_file: string;
};
export default function writeHMRTsxModule({ tsx, out_file }: Params): Promise<Pick<BundlerCTXMap, "css_path" | "path" | "hash" | "type"> | undefined>;
export default function writeHMRTsxModule({ tsx, out_file }: Params): Promise<Pick<BundlerCTXMap, "path" | "css_path" | "hash" | "type"> | undefined>;
export {};