Add retries to page component function error. Reload for all 404 pages
This commit is contained in:
parent
5a0972beb8
commit
af8c207ac1
@ -9,6 +9,7 @@ import virtualFilesPlugin from "./plugins/virtual-files-plugin";
|
|||||||
import esbuildCTXArtifactTracker from "./plugins/esbuild-ctx-artifact-tracker";
|
import esbuildCTXArtifactTracker from "./plugins/esbuild-ctx-artifact-tracker";
|
||||||
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames();
|
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames();
|
||||||
export default async function allPagesESBuildContextBundler(params) {
|
export default async function allPagesESBuildContextBundler(params) {
|
||||||
|
try {
|
||||||
const pages = grabAllPages({ exclude_api: true });
|
const pages = grabAllPages({ exclude_api: true });
|
||||||
global.PAGE_FILES = pages;
|
global.PAGE_FILES = pages;
|
||||||
const dev = isDevelopment();
|
const dev = isDevelopment();
|
||||||
@ -60,6 +61,10 @@ export default async function allPagesESBuildContextBundler(params) {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
await global.BUNDLER_CTX.rebuild();
|
await global.BUNDLER_CTX.rebuild();
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.log(`ESBUILD Error =>`, error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
function forceExternalReact() {
|
function forceExternalReact() {
|
||||||
return {
|
return {
|
||||||
|
|||||||
2
dist/functions/bunext-init.d.ts
vendored
2
dist/functions/bunext-init.d.ts
vendored
@ -40,5 +40,7 @@ declare global {
|
|||||||
css: string;
|
css: string;
|
||||||
}>;
|
}>;
|
||||||
var BUNDLER_CTX_DISPOSED: boolean | undefined;
|
var BUNDLER_CTX_DISPOSED: boolean | undefined;
|
||||||
|
var REBUILD_RETRIES: number;
|
||||||
|
var IS_404_PAGE: boolean;
|
||||||
}
|
}
|
||||||
export default function bunextInit(): Promise<void>;
|
export default function bunextInit(): Promise<void>;
|
||||||
|
|||||||
1
dist/functions/bunext-init.js
vendored
1
dist/functions/bunext-init.js
vendored
@ -15,6 +15,7 @@ export default async function bunextInit() {
|
|||||||
global.BUNDLER_CTX_MAP = {};
|
global.BUNDLER_CTX_MAP = {};
|
||||||
global.SSR_BUNDLER_CTX_MAP = {};
|
global.SSR_BUNDLER_CTX_MAP = {};
|
||||||
global.BUNDLER_REBUILDS = 0;
|
global.BUNDLER_REBUILDS = 0;
|
||||||
|
global.REBUILD_RETRIES = 0;
|
||||||
global.PAGE_FILES = [];
|
global.PAGE_FILES = [];
|
||||||
global.SKIPPED_BROWSER_MODULES = new Set();
|
global.SKIPPED_BROWSER_MODULES = new Set();
|
||||||
global.DIR_NAMES = dirNames;
|
global.DIR_NAMES = dirNames;
|
||||||
|
|||||||
3
dist/functions/server/full-rebuild.d.ts
vendored
Normal file
3
dist/functions/server/full-rebuild.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default function fullRebuild(params?: {
|
||||||
|
msg?: string;
|
||||||
|
}): Promise<void>;
|
||||||
30
dist/functions/server/full-rebuild.js
vendored
Normal file
30
dist/functions/server/full-rebuild.js
vendored
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,6 +9,10 @@ export default async function serverPostBuildFn() {
|
|||||||
if (!controller?.target_map?.local_path) {
|
if (!controller?.target_map?.local_path) {
|
||||||
continue;
|
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 target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
||||||
const mock_req = new Request(controller.page_url);
|
const mock_req = new Request(controller.page_url);
|
||||||
const { serverRes } = global.IS_SERVER_COMPONENT
|
const { serverRes } = global.IS_SERVER_COMPONENT
|
||||||
|
|||||||
36
dist/functions/server/watcher-esbuild-ctx.js
vendored
36
dist/functions/server/watcher-esbuild-ctx.js
vendored
@ -4,6 +4,7 @@ import grabDirNames from "../../utils/grab-dir-names";
|
|||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
|
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
|
||||||
import serverPostBuildFn from "./server-post-build-fn";
|
import serverPostBuildFn from "./server-post-build-fn";
|
||||||
|
import fullRebuild from "./full-rebuild";
|
||||||
const { ROOT_DIR } = grabDirNames();
|
const { ROOT_DIR } = grabDirNames();
|
||||||
export default async function watcherEsbuildCTX() {
|
export default async function watcherEsbuildCTX() {
|
||||||
const pages_src_watcher = watch(ROOT_DIR, {
|
const pages_src_watcher = watch(ROOT_DIR, {
|
||||||
@ -38,8 +39,10 @@ export default async function watcherEsbuildCTX() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
||||||
|
const rebuild_skip_paths = /\/pages\/api\//;
|
||||||
if (event !== "rename") {
|
if (event !== "rename") {
|
||||||
if (filename.match(target_files_match)) {
|
if (filename.match(target_files_match) &&
|
||||||
|
!filename.match(rebuild_skip_paths)) {
|
||||||
if (global.RECOMPILING)
|
if (global.RECOMPILING)
|
||||||
return;
|
return;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
@ -47,8 +50,13 @@ export default async function watcherEsbuildCTX() {
|
|||||||
global.IS_SERVER_COMPONENT = true;
|
global.IS_SERVER_COMPONENT = true;
|
||||||
}
|
}
|
||||||
if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) {
|
if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) {
|
||||||
|
try {
|
||||||
await global.BUNDLER_CTX.rebuild();
|
await global.BUNDLER_CTX.rebuild();
|
||||||
}
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.log(`ESBUILD Rebuild Error =>`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
await fullRebuild({ msg: `Restarting Bundler ...` });
|
await fullRebuild({ msg: `Restarting Bundler ...` });
|
||||||
global.BUNDLER_CTX_DISPOSED = false;
|
global.BUNDLER_CTX_DISPOSED = false;
|
||||||
@ -89,32 +97,6 @@ export default async function watcherEsbuildCTX() {
|
|||||||
});
|
});
|
||||||
global.PAGES_SRC_WATCHER = pages_src_watcher;
|
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() {
|
function reloadWatcher() {
|
||||||
if (global.PAGES_SRC_WATCHER) {
|
if (global.PAGES_SRC_WATCHER) {
|
||||||
global.PAGES_SRC_WATCHER.close();
|
global.PAGES_SRC_WATCHER.close();
|
||||||
|
|||||||
@ -3,8 +3,9 @@ type Params = {
|
|||||||
req?: Request;
|
req?: Request;
|
||||||
file_path?: string;
|
file_path?: string;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
|
retry?: boolean;
|
||||||
return_server_res_only?: boolean;
|
return_server_res_only?: boolean;
|
||||||
skip_server_res?: 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<GrabPageComponentRes>;
|
export default function grabPageComponent(params: Params): Promise<GrabPageComponentRes>;
|
||||||
export {};
|
export {};
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import _ from "lodash";
|
|||||||
import { log } from "../../../utils/log";
|
import { log } from "../../../utils/log";
|
||||||
import grabPageModules from "./grab-page-modules";
|
import grabPageModules from "./grab-page-modules";
|
||||||
import grabPageCombinedServerRes from "./grab-page-combined-server-res";
|
import grabPageCombinedServerRes from "./grab-page-combined-server-res";
|
||||||
|
import fullRebuild from "../full-rebuild";
|
||||||
class NotFoundError extends Error {
|
class NotFoundError extends Error {
|
||||||
status = 404;
|
status = 404;
|
||||||
constructor(message) {
|
constructor(message) {
|
||||||
@ -11,7 +12,8 @@ class NotFoundError extends Error {
|
|||||||
this.name = "NotFoundError";
|
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 url = req?.url ? new URL(req.url) : undefined;
|
||||||
const router = global.ROUTER;
|
const router = global.ROUTER;
|
||||||
let routeParams = undefined;
|
let routeParams = undefined;
|
||||||
@ -65,6 +67,7 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
|
|||||||
url,
|
url,
|
||||||
skip_server_res,
|
skip_server_res,
|
||||||
});
|
});
|
||||||
|
global.IS_404_PAGE = false;
|
||||||
return {
|
return {
|
||||||
component,
|
component,
|
||||||
serverRes,
|
serverRes,
|
||||||
@ -72,13 +75,33 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
|
|||||||
module,
|
module,
|
||||||
bundledMap,
|
bundledMap,
|
||||||
root_module,
|
root_module,
|
||||||
|
success: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
const is404 = error instanceof NotFoundError ||
|
const is404 = error instanceof NotFoundError ||
|
||||||
error?.name === "NotFoundError" ||
|
error?.name === "NotFoundError" ||
|
||||||
error?.status === 404;
|
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(`Error Grabbing Page Component: ${error.message}`);
|
||||||
log.error(`Page: ${passed_file_path || url?.pathname}`);
|
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<any> | undefined;
|
|
||||||
// let module: BunextPageModule;
|
|
||||||
// if (isDevelopment()) {
|
|
||||||
// module = await grabFilePathModule({ file_path });
|
|
||||||
// } else {
|
|
||||||
// module = await import(file_path);
|
|
||||||
// }
|
|
||||||
// const Component = main_module.default as FC<any>;
|
|
||||||
// const component = RootComponent ? (
|
|
||||||
// <RootComponent {...serverRes}>
|
|
||||||
// <Component {...serverRes} />
|
|
||||||
// </RootComponent>
|
|
||||||
// ) : (
|
|
||||||
// <Component {...serverRes} />
|
|
||||||
// );
|
|
||||||
|
|||||||
1
dist/types/index.d.ts
vendored
1
dist/types/index.d.ts
vendored
@ -250,6 +250,7 @@ export type GrabPageComponentRes = {
|
|||||||
module?: BunextPageModule;
|
module?: BunextPageModule;
|
||||||
root_module?: BunextRootModule;
|
root_module?: BunextRootModule;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
|
success?: boolean;
|
||||||
};
|
};
|
||||||
export type BunextRootModule = BunextPageModule;
|
export type BunextRootModule = BunextPageModule;
|
||||||
export type GrabPageReactBundledComponentRes = {
|
export type GrabPageReactBundledComponentRes = {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@moduletrace/bunext",
|
"name": "@moduletrace/bunext",
|
||||||
"version": "1.0.69",
|
"version": "1.0.70",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "index.ts",
|
"module": "index.ts",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@ -16,6 +16,7 @@ type Params = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default async function allPagesESBuildContextBundler(params?: Params) {
|
export default async function allPagesESBuildContextBundler(params?: Params) {
|
||||||
|
try {
|
||||||
const pages = grabAllPages({ exclude_api: true });
|
const pages = grabAllPages({ exclude_api: true });
|
||||||
|
|
||||||
global.PAGE_FILES = pages;
|
global.PAGE_FILES = pages;
|
||||||
@ -83,6 +84,9 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await global.BUNDLER_CTX.rebuild();
|
await global.BUNDLER_CTX.rebuild();
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`ESBUILD Error =>`, error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function forceExternalReact(): esbuild.Plugin {
|
function forceExternalReact(): esbuild.Plugin {
|
||||||
|
|||||||
@ -46,6 +46,8 @@ declare global {
|
|||||||
var REACT_DOM_SERVER: any;
|
var REACT_DOM_SERVER: any;
|
||||||
var REACT_DOM_MODULE_CACHE: Map<string, { main: any; css: string }>;
|
var REACT_DOM_MODULE_CACHE: Map<string, { main: any; css: string }>;
|
||||||
var BUNDLER_CTX_DISPOSED: boolean | undefined;
|
var BUNDLER_CTX_DISPOSED: boolean | undefined;
|
||||||
|
var REBUILD_RETRIES: number;
|
||||||
|
var IS_404_PAGE: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const dirNames = grabDirNames();
|
const dirNames = grabDirNames();
|
||||||
@ -56,6 +58,7 @@ export default async function bunextInit() {
|
|||||||
global.BUNDLER_CTX_MAP = {};
|
global.BUNDLER_CTX_MAP = {};
|
||||||
global.SSR_BUNDLER_CTX_MAP = {};
|
global.SSR_BUNDLER_CTX_MAP = {};
|
||||||
global.BUNDLER_REBUILDS = 0;
|
global.BUNDLER_REBUILDS = 0;
|
||||||
|
global.REBUILD_RETRIES = 0;
|
||||||
global.PAGE_FILES = [];
|
global.PAGE_FILES = [];
|
||||||
global.SKIPPED_BROWSER_MODULES = new Set<string>();
|
global.SKIPPED_BROWSER_MODULES = new Set<string>();
|
||||||
global.DIR_NAMES = dirNames;
|
global.DIR_NAMES = dirNames;
|
||||||
|
|||||||
37
src/functions/server/full-rebuild.ts
Normal file
37
src/functions/server/full-rebuild.ts
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,6 +14,13 @@ export default async function serverPostBuildFn() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (global.IS_404_PAGE) {
|
||||||
|
controller.controller.enqueue(
|
||||||
|
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const target_artifact =
|
const target_artifact =
|
||||||
global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import grabDirNames from "../../utils/grab-dir-names";
|
|||||||
import { log } from "../../utils/log";
|
import { log } from "../../utils/log";
|
||||||
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
|
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
|
||||||
import serverPostBuildFn from "./server-post-build-fn";
|
import serverPostBuildFn from "./server-post-build-fn";
|
||||||
|
import fullRebuild from "./full-rebuild";
|
||||||
|
|
||||||
const { ROOT_DIR } = grabDirNames();
|
const { ROOT_DIR } = grabDirNames();
|
||||||
|
|
||||||
@ -49,9 +50,13 @@ export default async function watcherEsbuildCTX() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
||||||
|
const rebuild_skip_paths = /\/pages\/api\//;
|
||||||
|
|
||||||
if (event !== "rename") {
|
if (event !== "rename") {
|
||||||
if (filename.match(target_files_match)) {
|
if (
|
||||||
|
filename.match(target_files_match) &&
|
||||||
|
!filename.match(rebuild_skip_paths)
|
||||||
|
) {
|
||||||
if (global.RECOMPILING) return;
|
if (global.RECOMPILING) return;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
|
|
||||||
@ -60,7 +65,11 @@ export default async function watcherEsbuildCTX() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) {
|
if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) {
|
||||||
|
try {
|
||||||
await global.BUNDLER_CTX.rebuild();
|
await global.BUNDLER_CTX.rebuild();
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`ESBUILD Rebuild Error =>`, error);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
await fullRebuild({ msg: `Restarting Bundler ...` });
|
await fullRebuild({ msg: `Restarting Bundler ...` });
|
||||||
global.BUNDLER_CTX_DISPOSED = false;
|
global.BUNDLER_CTX_DISPOSED = false;
|
||||||
@ -114,39 +123,6 @@ export default async function watcherEsbuildCTX() {
|
|||||||
global.PAGES_SRC_WATCHER = pages_src_watcher;
|
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() {
|
function reloadWatcher() {
|
||||||
if (global.PAGES_SRC_WATCHER) {
|
if (global.PAGES_SRC_WATCHER) {
|
||||||
global.PAGES_SRC_WATCHER.close();
|
global.PAGES_SRC_WATCHER.close();
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import _ from "lodash";
|
|||||||
import { log } from "../../../utils/log";
|
import { log } from "../../../utils/log";
|
||||||
import grabPageModules from "./grab-page-modules";
|
import grabPageModules from "./grab-page-modules";
|
||||||
import grabPageCombinedServerRes from "./grab-page-combined-server-res";
|
import grabPageCombinedServerRes from "./grab-page-combined-server-res";
|
||||||
|
import fullRebuild from "../full-rebuild";
|
||||||
|
|
||||||
class NotFoundError extends Error {
|
class NotFoundError extends Error {
|
||||||
status = 404;
|
status = 404;
|
||||||
@ -19,17 +20,22 @@ type Params = {
|
|||||||
req?: Request;
|
req?: Request;
|
||||||
file_path?: string;
|
file_path?: string;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
|
retry?: boolean;
|
||||||
return_server_res_only?: boolean;
|
return_server_res_only?: boolean;
|
||||||
skip_server_res?: boolean;
|
skip_server_res?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function grabPageComponent({
|
export default async function grabPageComponent(
|
||||||
|
params: Params,
|
||||||
|
): Promise<GrabPageComponentRes> {
|
||||||
|
const {
|
||||||
req,
|
req,
|
||||||
file_path: passed_file_path,
|
file_path: passed_file_path,
|
||||||
debug,
|
debug,
|
||||||
return_server_res_only,
|
return_server_res_only,
|
||||||
skip_server_res,
|
skip_server_res,
|
||||||
}: Params): Promise<GrabPageComponentRes> {
|
} = params;
|
||||||
|
|
||||||
const url = req?.url ? new URL(req.url) : undefined;
|
const url = req?.url ? new URL(req.url) : undefined;
|
||||||
const router = global.ROUTER;
|
const router = global.ROUTER;
|
||||||
|
|
||||||
@ -101,6 +107,8 @@ export default async function grabPageComponent({
|
|||||||
skip_server_res,
|
skip_server_res,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
global.IS_404_PAGE = false;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
component,
|
component,
|
||||||
serverRes,
|
serverRes,
|
||||||
@ -108,6 +116,7 @@ export default async function grabPageComponent({
|
|||||||
module,
|
module,
|
||||||
bundledMap,
|
bundledMap,
|
||||||
root_module,
|
root_module,
|
||||||
|
success: true,
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
const is404 =
|
const is404 =
|
||||||
@ -115,7 +124,29 @@ export default async function grabPageComponent({
|
|||||||
error?.name === "NotFoundError" ||
|
error?.name === "NotFoundError" ||
|
||||||
error?.status === 404;
|
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(`Error Grabbing Page Component: ${error.message}`);
|
||||||
log.error(`Page: ${passed_file_path || url?.pathname}`);
|
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<any> | undefined;
|
|
||||||
|
|
||||||
// let module: BunextPageModule;
|
|
||||||
|
|
||||||
// if (isDevelopment()) {
|
|
||||||
// module = await grabFilePathModule({ file_path });
|
|
||||||
// } else {
|
|
||||||
// module = await import(file_path);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const Component = main_module.default as FC<any>;
|
|
||||||
// const component = RootComponent ? (
|
|
||||||
// <RootComponent {...serverRes}>
|
|
||||||
// <Component {...serverRes} />
|
|
||||||
// </RootComponent>
|
|
||||||
// ) : (
|
|
||||||
// <Component {...serverRes} />
|
|
||||||
// );
|
|
||||||
|
|||||||
@ -284,6 +284,7 @@ export type GrabPageComponentRes = {
|
|||||||
module?: BunextPageModule;
|
module?: BunextPageModule;
|
||||||
root_module?: BunextRootModule;
|
root_module?: BunextRootModule;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
|
success?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type BunextRootModule = BunextPageModule;
|
export type BunextRootModule = BunextPageModule;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user