diff --git a/dist/functions/bundler/all-pages-esbuild-context-bundler.js b/dist/functions/bundler/all-pages-esbuild-context-bundler.js index 119d6f8..dbeed9e 100644 --- a/dist/functions/bundler/all-pages-esbuild-context-bundler.js +++ b/dist/functions/bundler/all-pages-esbuild-context-bundler.js @@ -9,57 +9,62 @@ import virtualFilesPlugin from "./plugins/virtual-files-plugin"; import esbuildCTXArtifactTracker from "./plugins/esbuild-ctx-artifact-tracker"; const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames(); export default async function allPagesESBuildContextBundler(params) { - const pages = grabAllPages({ exclude_api: true }); - global.PAGE_FILES = pages; - const dev = isDevelopment(); - const entryToPage = new Map(); - for (const page of pages) { - const tsx = await grabClientHydrationScript({ - page_local_path: page.local_path, + try { + const pages = grabAllPages({ exclude_api: true }); + global.PAGE_FILES = pages; + const dev = isDevelopment(); + const entryToPage = new Map(); + for (const page of pages) { + const tsx = await grabClientHydrationScript({ + page_local_path: page.local_path, + }); + if (!tsx) + continue; + const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`); + // await Bun.write(entryFile, txt, { createPath: true }); + entryToPage.set(entryFile, { ...page, tsx }); + } + const entryPoints = [...entryToPage.keys()].map((e) => `hydration-virtual:${e}`); + global.BUNDLER_CTX = await esbuild.context({ + entryPoints, + outdir: HYDRATION_DST_DIR, + bundle: true, + minify: !dev, + format: "esm", + target: "es2020", + platform: "browser", + define: { + "process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"), + }, + entryNames: "[dir]/[hash]", + metafile: true, + plugins: [ + forceExternalReact(), + tailwindEsbuildPlugin, + virtualFilesPlugin({ + entryToPage, + }), + esbuildCTXArtifactTracker({ + entryToPage, + post_build_fn: params?.post_build_fn, + }), + ], + jsx: "automatic", + splitting: true, + treeShaking: true, + external: [ + "react", + "react-dom", + "react-dom/client", + "react/jsx-runtime", + "react/jsx-dev-runtime", + ], }); - if (!tsx) - continue; - const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`); - // await Bun.write(entryFile, txt, { createPath: true }); - entryToPage.set(entryFile, { ...page, tsx }); + await global.BUNDLER_CTX.rebuild(); + } + catch (error) { + console.log(`ESBUILD Error =>`, error); } - const entryPoints = [...entryToPage.keys()].map((e) => `hydration-virtual:${e}`); - global.BUNDLER_CTX = await esbuild.context({ - entryPoints, - outdir: HYDRATION_DST_DIR, - bundle: true, - minify: !dev, - format: "esm", - target: "es2020", - platform: "browser", - define: { - "process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"), - }, - entryNames: "[dir]/[hash]", - metafile: true, - plugins: [ - forceExternalReact(), - tailwindEsbuildPlugin, - virtualFilesPlugin({ - entryToPage, - }), - esbuildCTXArtifactTracker({ - entryToPage, - post_build_fn: params?.post_build_fn, - }), - ], - jsx: "automatic", - splitting: true, - treeShaking: true, - external: [ - "react", - "react-dom", - "react-dom/client", - "react/jsx-runtime", - "react/jsx-dev-runtime", - ], - }); - await global.BUNDLER_CTX.rebuild(); } function forceExternalReact() { return { diff --git a/dist/functions/bunext-init.d.ts b/dist/functions/bunext-init.d.ts index 92c6d0a..10c82f1 100644 --- a/dist/functions/bunext-init.d.ts +++ b/dist/functions/bunext-init.d.ts @@ -40,5 +40,7 @@ declare global { css: string; }>; var BUNDLER_CTX_DISPOSED: boolean | undefined; + var REBUILD_RETRIES: number; + var IS_404_PAGE: boolean; } export default function bunextInit(): Promise; diff --git a/dist/functions/bunext-init.js b/dist/functions/bunext-init.js index 5e7db8e..f6f2bcd 100644 --- a/dist/functions/bunext-init.js +++ b/dist/functions/bunext-init.js @@ -15,6 +15,7 @@ export default async function bunextInit() { global.BUNDLER_CTX_MAP = {}; global.SSR_BUNDLER_CTX_MAP = {}; global.BUNDLER_REBUILDS = 0; + global.REBUILD_RETRIES = 0; global.PAGE_FILES = []; global.SKIPPED_BROWSER_MODULES = new Set(); global.DIR_NAMES = dirNames; diff --git a/dist/functions/server/full-rebuild.d.ts b/dist/functions/server/full-rebuild.d.ts new file mode 100644 index 0000000..fd52afe --- /dev/null +++ b/dist/functions/server/full-rebuild.d.ts @@ -0,0 +1,3 @@ +export default function fullRebuild(params?: { + msg?: string; +}): Promise; diff --git a/dist/functions/server/full-rebuild.js b/dist/functions/server/full-rebuild.js new file mode 100644 index 0000000..968f32c --- /dev/null +++ b/dist/functions/server/full-rebuild.js @@ -0,0 +1,30 @@ +import { log } from "../../utils/log"; +import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler"; +import serverPostBuildFn from "./server-post-build-fn"; +import watcherEsbuildCTX from "./watcher-esbuild-ctx"; +export default async function fullRebuild(params) { + try { + const { msg } = params || {}; + global.RECOMPILING = true; + if (msg) { + log.watch(msg); + } + global.ROUTER.reload(); + await global.BUNDLER_CTX?.dispose(); + global.BUNDLER_CTX = undefined; + global.BUNDLER_CTX_MAP = {}; + await global.SSR_BUNDLER_CTX?.dispose(); + global.SSR_BUNDLER_CTX = undefined; + global.SSR_BUNDLER_CTX_MAP = {}; + allPagesESBuildContextBundler({ + post_build_fn: serverPostBuildFn, + }); + } + catch (error) { + log.error(error); + } + if (global.PAGES_SRC_WATCHER) { + global.PAGES_SRC_WATCHER.close(); + watcherEsbuildCTX(); + } +} diff --git a/dist/functions/server/server-post-build-fn.js b/dist/functions/server/server-post-build-fn.js index 811ef9f..2fed30d 100644 --- a/dist/functions/server/server-post-build-fn.js +++ b/dist/functions/server/server-post-build-fn.js @@ -9,6 +9,10 @@ export default async function serverPostBuildFn() { if (!controller?.target_map?.local_path) { continue; } + if (global.IS_404_PAGE) { + controller.controller.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`); + continue; + } const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path]; const mock_req = new Request(controller.page_url); const { serverRes } = global.IS_SERVER_COMPONENT diff --git a/dist/functions/server/watcher-esbuild-ctx.js b/dist/functions/server/watcher-esbuild-ctx.js index 8d57daf..bdb274c 100644 --- a/dist/functions/server/watcher-esbuild-ctx.js +++ b/dist/functions/server/watcher-esbuild-ctx.js @@ -4,6 +4,7 @@ import grabDirNames from "../../utils/grab-dir-names"; import { log } from "../../utils/log"; import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler"; import serverPostBuildFn from "./server-post-build-fn"; +import fullRebuild from "./full-rebuild"; const { ROOT_DIR } = grabDirNames(); export default async function watcherEsbuildCTX() { const pages_src_watcher = watch(ROOT_DIR, { @@ -38,8 +39,10 @@ export default async function watcherEsbuildCTX() { return; } const target_files_match = /\.(tsx?|jsx?|css)$/; + const rebuild_skip_paths = /\/pages\/api\//; if (event !== "rename") { - if (filename.match(target_files_match)) { + if (filename.match(target_files_match) && + !filename.match(rebuild_skip_paths)) { if (global.RECOMPILING) return; global.RECOMPILING = true; @@ -47,7 +50,12 @@ export default async function watcherEsbuildCTX() { global.IS_SERVER_COMPONENT = true; } if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) { - await global.BUNDLER_CTX.rebuild(); + try { + await global.BUNDLER_CTX.rebuild(); + } + catch (error) { + console.log(`ESBUILD Rebuild Error =>`, error); + } } else { await fullRebuild({ msg: `Restarting Bundler ...` }); @@ -89,32 +97,6 @@ export default async function watcherEsbuildCTX() { }); global.PAGES_SRC_WATCHER = pages_src_watcher; } -async function fullRebuild(params) { - try { - const { msg } = params || {}; - global.RECOMPILING = true; - if (msg) { - log.watch(msg); - } - global.ROUTER.reload(); - await global.BUNDLER_CTX?.dispose(); - global.BUNDLER_CTX = undefined; - global.BUNDLER_CTX_MAP = {}; - await global.SSR_BUNDLER_CTX?.dispose(); - global.SSR_BUNDLER_CTX = undefined; - global.SSR_BUNDLER_CTX_MAP = {}; - allPagesESBuildContextBundler({ - post_build_fn: serverPostBuildFn, - }); - } - catch (error) { - log.error(error); - } - if (global.PAGES_SRC_WATCHER) { - global.PAGES_SRC_WATCHER.close(); - watcherEsbuildCTX(); - } -} function reloadWatcher() { if (global.PAGES_SRC_WATCHER) { global.PAGES_SRC_WATCHER.close(); diff --git a/dist/functions/server/web-pages/grab-page-component.d.ts b/dist/functions/server/web-pages/grab-page-component.d.ts index fc64d6b..39c1034 100644 --- a/dist/functions/server/web-pages/grab-page-component.d.ts +++ b/dist/functions/server/web-pages/grab-page-component.d.ts @@ -3,8 +3,9 @@ type Params = { req?: Request; file_path?: string; debug?: boolean; + retry?: boolean; return_server_res_only?: boolean; skip_server_res?: boolean; }; -export default function grabPageComponent({ req, file_path: passed_file_path, debug, return_server_res_only, skip_server_res, }: Params): Promise; +export default function grabPageComponent(params: Params): Promise; export {}; diff --git a/dist/functions/server/web-pages/grab-page-component.js b/dist/functions/server/web-pages/grab-page-component.js index 6eb7fb4..e2546b1 100644 --- a/dist/functions/server/web-pages/grab-page-component.js +++ b/dist/functions/server/web-pages/grab-page-component.js @@ -4,6 +4,7 @@ import _ from "lodash"; import { log } from "../../../utils/log"; import grabPageModules from "./grab-page-modules"; import grabPageCombinedServerRes from "./grab-page-combined-server-res"; +import fullRebuild from "../full-rebuild"; class NotFoundError extends Error { status = 404; constructor(message) { @@ -11,7 +12,8 @@ class NotFoundError extends Error { this.name = "NotFoundError"; } } -export default async function grabPageComponent({ req, file_path: passed_file_path, debug, return_server_res_only, skip_server_res, }) { +export default async function grabPageComponent(params) { + const { req, file_path: passed_file_path, debug, return_server_res_only, skip_server_res, } = params; const url = req?.url ? new URL(req.url) : undefined; const router = global.ROUTER; let routeParams = undefined; @@ -65,6 +67,7 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa url, skip_server_res, }); + global.IS_404_PAGE = false; return { component, serverRes, @@ -72,13 +75,33 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa module, bundledMap, root_module, + success: true, }; } catch (error) { const is404 = error instanceof NotFoundError || error?.name === "NotFoundError" || error?.status === 404; - if (!is404) { + if (!params.retry) { + while (global.REBUILD_RETRIES < 2) { + global.REBUILD_RETRIES = global.REBUILD_RETRIES + 1; + await fullRebuild(); + await Bun.sleep(200); + const component_retried = await grabPageComponent({ + ...params, + retry: true, + }); + if (component_retried.success) { + global.REBUILD_RETRIES = 0; + return component_retried; + } + } + global.REBUILD_RETRIES = 0; + } + if (is404) { + global.IS_404_PAGE = true; + } + else { log.error(`Error Grabbing Page Component: ${error.message}`); log.error(`Page: ${passed_file_path || url?.pathname}`); } @@ -90,28 +113,3 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa }); } } -// let root_module: any; -// if (root_file) { -// if (isDevelopment()) { -// root_module = await grabFilePathModule({ -// file_path: root_file, -// }); -// } else { -// root_module = root_file ? await import(root_file) : undefined; -// } -// } -// const RootComponent = root_module?.default as FC | undefined; -// let module: BunextPageModule; -// if (isDevelopment()) { -// module = await grabFilePathModule({ file_path }); -// } else { -// module = await import(file_path); -// } -// const Component = main_module.default as FC; -// const component = RootComponent ? ( -// -// -// -// ) : ( -// -// ); diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index 6731400..1374d0e 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -250,6 +250,7 @@ export type GrabPageComponentRes = { module?: BunextPageModule; root_module?: BunextRootModule; debug?: boolean; + success?: boolean; }; export type BunextRootModule = BunextPageModule; export type GrabPageReactBundledComponentRes = { diff --git a/package.json b/package.json index ec19db9..40050c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bunext", - "version": "1.0.69", + "version": "1.0.70", "main": "dist/index.js", "module": "index.ts", "dependencies": { diff --git a/src/functions/bundler/all-pages-esbuild-context-bundler.ts b/src/functions/bundler/all-pages-esbuild-context-bundler.ts index 39803ea..52890e8 100644 --- a/src/functions/bundler/all-pages-esbuild-context-bundler.ts +++ b/src/functions/bundler/all-pages-esbuild-context-bundler.ts @@ -16,73 +16,77 @@ type Params = { }; export default async function allPagesESBuildContextBundler(params?: Params) { - const pages = grabAllPages({ exclude_api: true }); + try { + const pages = grabAllPages({ exclude_api: true }); - global.PAGE_FILES = pages; + global.PAGE_FILES = pages; - const dev = isDevelopment(); + const dev = isDevelopment(); - const entryToPage = new Map(); + const entryToPage = new Map(); - for (const page of pages) { - const tsx = await grabClientHydrationScript({ - page_local_path: page.local_path, - }); + for (const page of pages) { + const tsx = await grabClientHydrationScript({ + page_local_path: page.local_path, + }); - if (!tsx) continue; + if (!tsx) continue; - const entryFile = path.join( - BUNX_HYDRATION_SRC_DIR, - `${page.url_path}.tsx`, + const entryFile = path.join( + BUNX_HYDRATION_SRC_DIR, + `${page.url_path}.tsx`, + ); + + // await Bun.write(entryFile, txt, { createPath: true }); + entryToPage.set(entryFile, { ...page, tsx }); + } + + const entryPoints = [...entryToPage.keys()].map( + (e) => `hydration-virtual:${e}`, ); - // await Bun.write(entryFile, txt, { createPath: true }); - entryToPage.set(entryFile, { ...page, tsx }); + global.BUNDLER_CTX = await esbuild.context({ + entryPoints, + outdir: HYDRATION_DST_DIR, + bundle: true, + minify: !dev, + format: "esm", + target: "es2020", + platform: "browser", + define: { + "process.env.NODE_ENV": JSON.stringify( + dev ? "development" : "production", + ), + }, + entryNames: "[dir]/[hash]", + metafile: true, + plugins: [ + forceExternalReact(), + tailwindEsbuildPlugin, + virtualFilesPlugin({ + entryToPage, + }), + esbuildCTXArtifactTracker({ + entryToPage, + post_build_fn: params?.post_build_fn, + }), + ], + jsx: "automatic", + splitting: true, + treeShaking: true, + external: [ + "react", + "react-dom", + "react-dom/client", + "react/jsx-runtime", + "react/jsx-dev-runtime", + ], + }); + + await global.BUNDLER_CTX.rebuild(); + } catch (error) { + console.log(`ESBUILD Error =>`, error); } - - const entryPoints = [...entryToPage.keys()].map( - (e) => `hydration-virtual:${e}`, - ); - - global.BUNDLER_CTX = await esbuild.context({ - entryPoints, - outdir: HYDRATION_DST_DIR, - bundle: true, - minify: !dev, - format: "esm", - target: "es2020", - platform: "browser", - define: { - "process.env.NODE_ENV": JSON.stringify( - dev ? "development" : "production", - ), - }, - entryNames: "[dir]/[hash]", - metafile: true, - plugins: [ - forceExternalReact(), - tailwindEsbuildPlugin, - virtualFilesPlugin({ - entryToPage, - }), - esbuildCTXArtifactTracker({ - entryToPage, - post_build_fn: params?.post_build_fn, - }), - ], - jsx: "automatic", - splitting: true, - treeShaking: true, - external: [ - "react", - "react-dom", - "react-dom/client", - "react/jsx-runtime", - "react/jsx-dev-runtime", - ], - }); - - await global.BUNDLER_CTX.rebuild(); } function forceExternalReact(): esbuild.Plugin { diff --git a/src/functions/bunext-init.ts b/src/functions/bunext-init.ts index 067337e..5402efc 100644 --- a/src/functions/bunext-init.ts +++ b/src/functions/bunext-init.ts @@ -46,6 +46,8 @@ declare global { var REACT_DOM_SERVER: any; var REACT_DOM_MODULE_CACHE: Map; var BUNDLER_CTX_DISPOSED: boolean | undefined; + var REBUILD_RETRIES: number; + var IS_404_PAGE: boolean; } const dirNames = grabDirNames(); @@ -56,6 +58,7 @@ export default async function bunextInit() { global.BUNDLER_CTX_MAP = {}; global.SSR_BUNDLER_CTX_MAP = {}; global.BUNDLER_REBUILDS = 0; + global.REBUILD_RETRIES = 0; global.PAGE_FILES = []; global.SKIPPED_BROWSER_MODULES = new Set(); global.DIR_NAMES = dirNames; diff --git a/src/functions/server/full-rebuild.ts b/src/functions/server/full-rebuild.ts new file mode 100644 index 0000000..0830205 --- /dev/null +++ b/src/functions/server/full-rebuild.ts @@ -0,0 +1,37 @@ +import { log } from "../../utils/log"; +import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler"; +import serverPostBuildFn from "./server-post-build-fn"; +import watcherEsbuildCTX from "./watcher-esbuild-ctx"; + +export default async function fullRebuild(params?: { msg?: string }) { + try { + const { msg } = params || {}; + + global.RECOMPILING = true; + + if (msg) { + log.watch(msg); + } + + global.ROUTER.reload(); + + await global.BUNDLER_CTX?.dispose(); + global.BUNDLER_CTX = undefined; + global.BUNDLER_CTX_MAP = {}; + + await global.SSR_BUNDLER_CTX?.dispose(); + global.SSR_BUNDLER_CTX = undefined; + global.SSR_BUNDLER_CTX_MAP = {}; + + allPagesESBuildContextBundler({ + post_build_fn: serverPostBuildFn, + }); + } catch (error: any) { + log.error(error); + } + + if (global.PAGES_SRC_WATCHER) { + global.PAGES_SRC_WATCHER.close(); + watcherEsbuildCTX(); + } +} diff --git a/src/functions/server/server-post-build-fn.ts b/src/functions/server/server-post-build-fn.ts index d655235..6375384 100644 --- a/src/functions/server/server-post-build-fn.ts +++ b/src/functions/server/server-post-build-fn.ts @@ -14,6 +14,13 @@ export default async function serverPostBuildFn() { continue; } + if (global.IS_404_PAGE) { + controller.controller.enqueue( + `event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`, + ); + continue; + } + const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path]; diff --git a/src/functions/server/watcher-esbuild-ctx.ts b/src/functions/server/watcher-esbuild-ctx.ts index a0d01af..7bb3f6f 100644 --- a/src/functions/server/watcher-esbuild-ctx.ts +++ b/src/functions/server/watcher-esbuild-ctx.ts @@ -4,6 +4,7 @@ import grabDirNames from "../../utils/grab-dir-names"; import { log } from "../../utils/log"; import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler"; import serverPostBuildFn from "./server-post-build-fn"; +import fullRebuild from "./full-rebuild"; const { ROOT_DIR } = grabDirNames(); @@ -49,9 +50,13 @@ export default async function watcherEsbuildCTX() { } const target_files_match = /\.(tsx?|jsx?|css)$/; + const rebuild_skip_paths = /\/pages\/api\//; if (event !== "rename") { - if (filename.match(target_files_match)) { + if ( + filename.match(target_files_match) && + !filename.match(rebuild_skip_paths) + ) { if (global.RECOMPILING) return; global.RECOMPILING = true; @@ -60,7 +65,11 @@ export default async function watcherEsbuildCTX() { } if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) { - await global.BUNDLER_CTX.rebuild(); + try { + await global.BUNDLER_CTX.rebuild(); + } catch (error) { + console.log(`ESBUILD Rebuild Error =>`, error); + } } else { await fullRebuild({ msg: `Restarting Bundler ...` }); global.BUNDLER_CTX_DISPOSED = false; @@ -114,39 +123,6 @@ export default async function watcherEsbuildCTX() { global.PAGES_SRC_WATCHER = pages_src_watcher; } -async function fullRebuild(params?: { msg?: string }) { - try { - const { msg } = params || {}; - - global.RECOMPILING = true; - - if (msg) { - log.watch(msg); - } - - global.ROUTER.reload(); - - await global.BUNDLER_CTX?.dispose(); - global.BUNDLER_CTX = undefined; - global.BUNDLER_CTX_MAP = {}; - - await global.SSR_BUNDLER_CTX?.dispose(); - global.SSR_BUNDLER_CTX = undefined; - global.SSR_BUNDLER_CTX_MAP = {}; - - allPagesESBuildContextBundler({ - post_build_fn: serverPostBuildFn, - }); - } catch (error: any) { - log.error(error); - } - - if (global.PAGES_SRC_WATCHER) { - global.PAGES_SRC_WATCHER.close(); - watcherEsbuildCTX(); - } -} - function reloadWatcher() { if (global.PAGES_SRC_WATCHER) { global.PAGES_SRC_WATCHER.close(); diff --git a/src/functions/server/web-pages/grab-page-component.tsx b/src/functions/server/web-pages/grab-page-component.tsx index ef52948..f956348 100644 --- a/src/functions/server/web-pages/grab-page-component.tsx +++ b/src/functions/server/web-pages/grab-page-component.tsx @@ -5,6 +5,7 @@ import _ from "lodash"; import { log } from "../../../utils/log"; import grabPageModules from "./grab-page-modules"; import grabPageCombinedServerRes from "./grab-page-combined-server-res"; +import fullRebuild from "../full-rebuild"; class NotFoundError extends Error { status = 404; @@ -19,17 +20,22 @@ type Params = { req?: Request; file_path?: string; debug?: boolean; + retry?: boolean; return_server_res_only?: boolean; skip_server_res?: boolean; }; -export default async function grabPageComponent({ - req, - file_path: passed_file_path, - debug, - return_server_res_only, - skip_server_res, -}: Params): Promise { +export default async function grabPageComponent( + params: Params, +): Promise { + const { + req, + file_path: passed_file_path, + debug, + return_server_res_only, + skip_server_res, + } = params; + const url = req?.url ? new URL(req.url) : undefined; const router = global.ROUTER; @@ -101,6 +107,8 @@ export default async function grabPageComponent({ skip_server_res, }); + global.IS_404_PAGE = false; + return { component, serverRes, @@ -108,6 +116,7 @@ export default async function grabPageComponent({ module, bundledMap, root_module, + success: true, }; } catch (error: any) { const is404 = @@ -115,7 +124,29 @@ export default async function grabPageComponent({ error?.name === "NotFoundError" || error?.status === 404; - if (!is404) { + if (!params.retry) { + while (global.REBUILD_RETRIES < 2) { + global.REBUILD_RETRIES = global.REBUILD_RETRIES + 1; + + await fullRebuild(); + await Bun.sleep(200); + const component_retried = await grabPageComponent({ + ...params, + retry: true, + }); + + if (component_retried.success) { + global.REBUILD_RETRIES = 0; + return component_retried; + } + } + + global.REBUILD_RETRIES = 0; + } + + if (is404) { + global.IS_404_PAGE = true; + } else { log.error(`Error Grabbing Page Component: ${error.message}`); log.error(`Page: ${passed_file_path || url?.pathname}`); } @@ -128,34 +159,3 @@ export default async function grabPageComponent({ }); } } - -// let root_module: any; - -// if (root_file) { -// if (isDevelopment()) { -// root_module = await grabFilePathModule({ -// file_path: root_file, -// }); -// } else { -// root_module = root_file ? await import(root_file) : undefined; -// } -// } - -// const RootComponent = root_module?.default as FC | undefined; - -// let module: BunextPageModule; - -// if (isDevelopment()) { -// module = await grabFilePathModule({ file_path }); -// } else { -// module = await import(file_path); -// } - -// const Component = main_module.default as FC; -// const component = RootComponent ? ( -// -// -// -// ) : ( -// -// ); diff --git a/src/types/index.ts b/src/types/index.ts index 02b986a..0d316ce 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -284,6 +284,7 @@ export type GrabPageComponentRes = { module?: BunextPageModule; root_module?: BunextRootModule; debug?: boolean; + success?: boolean; }; export type BunextRootModule = BunextPageModule;