Add features: custom server and websocket. Minor refactoring
This commit is contained in:
parent
1611c845b9
commit
98d3cf7da7
12
dist/commands/build/index.js
vendored
12
dist/commands/build/index.js
vendored
@ -1,21 +1,13 @@
|
|||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
import grabConfig from "../../functions/grab-config";
|
|
||||||
import init from "../../functions/init";
|
|
||||||
import allPagesBundler from "../../functions/bundler/all-pages-bundler";
|
import allPagesBundler from "../../functions/bundler/all-pages-bundler";
|
||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
export default function () {
|
export default function () {
|
||||||
return new Command("build")
|
return new Command("build")
|
||||||
.description("Build Project")
|
.description("Build Project")
|
||||||
.action(async () => {
|
.action(async () => {
|
||||||
log.banner();
|
|
||||||
log.build("Building Project ...");
|
|
||||||
process.env.NODE_ENV = "production";
|
process.env.NODE_ENV = "production";
|
||||||
await init();
|
process.env.BUILD = "true";
|
||||||
const config = (await grabConfig()) || {};
|
log.build("Building Project ...");
|
||||||
global.CONFIG = {
|
|
||||||
...config,
|
|
||||||
development: true,
|
|
||||||
};
|
|
||||||
allPagesBundler({
|
allPagesBundler({
|
||||||
exit_after_first_build: true,
|
exit_after_first_build: true,
|
||||||
});
|
});
|
||||||
|
|||||||
14
dist/commands/dev/index.js
vendored
14
dist/commands/dev/index.js
vendored
@ -1,20 +1,14 @@
|
|||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
import grabConfig from "../../functions/grab-config";
|
|
||||||
import startServer from "../../functions/server/start-server";
|
import startServer from "../../functions/server/start-server";
|
||||||
import init from "../../functions/init";
|
|
||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
|
import bunextInit from "../../functions/bunext-init";
|
||||||
export default function () {
|
export default function () {
|
||||||
return new Command("dev")
|
return new Command("dev")
|
||||||
.description("Run development server")
|
.description("Run development server")
|
||||||
.action(async () => {
|
.action(async () => {
|
||||||
log.banner();
|
process.env.NODE_ENV == "development";
|
||||||
log.info("Running development server ...");
|
log.info("Running development server ...");
|
||||||
await init();
|
await bunextInit();
|
||||||
const config = (await grabConfig()) || {};
|
await startServer();
|
||||||
global.CONFIG = {
|
|
||||||
...config,
|
|
||||||
development: true,
|
|
||||||
};
|
|
||||||
await startServer({ dev: true });
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
26
dist/commands/index.d.ts
vendored
26
dist/commands/index.d.ts
vendored
@ -1,26 +1,2 @@
|
|||||||
#!/usr/bin/env bun
|
#!/usr/bin/env bun
|
||||||
import { type Ora } from "ora";
|
export {};
|
||||||
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[];
|
|
||||||
}
|
|
||||||
|
|||||||
16
dist/commands/index.js
vendored
16
dist/commands/index.js
vendored
@ -2,23 +2,7 @@
|
|||||||
import { program } from "commander";
|
import { program } from "commander";
|
||||||
import start from "./start";
|
import start from "./start";
|
||||||
import dev from "./dev";
|
import dev from "./dev";
|
||||||
import ora, {} from "ora";
|
|
||||||
import init from "../functions/init";
|
|
||||||
import grabDirNames from "../utils/grab-dir-names";
|
|
||||||
import build from "./build";
|
import build from "./build";
|
||||||
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
|
* # Describe Program
|
||||||
*/
|
*/
|
||||||
|
|||||||
10
dist/commands/start/index.js
vendored
10
dist/commands/start/index.js
vendored
@ -1,18 +1,14 @@
|
|||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
import grabConfig from "../../functions/grab-config";
|
|
||||||
import startServer from "../../functions/server/start-server";
|
import startServer from "../../functions/server/start-server";
|
||||||
import init from "../../functions/init";
|
|
||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
|
import bunextInit from "../../functions/bunext-init";
|
||||||
export default function () {
|
export default function () {
|
||||||
return new Command("start")
|
return new Command("start")
|
||||||
.description("Start production server")
|
.description("Start production server")
|
||||||
.action(async () => {
|
.action(async () => {
|
||||||
log.banner();
|
|
||||||
log.info("Starting production server ...");
|
|
||||||
process.env.NODE_ENV = "production";
|
process.env.NODE_ENV = "production";
|
||||||
await init();
|
log.info("Starting production server ...");
|
||||||
const config = await grabConfig();
|
await bunextInit();
|
||||||
global.CONFIG = { ...config };
|
|
||||||
await startServer();
|
await startServer();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
26
dist/functions/bunext-init.d.ts
vendored
Normal file
26
dist/functions/bunext-init.d.ts
vendored
Normal 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
dist/functions/bunext-init.js
vendored
Normal file
61
dist/functions/bunext-init.js
vendored
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
dist/functions/init.js
vendored
8
dist/functions/init.js
vendored
@ -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 grabDirNames from "../utils/grab-dir-names";
|
||||||
import { execSync } from "child_process";
|
import { execSync } from "child_process";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import grabConfig from "./grab-config";
|
||||||
export default async function () {
|
export default async function () {
|
||||||
const dirNames = grabDirNames();
|
const dirNames = grabDirNames();
|
||||||
execSync(`rm -rf ${dirNames.BUNEXT_CACHE_DIR}`);
|
execSync(`rm -rf ${dirNames.BUNEXT_CACHE_DIR}`);
|
||||||
@ -26,4 +27,9 @@ export default async function () {
|
|||||||
writeFileSync(dir, basicConfig);
|
writeFileSync(dir, basicConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const config = (await grabConfig()) || {};
|
||||||
|
global.CONFIG = {
|
||||||
|
...config,
|
||||||
|
development: true,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
6
dist/functions/server/server-params-gen.d.ts
vendored
6
dist/functions/server/server-params-gen.d.ts
vendored
@ -1,6 +1,2 @@
|
|||||||
import type { ServeOptions } from "bun";
|
import type { ServeOptions } from "bun";
|
||||||
type Params = {
|
export default function (): Promise<ServeOptions>;
|
||||||
dev?: boolean;
|
|
||||||
};
|
|
||||||
export default function (params?: Params): Promise<ServeOptions>;
|
|
||||||
export {};
|
|
||||||
|
|||||||
15
dist/functions/server/server-params-gen.js
vendored
15
dist/functions/server/server-params-gen.js
vendored
@ -1,17 +1,20 @@
|
|||||||
import grabAppPort from "../../utils/grab-app-port";
|
import grabAppPort from "../../utils/grab-app-port";
|
||||||
import isDevelopment from "../../utils/is-development";
|
import isDevelopment from "../../utils/is-development";
|
||||||
import bunextRequestHandler from "./bunext-req-handler";
|
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 port = grabAppPort();
|
||||||
const is_dev = isDevelopment();
|
const development = isDevelopment();
|
||||||
|
const config = await grabConfig();
|
||||||
return {
|
return {
|
||||||
async fetch(req, server) {
|
async fetch(req, server) {
|
||||||
return await bunextRequestHandler({ req });
|
return await bunextRequestHandler({ req });
|
||||||
},
|
},
|
||||||
port,
|
port,
|
||||||
// idleTimeout: 0,
|
idleTimeout: development ? 0 : undefined,
|
||||||
development: {
|
development,
|
||||||
hmr: true,
|
websocket: config?.websocket,
|
||||||
},
|
..._.omit(config?.serverOptions || {}, ["fetch"]),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
6
dist/functions/server/start-server.d.ts
vendored
6
dist/functions/server/start-server.d.ts
vendored
@ -1,5 +1 @@
|
|||||||
type Params = {
|
export default function startServer(): Promise<import("bun").Server>;
|
||||||
dev?: boolean;
|
|
||||||
};
|
|
||||||
export default function startServer(params?: Params): Promise<import("bun").Server>;
|
|
||||||
export {};
|
|
||||||
|
|||||||
45
dist/functions/server/start-server.js
vendored
45
dist/functions/server/start-server.js
vendored
@ -1,53 +1,10 @@
|
|||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import AppNames from "../../utils/grab-app-names";
|
|
||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
import allPagesBundler from "../bundler/all-pages-bundler";
|
|
||||||
import serverParamsGen from "./server-params-gen";
|
import serverParamsGen from "./server-params-gen";
|
||||||
import watcher from "./watcher";
|
export default async function startServer() {
|
||||||
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;
|
|
||||||
const serverParams = await serverParamsGen();
|
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);
|
const server = Bun.serve(serverParams);
|
||||||
global.SERVER = server;
|
global.SERVER = server;
|
||||||
log.server(`http://localhost:${server.port}`);
|
log.server(`http://localhost:${server.port}`);
|
||||||
/**
|
|
||||||
* First Rebuild to Avoid errors
|
|
||||||
*/
|
|
||||||
if (params?.dev && global.BUNDLER_CTX) {
|
|
||||||
await global.BUNDLER_CTX.rebuild();
|
|
||||||
}
|
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,5 +3,5 @@ type Params = {
|
|||||||
tsx: string;
|
tsx: string;
|
||||||
out_file: 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 {};
|
export {};
|
||||||
|
|||||||
12
dist/index.d.ts
vendored
12
dist/index.d.ts
vendored
@ -1,5 +1,17 @@
|
|||||||
|
import bunextInit from "./functions/bunext-init";
|
||||||
import bunextRequestHandler from "./functions/server/bunext-req-handler";
|
import bunextRequestHandler from "./functions/server/bunext-req-handler";
|
||||||
declare const bunext: {
|
declare const bunext: {
|
||||||
bunextRequestHandler: typeof bunextRequestHandler;
|
bunextRequestHandler: typeof bunextRequestHandler;
|
||||||
|
bunextLog: {
|
||||||
|
info: (msg: string, log?: any) => void;
|
||||||
|
success: (msg: string, log?: any) => void;
|
||||||
|
error: (msg: string | Error) => void;
|
||||||
|
warn: (msg: string) => void;
|
||||||
|
build: (msg: string) => void;
|
||||||
|
watch: (msg: string) => void;
|
||||||
|
server: (url: string) => void;
|
||||||
|
banner: () => void;
|
||||||
|
};
|
||||||
|
bunextInit: typeof bunextInit;
|
||||||
};
|
};
|
||||||
export default bunext;
|
export default bunext;
|
||||||
|
|||||||
4
dist/index.js
vendored
4
dist/index.js
vendored
@ -1,5 +1,9 @@
|
|||||||
|
import bunextInit from "./functions/bunext-init";
|
||||||
import bunextRequestHandler from "./functions/server/bunext-req-handler";
|
import bunextRequestHandler from "./functions/server/bunext-req-handler";
|
||||||
|
import { log } from "./utils/log";
|
||||||
const bunext = {
|
const bunext = {
|
||||||
bunextRequestHandler,
|
bunextRequestHandler,
|
||||||
|
bunextLog: log,
|
||||||
|
bunextInit,
|
||||||
};
|
};
|
||||||
export default bunext;
|
export default bunext;
|
||||||
|
|||||||
4
dist/types/index.d.ts
vendored
4
dist/types/index.d.ts
vendored
@ -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";
|
import type { FC, JSX, ReactNode } from "react";
|
||||||
export type ServerProps = {
|
export type ServerProps = {
|
||||||
params: Record<string, string>;
|
params: Record<string, string>;
|
||||||
@ -46,6 +46,8 @@ export type BunextConfig = {
|
|||||||
development?: boolean;
|
development?: boolean;
|
||||||
middleware?: (params: BunextConfigMiddlewareParams) => Promise<Response | undefined> | Response | undefined;
|
middleware?: (params: BunextConfigMiddlewareParams) => Promise<Response | undefined> | Response | undefined;
|
||||||
defaultCacheExpiry?: number;
|
defaultCacheExpiry?: number;
|
||||||
|
websocket?: WebSocketHandler<any>;
|
||||||
|
serverOptions?: ServeOptions;
|
||||||
};
|
};
|
||||||
export type BunextConfigMiddlewareParams = {
|
export type BunextConfigMiddlewareParams = {
|
||||||
req: Request;
|
req: Request;
|
||||||
|
|||||||
31
examples/custom-server/server.ts
Normal file
31
examples/custom-server/server.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import bunext from "../../dist"; // => @moduletrace/bunext
|
||||||
|
|
||||||
|
const development = process.env.NODE_ENV == "development";
|
||||||
|
const port = process.env.PORT || 3700;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize Bunext
|
||||||
|
*/
|
||||||
|
await bunext.bunextInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start your custom server
|
||||||
|
*/
|
||||||
|
const server = Bun.serve({
|
||||||
|
routes: {
|
||||||
|
"/*": {
|
||||||
|
async GET(req) {
|
||||||
|
return await bunext.bunextRequestHandler({ req });
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Set this to prevent HMR timeout warnings in the
|
||||||
|
* browser console in development mode.
|
||||||
|
*/
|
||||||
|
idleTimeout: development ? 0 : undefined,
|
||||||
|
development,
|
||||||
|
port,
|
||||||
|
});
|
||||||
|
|
||||||
|
bunext.bunextLog.info(`Server running on http://localhost:${server.port} ...`);
|
||||||
8
examples/websocket/bunext.config.ts
Normal file
8
examples/websocket/bunext.config.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import type { BunextConfig } from "../../dist/types";
|
||||||
|
import { BunextWebsocket } from "./websocket";
|
||||||
|
|
||||||
|
const config: BunextConfig = {
|
||||||
|
websocket: BunextWebsocket,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default config;
|
||||||
7
examples/websocket/websocket.ts
Normal file
7
examples/websocket/websocket.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import type { WebSocketHandler } from "bun";
|
||||||
|
|
||||||
|
export const BunextWebsocket: WebSocketHandler<any> = {
|
||||||
|
message(ws, message) {
|
||||||
|
console.log(`WS Message => ${message}`);
|
||||||
|
},
|
||||||
|
};
|
||||||
@ -1,7 +1,4 @@
|
|||||||
import { Command } from "commander";
|
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 allPagesBundler from "../../functions/bundler/all-pages-bundler";
|
||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
|
|
||||||
@ -9,19 +6,10 @@ export default function () {
|
|||||||
return new Command("build")
|
return new Command("build")
|
||||||
.description("Build Project")
|
.description("Build Project")
|
||||||
.action(async () => {
|
.action(async () => {
|
||||||
log.banner();
|
|
||||||
log.build("Building Project ...");
|
|
||||||
|
|
||||||
process.env.NODE_ENV = "production";
|
process.env.NODE_ENV = "production";
|
||||||
|
process.env.BUILD = "true";
|
||||||
|
|
||||||
await init();
|
log.build("Building Project ...");
|
||||||
|
|
||||||
const config: BunextConfig = (await grabConfig()) || {};
|
|
||||||
|
|
||||||
global.CONFIG = {
|
|
||||||
...config,
|
|
||||||
development: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
allPagesBundler({
|
allPagesBundler({
|
||||||
exit_after_first_build: true,
|
exit_after_first_build: true,
|
||||||
|
|||||||
@ -1,26 +1,18 @@
|
|||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
import grabConfig from "../../functions/grab-config";
|
|
||||||
import startServer from "../../functions/server/start-server";
|
import startServer from "../../functions/server/start-server";
|
||||||
import init from "../../functions/init";
|
|
||||||
import type { BunextConfig } from "../../types";
|
|
||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
|
import bunextInit from "../../functions/bunext-init";
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
return new Command("dev")
|
return new Command("dev")
|
||||||
.description("Run development server")
|
.description("Run development server")
|
||||||
.action(async () => {
|
.action(async () => {
|
||||||
log.banner();
|
process.env.NODE_ENV == "development";
|
||||||
|
|
||||||
log.info("Running development server ...");
|
log.info("Running development server ...");
|
||||||
|
|
||||||
await init();
|
await bunextInit();
|
||||||
|
|
||||||
const config: BunextConfig = (await grabConfig()) || {};
|
await startServer();
|
||||||
|
|
||||||
global.CONFIG = {
|
|
||||||
...config,
|
|
||||||
development: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
await startServer({ dev: true });
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,58 +3,7 @@
|
|||||||
import { program } from "commander";
|
import { program } from "commander";
|
||||||
import start from "./start";
|
import start from "./start";
|
||||||
import dev from "./dev";
|
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 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
|
* # Describe Program
|
||||||
|
|||||||
@ -1,23 +1,17 @@
|
|||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
import grabConfig from "../../functions/grab-config";
|
|
||||||
import startServer from "../../functions/server/start-server";
|
import startServer from "../../functions/server/start-server";
|
||||||
import init from "../../functions/init";
|
|
||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
|
import bunextInit from "../../functions/bunext-init";
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
return new Command("start")
|
return new Command("start")
|
||||||
.description("Start production server")
|
.description("Start production server")
|
||||||
.action(async () => {
|
.action(async () => {
|
||||||
log.banner();
|
|
||||||
log.info("Starting production server ...");
|
|
||||||
|
|
||||||
process.env.NODE_ENV = "production";
|
process.env.NODE_ENV = "production";
|
||||||
|
|
||||||
await init();
|
log.info("Starting production server ...");
|
||||||
|
|
||||||
const config = await grabConfig();
|
await bunextInit();
|
||||||
|
|
||||||
global.CONFIG = { ...config };
|
|
||||||
|
|
||||||
await startServer();
|
await startServer();
|
||||||
});
|
});
|
||||||
|
|||||||
102
src/functions/bunext-init.ts
Normal file
102
src/functions/bunext-init.ts
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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 grabDirNames from "../utils/grab-dir-names";
|
||||||
import { execSync } from "child_process";
|
import { execSync } from "child_process";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import grabConfig from "./grab-config";
|
||||||
|
import type { BunextConfig } from "../types";
|
||||||
|
|
||||||
export default async function () {
|
export default async function () {
|
||||||
const dirNames = grabDirNames();
|
const dirNames = grabDirNames();
|
||||||
@ -39,4 +41,11 @@ export default async function () {
|
|||||||
writeFileSync(dir, basicConfig);
|
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 grabAppPort from "../../utils/grab-app-port";
|
||||||
import isDevelopment from "../../utils/is-development";
|
import isDevelopment from "../../utils/is-development";
|
||||||
import bunextRequestHandler from "./bunext-req-handler";
|
import bunextRequestHandler from "./bunext-req-handler";
|
||||||
|
import grabConfig from "../grab-config";
|
||||||
|
import _ from "lodash";
|
||||||
|
|
||||||
type Params = {
|
export default async function (): Promise<ServeOptions> {
|
||||||
dev?: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function (params?: Params): Promise<ServeOptions> {
|
|
||||||
const port = grabAppPort();
|
const port = grabAppPort();
|
||||||
|
|
||||||
const is_dev = isDevelopment();
|
const development = isDevelopment();
|
||||||
|
const config = await grabConfig();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
async fetch(req, server) {
|
async fetch(req, server) {
|
||||||
return await bunextRequestHandler({ req });
|
return await bunextRequestHandler({ req });
|
||||||
},
|
},
|
||||||
port,
|
port,
|
||||||
// idleTimeout: 0,
|
idleTimeout: development ? 0 : undefined,
|
||||||
development: {
|
development,
|
||||||
hmr: true,
|
websocket: config?.websocket,
|
||||||
},
|
..._.omit(config?.serverOptions || {}, ["fetch"]),
|
||||||
} as ServeOptions;
|
} as ServeOptions;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,70 +1,15 @@
|
|||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import AppNames from "../../utils/grab-app-names";
|
|
||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
import allPagesBundler from "../bundler/all-pages-bundler";
|
|
||||||
import serverParamsGen from "./server-params-gen";
|
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();
|
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);
|
const server = Bun.serve(serverParams);
|
||||||
|
|
||||||
global.SERVER = server;
|
global.SERVER = server;
|
||||||
|
|
||||||
log.server(`http://localhost:${server.port}`);
|
log.server(`http://localhost:${server.port}`);
|
||||||
|
|
||||||
/**
|
|
||||||
* First Rebuild to Avoid errors
|
|
||||||
*/
|
|
||||||
if (params?.dev && global.BUNDLER_CTX) {
|
|
||||||
await global.BUNDLER_CTX.rebuild();
|
|
||||||
}
|
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,10 @@
|
|||||||
|
import bunextInit from "./functions/bunext-init";
|
||||||
import bunextRequestHandler from "./functions/server/bunext-req-handler";
|
import bunextRequestHandler from "./functions/server/bunext-req-handler";
|
||||||
|
import { log } from "./utils/log";
|
||||||
|
|
||||||
const bunext = {
|
const bunext = {
|
||||||
bunextRequestHandler,
|
bunextRequestHandler,
|
||||||
|
bunextLog: log,
|
||||||
|
bunextInit,
|
||||||
};
|
};
|
||||||
export default bunext;
|
export default bunext;
|
||||||
|
|||||||
@ -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";
|
import type { FC, JSX, ReactNode } from "react";
|
||||||
|
|
||||||
export type ServerProps = {
|
export type ServerProps = {
|
||||||
@ -52,6 +52,8 @@ export type BunextConfig = {
|
|||||||
params: BunextConfigMiddlewareParams,
|
params: BunextConfigMiddlewareParams,
|
||||||
) => Promise<Response | undefined> | Response | undefined;
|
) => Promise<Response | undefined> | Response | undefined;
|
||||||
defaultCacheExpiry?: number;
|
defaultCacheExpiry?: number;
|
||||||
|
websocket?: WebSocketHandler<any>;
|
||||||
|
serverOptions?: ServeOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type BunextConfigMiddlewareParams = {
|
export type BunextConfigMiddlewareParams = {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user