diff --git a/dist/commands/build/index.js b/dist/commands/build/index.js index 095abeb..cfb1425 100644 --- a/dist/commands/build/index.js +++ b/dist/commands/build/index.js @@ -1,9 +1,8 @@ import { Command } from "commander"; import { log } from "../../utils/log"; -import init from "../../functions/init"; import grabDirNames from "../../utils/grab-dir-names"; import { rmSync } from "fs"; -import allPagesESBuildContextBundler from "../../functions/bundler/all-pages-esbuild-context-bundler"; +import bunextInit from "../../functions/bunext-init"; const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames(); export default function () { return new Command("build") @@ -15,13 +14,8 @@ export default function () { } catch (error) { } global.SKIPPED_BROWSER_MODULES = new Set(); - // await rewritePagesModule(); - await init(); - log.banner(); - log.build("Building Project ..."); - // await allPagesBunBundler(); - // await allPagesBundler(); - await allPagesESBuildContextBundler(); + await bunextInit({ build_only: true }); + log.success("Modules Built Successfully!"); process.exit(); }); } diff --git a/dist/commands/dev/index.js b/dist/commands/dev/index.js index 6b2aebd..ca8d197 100644 --- a/dist/commands/dev/index.js +++ b/dist/commands/dev/index.js @@ -35,13 +35,27 @@ async function dev() { NODE_ENV: "development", }, }; - let dev_process = Bun.spawn(spawn_options); - retries++; + let dev_process; + try { + dev_process = Bun.spawn(spawn_options); + } + catch (error) { + console.error(`Failed to start dev process:`, error); + retries++; + timeout = setTimeout(() => { + retries = 0; + }, 10000); + return await dev(); + } + const exited = await dev_process.exited; + if (exited) { + retries++; + timeout = setTimeout(() => { + retries = 0; + }, 10000); + return await dev(); + } timeout = setTimeout(() => { retries = 0; }, 10000); - const exited = await dev_process.exited; - if (exited) { - return await dev(); - } } diff --git a/dist/commands/start/index.js b/dist/commands/start/index.js index 7abff6e..2e53216 100644 --- a/dist/commands/start/index.js +++ b/dist/commands/start/index.js @@ -34,13 +34,27 @@ async function start() { NODE_ENV: "production", }, }; - let dev_process = Bun.spawn(spawn_options); - retries++; + let dev_process; + try { + dev_process = Bun.spawn(spawn_options); + } + catch (error) { + console.error(`Failed to start production process:`, error); + retries++; + timeout = setTimeout(() => { + retries = 0; + }, 10000); + return await start(); + } + const exited = await dev_process.exited; + if (exited) { + retries++; + timeout = setTimeout(() => { + retries = 0; + }, 10000); + return await start(); + } timeout = setTimeout(() => { retries = 0; }, 10000); - const exited = await dev_process.exited; - if (exited) { - return await start(); - } } diff --git a/dist/functions/bundler/all-pages-esbuild-context-bundler.d.ts b/dist/functions/bundler/all-pages-esbuild-context-bundler.d.ts index 25597b5..749927e 100644 --- a/dist/functions/bundler/all-pages-esbuild-context-bundler.d.ts +++ b/dist/functions/bundler/all-pages-esbuild-context-bundler.d.ts @@ -3,6 +3,7 @@ type Params = { post_build_fn?: (params: { artifacts: BundlerCTXMap[]; }) => Promise | void; + build_only?: boolean; }; export default function allPagesESBuildContextBundler(params?: Params): Promise; export {}; diff --git a/dist/functions/bundler/all-pages-esbuild-context-bundler.js b/dist/functions/bundler/all-pages-esbuild-context-bundler.js index 8528e4b..8b1f9d7 100644 --- a/dist/functions/bundler/all-pages-esbuild-context-bundler.js +++ b/dist/functions/bundler/all-pages-esbuild-context-bundler.js @@ -50,6 +50,7 @@ export default async function allPagesESBuildContextBundler(params) { esbuildCTXArtifactTracker({ entryToPage, post_build_fn: params?.post_build_fn, + build_only: params?.build_only, }), ], jsx: "automatic", diff --git a/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.d.ts b/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.d.ts index 7df1291..8f2876d 100644 --- a/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.d.ts +++ b/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.d.ts @@ -7,6 +7,7 @@ type Params = { post_build_fn?: (params: { artifacts: any[]; }) => Promise | void; + build_only?: boolean; }; -export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, }: Params): Plugin; +export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, build_only, }: Params): Plugin; export {}; diff --git a/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js b/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js index 205640d..6d43972 100644 --- a/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js +++ b/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js @@ -12,7 +12,7 @@ import cleanupLogsDirs from "../../cleanup-logs-dir"; const { BUNX_BUNDLER_ERROR_EXIT_FILE, BUNX_ERROR_LOGS_DIR } = grabDirNames(); let build_start = 0; const MAX_BUILD_STARTS = 2; -export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, }) { +export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, build_only, }) { const artifactTracker = { name: "artifact-tracker", setup(build) { @@ -25,7 +25,7 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, await buildOnstartErrorHandler(); } }); - build.onEnd((result) => { + build.onEnd(async (result) => { if (result.errors.length > 0) { global.RECOMPILING = false; global.IS_SERVER_COMPONENT = false; @@ -65,12 +65,20 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, global.MAIN_CTX_BUILD_STARTS = 0; global.BUNDLER_CTX_DISPOSED = false; const does_error_file_exist = existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE); - if (does_error_file_exist) { + if (build_only) { + try { + await pagesSSRBundler(); + } + catch (error) { + log.error(`SSR Bundler Error: ${error}`); + } + } + else if (does_error_file_exist) { mkdirSync(BUNX_ERROR_LOGS_DIR, { recursive: true }); cpSync(BUNX_BUNDLER_ERROR_EXIT_FILE, path.join(BUNX_ERROR_LOGS_DIR, `${Date.now()}.log`)); rmSync(BUNX_BUNDLER_ERROR_EXIT_FILE, { force: true }); cleanupLogsDirs(); - fullRebuild(); + await fullRebuild(); } else { try { diff --git a/dist/functions/bunext-init.d.ts b/dist/functions/bunext-init.d.ts index 88ad2ca..c90c78b 100644 --- a/dist/functions/bunext-init.d.ts +++ b/dist/functions/bunext-init.d.ts @@ -47,4 +47,8 @@ declare global { var CONSTANTS: ReturnType; var MAIN_CTX_BUILD_STARTS: number; } -export default function bunextInit(): Promise; +type Params = { + build_only?: boolean; +}; +export default function bunextInit(params?: Params): Promise; +export {}; diff --git a/dist/functions/bunext-init.js b/dist/functions/bunext-init.js index e10183a..0d595f5 100644 --- a/dist/functions/bunext-init.js +++ b/dist/functions/bunext-init.js @@ -10,7 +10,7 @@ import grabConstants from "../utils/grab-constants"; import watcherEsbuildCTX from "./server/watcher-esbuild-ctx"; const dirNames = grabDirNames(); const { PAGES_DIR } = dirNames; -export default async function bunextInit() { +export default async function bunextInit(params) { global.HMR_CONTROLLERS = []; global.BUNDLER_CTX_MAP = {}; global.SSR_BUNDLER_CTX_MAP = {}; @@ -33,7 +33,11 @@ export default async function bunextInit() { }); global.ROUTER = router; const is_dev = isDevelopment(); - if (is_dev) { + if (params?.build_only) { + log.build(`Building Modules ...`); + await allPagesESBuildContextBundler(); + } + else if (is_dev) { log.build(`Building Modules ...`); await allPagesESBuildContextBundler({ post_build_fn: () => { diff --git a/dist/functions/server/full-rebuild.js b/dist/functions/server/full-rebuild.js index 87d3552..f08b5eb 100644 --- a/dist/functions/server/full-rebuild.js +++ b/dist/functions/server/full-rebuild.js @@ -18,7 +18,7 @@ export default async function fullRebuild(params) { } catch (error) { } // await pagesSSRBundler(); - allPagesESBuildContextBundler({ + await allPagesESBuildContextBundler({ post_build_fn: () => { serverPostBuildFn(); }, diff --git a/dist/functions/server/watcher-esbuild-ctx.js b/dist/functions/server/watcher-esbuild-ctx.js index 5ca70aa..f84460a 100644 --- a/dist/functions/server/watcher-esbuild-ctx.js +++ b/dist/functions/server/watcher-esbuild-ctx.js @@ -22,6 +22,7 @@ export default async function watcherEsbuildCTX() { } if (global.BUNDLER_CTX_DISPOSED) { await fullRebuild({ msg: `Restarting Bundler ...` }); + return; } if (global.SSR_BUNDLER_CTX_DISPOSED) { pagesSSRBundler(); diff --git a/package.json b/package.json index 382522b..7e4c638 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bunext", - "version": "1.0.94", + "version": "1.0.95", "main": "dist/index.js", "module": "index.ts", "dependencies": { diff --git a/src/commands/build/index.ts b/src/commands/build/index.ts index f1b43d0..4982f53 100644 --- a/src/commands/build/index.ts +++ b/src/commands/build/index.ts @@ -1,9 +1,8 @@ import { Command } from "commander"; import { log } from "../../utils/log"; -import init from "../../functions/init"; import grabDirNames from "../../utils/grab-dir-names"; import { rmSync } from "fs"; -import allPagesESBuildContextBundler from "../../functions/bundler/all-pages-esbuild-context-bundler"; +import bunextInit from "../../functions/bunext-init"; const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames(); @@ -18,16 +17,9 @@ export default function () { global.SKIPPED_BROWSER_MODULES = new Set(); - // await rewritePagesModule(); - await init(); + await bunextInit({ build_only: true }); - log.banner(); - log.build("Building Project ..."); - - // await allPagesBunBundler(); - - // await allPagesBundler(); - await allPagesESBuildContextBundler(); + log.success("Modules Built Successfully!"); process.exit(); }); diff --git a/src/commands/dev/index.ts b/src/commands/dev/index.ts index ac1d2e3..3cc2b4b 100644 --- a/src/commands/dev/index.ts +++ b/src/commands/dev/index.ts @@ -44,16 +44,29 @@ async function dev() { }, }; - let dev_process = Bun.spawn(spawn_options); + let dev_process; + try { + dev_process = Bun.spawn(spawn_options); + } catch (error) { + console.error(`Failed to start dev process:`, error); + retries++; + timeout = setTimeout(() => { + retries = 0; + }, 10000); + return await dev(); + } - retries++; + const exited = await dev_process.exited; + + if (exited) { + retries++; + timeout = setTimeout(() => { + retries = 0; + }, 10000); + return await dev(); + } timeout = setTimeout(() => { retries = 0; }, 10000); - - const exited = await dev_process.exited; - if (exited) { - return await dev(); - } } diff --git a/src/commands/start/index.ts b/src/commands/start/index.ts index 454e01a..221ed1a 100644 --- a/src/commands/start/index.ts +++ b/src/commands/start/index.ts @@ -45,17 +45,29 @@ async function start() { }, }; - let dev_process = Bun.spawn(spawn_options); - - retries++; - - timeout = setTimeout(() => { - retries = 0; - }, 10000); + let dev_process; + try { + dev_process = Bun.spawn(spawn_options); + } catch (error) { + console.error(`Failed to start production process:`, error); + retries++; + timeout = setTimeout(() => { + retries = 0; + }, 10000); + return await start(); + } const exited = await dev_process.exited; if (exited) { + retries++; + timeout = setTimeout(() => { + retries = 0; + }, 10000); return await start(); } + + timeout = setTimeout(() => { + retries = 0; + }, 10000); } diff --git a/src/functions/bundler/all-pages-esbuild-context-bundler.ts b/src/functions/bundler/all-pages-esbuild-context-bundler.ts index 6aa0e14..e7970f6 100644 --- a/src/functions/bundler/all-pages-esbuild-context-bundler.ts +++ b/src/functions/bundler/all-pages-esbuild-context-bundler.ts @@ -20,6 +20,7 @@ type Params = { post_build_fn?: (params: { artifacts: BundlerCTXMap[]; }) => Promise | void; + build_only?: boolean; }; export default async function allPagesESBuildContextBundler(params?: Params) { @@ -82,6 +83,7 @@ export default async function allPagesESBuildContextBundler(params?: Params) { esbuildCTXArtifactTracker({ entryToPage, post_build_fn: params?.post_build_fn, + build_only: params?.build_only, }), ], jsx: "automatic", diff --git a/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts b/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts index 111113e..aec8b6c 100644 --- a/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts +++ b/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts @@ -24,11 +24,13 @@ type Params = { } >; post_build_fn?: (params: { artifacts: any[] }) => Promise | void; + build_only?: boolean; }; export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, + build_only, }: Params) { const artifactTracker: Plugin = { name: "artifact-tracker", @@ -49,7 +51,7 @@ export default function esbuildCTXArtifactTracker({ } }); - build.onEnd((result) => { + build.onEnd(async (result) => { if (result.errors.length > 0) { global.RECOMPILING = false; global.IS_SERVER_COMPONENT = false; @@ -112,7 +114,13 @@ export default function esbuildCTXArtifactTracker({ BUNX_BUNDLER_ERROR_EXIT_FILE, ); - if (does_error_file_exist) { + if (build_only) { + try { + await pagesSSRBundler(); + } catch (error) { + log.error(`SSR Bundler Error: ${error}`); + } + } else if (does_error_file_exist) { mkdirSync(BUNX_ERROR_LOGS_DIR, { recursive: true }); cpSync( BUNX_BUNDLER_ERROR_EXIT_FILE, @@ -120,7 +128,7 @@ export default function esbuildCTXArtifactTracker({ ); rmSync(BUNX_BUNDLER_ERROR_EXIT_FILE, { force: true }); cleanupLogsDirs(); - fullRebuild(); + await fullRebuild(); } else { try { pagesSSRBundler(); diff --git a/src/functions/bunext-init.ts b/src/functions/bunext-init.ts index 6e0f768..5996363 100644 --- a/src/functions/bunext-init.ts +++ b/src/functions/bunext-init.ts @@ -58,7 +58,11 @@ declare global { const dirNames = grabDirNames(); const { PAGES_DIR } = dirNames; -export default async function bunextInit() { +type Params = { + build_only?: boolean; +}; + +export default async function bunextInit(params?: Params) { global.HMR_CONTROLLERS = []; global.BUNDLER_CTX_MAP = {}; global.SSR_BUNDLER_CTX_MAP = {}; @@ -88,7 +92,10 @@ export default async function bunextInit() { const is_dev = isDevelopment(); - if (is_dev) { + if (params?.build_only) { + log.build(`Building Modules ...`); + await allPagesESBuildContextBundler(); + } else if (is_dev) { log.build(`Building Modules ...`); await allPagesESBuildContextBundler({ post_build_fn: () => { diff --git a/src/functions/server/full-rebuild.ts b/src/functions/server/full-rebuild.ts index ad946e3..b2d1cef 100644 --- a/src/functions/server/full-rebuild.ts +++ b/src/functions/server/full-rebuild.ts @@ -25,7 +25,7 @@ export default async function fullRebuild(params?: { msg?: string }) { // await pagesSSRBundler(); - allPagesESBuildContextBundler({ + await allPagesESBuildContextBundler({ post_build_fn: () => { serverPostBuildFn(); }, diff --git a/src/functions/server/watcher-esbuild-ctx.ts b/src/functions/server/watcher-esbuild-ctx.ts index 947209c..40cb14a 100644 --- a/src/functions/server/watcher-esbuild-ctx.ts +++ b/src/functions/server/watcher-esbuild-ctx.ts @@ -29,6 +29,7 @@ export default async function watcherEsbuildCTX() { if (global.BUNDLER_CTX_DISPOSED) { await fullRebuild({ msg: `Restarting Bundler ...` }); + return; } if (global.SSR_BUNDLER_CTX_DISPOSED) {