Add features: custom server and websocket. Minor refactoring
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
import { Command } from "commander";
|
||||
import grabConfig from "../../functions/grab-config";
|
||||
import init from "../../functions/init";
|
||||
import type { BunextConfig } from "../../types";
|
||||
import allPagesBundler from "../../functions/bundler/all-pages-bundler";
|
||||
import { log } from "../../utils/log";
|
||||
|
||||
@@ -9,19 +6,10 @@ export default function () {
|
||||
return new Command("build")
|
||||
.description("Build Project")
|
||||
.action(async () => {
|
||||
log.banner();
|
||||
log.build("Building Project ...");
|
||||
|
||||
process.env.NODE_ENV = "production";
|
||||
process.env.BUILD = "true";
|
||||
|
||||
await init();
|
||||
|
||||
const config: BunextConfig = (await grabConfig()) || {};
|
||||
|
||||
global.CONFIG = {
|
||||
...config,
|
||||
development: true,
|
||||
};
|
||||
log.build("Building Project ...");
|
||||
|
||||
allPagesBundler({
|
||||
exit_after_first_build: true,
|
||||
|
||||
@@ -1,26 +1,18 @@
|
||||
import { Command } from "commander";
|
||||
import grabConfig from "../../functions/grab-config";
|
||||
import startServer from "../../functions/server/start-server";
|
||||
import init from "../../functions/init";
|
||||
import type { BunextConfig } from "../../types";
|
||||
import { log } from "../../utils/log";
|
||||
import bunextInit from "../../functions/bunext-init";
|
||||
|
||||
export default function () {
|
||||
return new Command("dev")
|
||||
.description("Run development server")
|
||||
.action(async () => {
|
||||
log.banner();
|
||||
process.env.NODE_ENV == "development";
|
||||
|
||||
log.info("Running development server ...");
|
||||
|
||||
await init();
|
||||
await bunextInit();
|
||||
|
||||
const config: BunextConfig = (await grabConfig()) || {};
|
||||
|
||||
global.CONFIG = {
|
||||
...config,
|
||||
development: true,
|
||||
};
|
||||
|
||||
await startServer({ dev: true });
|
||||
await startServer();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,58 +3,7 @@
|
||||
import { program } from "commander";
|
||||
import start from "./start";
|
||||
import dev from "./dev";
|
||||
import ora, { type Ora } from "ora";
|
||||
import type {
|
||||
BundlerCTXMap,
|
||||
BunextConfig,
|
||||
GlobalHMRControllerObject,
|
||||
PageFiles,
|
||||
} from "../types";
|
||||
import type { FileSystemRouter, Server } from "bun";
|
||||
import init from "../functions/init";
|
||||
import grabDirNames from "../utils/grab-dir-names";
|
||||
import build from "./build";
|
||||
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[];
|
||||
}
|
||||
|
||||
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 } = grabDirNames();
|
||||
|
||||
const router = new Bun.FileSystemRouter({
|
||||
style: "nextjs",
|
||||
dir: PAGES_DIR,
|
||||
});
|
||||
|
||||
global.ROUTER = router;
|
||||
|
||||
/**
|
||||
* # Describe Program
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
import { Command } from "commander";
|
||||
import grabConfig from "../../functions/grab-config";
|
||||
import startServer from "../../functions/server/start-server";
|
||||
import init from "../../functions/init";
|
||||
import { log } from "../../utils/log";
|
||||
import bunextInit from "../../functions/bunext-init";
|
||||
|
||||
export default function () {
|
||||
return new Command("start")
|
||||
.description("Start production server")
|
||||
.action(async () => {
|
||||
log.banner();
|
||||
log.info("Starting production server ...");
|
||||
|
||||
process.env.NODE_ENV = "production";
|
||||
|
||||
await init();
|
||||
log.info("Starting production server ...");
|
||||
|
||||
const config = await grabConfig();
|
||||
|
||||
global.CONFIG = { ...config };
|
||||
await bunextInit();
|
||||
|
||||
await startServer();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import ora, { type Ora } from "ora";
|
||||
import type {
|
||||
BundlerCTXMap,
|
||||
BunextConfig,
|
||||
GlobalHMRControllerObject,
|
||||
PageFiles,
|
||||
} from "../types";
|
||||
import type { FileSystemRouter, Server } from "bun";
|
||||
import grabDirNames from "../utils/grab-dir-names";
|
||||
import type { BuildContext } from "esbuild";
|
||||
import { readFileSync, type FSWatcher } 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";
|
||||
|
||||
/**
|
||||
* # 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 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"),
|
||||
) as BundlerCTXMap[] | undefined;
|
||||
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();
|
||||
}
|
||||
}
|
||||
+10
-1
@@ -1,7 +1,9 @@
|
||||
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";
|
||||
import type { BunextConfig } from "../types";
|
||||
|
||||
export default async function () {
|
||||
const dirNames = grabDirNames();
|
||||
@@ -39,4 +41,11 @@ export default async function () {
|
||||
writeFileSync(dir, basicConfig);
|
||||
}
|
||||
}
|
||||
|
||||
const config: BunextConfig = (await grabConfig()) || {};
|
||||
|
||||
global.CONFIG = {
|
||||
...config,
|
||||
development: true,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,24 +2,23 @@ import type { ServeOptions } from "bun";
|
||||
import grabAppPort from "../../utils/grab-app-port";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import bunextRequestHandler from "./bunext-req-handler";
|
||||
import grabConfig from "../grab-config";
|
||||
import _ from "lodash";
|
||||
|
||||
type Params = {
|
||||
dev?: boolean;
|
||||
};
|
||||
|
||||
export default async function (params?: Params): Promise<ServeOptions> {
|
||||
export default async function (): Promise<ServeOptions> {
|
||||
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"]),
|
||||
} as ServeOptions;
|
||||
}
|
||||
|
||||
@@ -1,70 +1,15 @@
|
||||
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 type { BundlerCTXMap } from "../../types";
|
||||
import cron from "./cron";
|
||||
|
||||
const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
||||
|
||||
type Params = {
|
||||
dev?: boolean;
|
||||
};
|
||||
|
||||
export default async function startServer(params?: 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"),
|
||||
) as BundlerCTXMap[] | undefined;
|
||||
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,6 +1,10 @@
|
||||
import bunextInit from "./functions/bunext-init";
|
||||
import bunextRequestHandler from "./functions/server/bunext-req-handler";
|
||||
import { log } from "./utils/log";
|
||||
|
||||
const bunext = {
|
||||
bunextRequestHandler,
|
||||
bunextLog: log,
|
||||
bunextInit,
|
||||
};
|
||||
export default bunext;
|
||||
|
||||
+3
-1
@@ -1,4 +1,4 @@
|
||||
import type { MatchedRoute, Server } from "bun";
|
||||
import type { MatchedRoute, ServeOptions, Server, WebSocketHandler } from "bun";
|
||||
import type { FC, JSX, ReactNode } from "react";
|
||||
|
||||
export type ServerProps = {
|
||||
@@ -52,6 +52,8 @@ export type BunextConfig = {
|
||||
params: BunextConfigMiddlewareParams,
|
||||
) => Promise<Response | undefined> | Response | undefined;
|
||||
defaultCacheExpiry?: number;
|
||||
websocket?: WebSocketHandler<any>;
|
||||
serverOptions?: ServeOptions;
|
||||
};
|
||||
|
||||
export type BunextConfigMiddlewareParams = {
|
||||
|
||||
Reference in New Issue
Block a user