Refactor bundler function
This commit is contained in:
@@ -14,7 +14,7 @@ export default async function serverPostBuildFn() {
|
||||
for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
|
||||
const controller = global.HMR_CONTROLLERS[i];
|
||||
|
||||
if (!controller.target_map?.local_path) {
|
||||
if (!controller?.target_map?.local_path) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,10 @@ export default async function watcherEsbuildCTX() {
|
||||
async (event, filename) => {
|
||||
if (!filename) return;
|
||||
|
||||
if (filename.match(/^\.\w+/)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const full_file_path = path.join(ROOT_DIR, filename);
|
||||
|
||||
if (full_file_path.match(/\/styles$/)) {
|
||||
@@ -47,7 +51,21 @@ export default async function watcherEsbuildCTX() {
|
||||
if (filename.match(target_files_match)) {
|
||||
if (global.RECOMPILING) return;
|
||||
global.RECOMPILING = true;
|
||||
|
||||
await global.BUNDLER_CTX?.rebuild();
|
||||
|
||||
if (filename.match(/(404|500)\.tsx?/)) {
|
||||
for (
|
||||
let i = global.HMR_CONTROLLERS.length - 1;
|
||||
i >= 0;
|
||||
i--
|
||||
) {
|
||||
const controller = global.HMR_CONTROLLERS[i];
|
||||
controller?.controller?.enqueue(
|
||||
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { JSX } from "react";
|
||||
import type { GrabPageReactBundledComponentRes } from "../../../types";
|
||||
import grabPageReactComponentString from "./grab-page-react-component-string";
|
||||
import grabTsxStringModule from "./grab-tsx-string-module";
|
||||
|
||||
@@ -14,6 +14,7 @@ import { log } from "../../../utils/log";
|
||||
import grabRootFilePath from "./grab-root-file-path";
|
||||
import grabPageServerRes from "./grab-page-server-res";
|
||||
import grabPageServerPath from "./grab-page-server-path";
|
||||
import grabPageModules from "./grab-page-modules";
|
||||
|
||||
class NotFoundError extends Error {}
|
||||
|
||||
@@ -30,7 +31,6 @@ export default async function grabPageComponent({
|
||||
}: Params): Promise<GrabPageComponentRes> {
|
||||
const url = req?.url ? new URL(req.url) : undefined;
|
||||
const router = global.ROUTER;
|
||||
const now = Date.now();
|
||||
|
||||
let routeParams: BunxRouteParams | undefined = undefined;
|
||||
|
||||
@@ -78,79 +78,18 @@ export default async function grabPageComponent({
|
||||
log.info(`bundledMap:`, bundledMap);
|
||||
}
|
||||
|
||||
const { root_file_path } = grabRootFilePath();
|
||||
const root_module: BunextRootModule | undefined = root_file_path
|
||||
? await import(`${root_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
const { server_file_path: root_server_file_path } = root_file_path
|
||||
? grabPageServerPath({ file_path: root_file_path })
|
||||
: {};
|
||||
const root_server_module: BunextPageServerModule = root_server_file_path
|
||||
? await import(`${root_server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
|
||||
const root_server_fn =
|
||||
root_server_module?.default || root_server_module?.server;
|
||||
|
||||
const rootServerRes: BunextPageModuleServerReturn | undefined =
|
||||
root_server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: root_server_fn,
|
||||
url,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`rootServerRes:`, rootServerRes);
|
||||
}
|
||||
|
||||
const module: BunextPageModule = await import(`${file_path}?t=${now}`);
|
||||
const { server_file_path } = grabPageServerPath({ file_path });
|
||||
const server_module: BunextPageServerModule = server_file_path
|
||||
? await import(`${server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`module:`, module);
|
||||
}
|
||||
|
||||
const server_fn = server_module?.default || server_module?.server;
|
||||
|
||||
const serverRes: BunextPageModuleServerReturn | undefined = server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: server_fn,
|
||||
url,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`serverRes:`, serverRes);
|
||||
}
|
||||
|
||||
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
|
||||
|
||||
const { component } =
|
||||
(await grabPageBundledReactComponent({
|
||||
const { component, module, serverRes, root_module } =
|
||||
await grabPageModules({
|
||||
file_path,
|
||||
root_file_path,
|
||||
server_res: mergedServerRes,
|
||||
})) || {};
|
||||
|
||||
if (!component) {
|
||||
throw new Error(`Couldn't grab page component`);
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
log.info(`component:`, component);
|
||||
}
|
||||
debug,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
url,
|
||||
});
|
||||
|
||||
return {
|
||||
component,
|
||||
serverRes: mergedServerRes,
|
||||
serverRes,
|
||||
routeParams,
|
||||
module,
|
||||
bundledMap,
|
||||
@@ -163,6 +102,7 @@ export default async function grabPageComponent({
|
||||
error,
|
||||
routeParams,
|
||||
is404: error instanceof NotFoundError,
|
||||
url,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,17 +5,21 @@ import type {
|
||||
BunxRouteParams,
|
||||
GrabPageComponentRes,
|
||||
} from "../../../types";
|
||||
import grabPageModules from "./grab-page-modules";
|
||||
import _ from "lodash";
|
||||
|
||||
type Params = {
|
||||
error?: any;
|
||||
routeParams?: BunxRouteParams;
|
||||
is404?: boolean;
|
||||
url?: URL;
|
||||
};
|
||||
|
||||
export default async function grabPageErrorComponent({
|
||||
error,
|
||||
routeParams,
|
||||
is404,
|
||||
url,
|
||||
}: Params): Promise<GrabPageComponentRes> {
|
||||
const router = global.ROUTER;
|
||||
|
||||
@@ -27,28 +31,51 @@ export default async function grabPageErrorComponent({
|
||||
? BUNX_ROOT_404_PRESET_COMPONENT
|
||||
: BUNX_ROOT_500_PRESET_COMPONENT;
|
||||
|
||||
const default_server_res = {
|
||||
responseOptions: {
|
||||
status: is404 ? 404 : 500,
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const match = router.match(errorRoute);
|
||||
const filePath = match?.filePath || presetComponent;
|
||||
|
||||
const bundledMap = match?.filePath
|
||||
? global.BUNDLER_CTX_MAP?.[match.filePath]
|
||||
: undefined;
|
||||
if (!match?.filePath) {
|
||||
const default_module: BunextPageModule = await import(
|
||||
presetComponent
|
||||
);
|
||||
const Component = default_module.default as FC<any>;
|
||||
const default_jsx = (
|
||||
<Component>{<span>{error.message}</span>}</Component>
|
||||
);
|
||||
|
||||
const module: BunextPageModule = await import(filePath);
|
||||
const Component = module.default as FC<any>;
|
||||
const component = <Component>{<span>{error.message}</span>}</Component>;
|
||||
return {
|
||||
component: default_jsx,
|
||||
module: default_module,
|
||||
routeParams,
|
||||
serverRes: default_server_res,
|
||||
};
|
||||
}
|
||||
|
||||
const file_path = match.filePath;
|
||||
|
||||
const bundledMap = global.BUNDLER_CTX_MAP?.[file_path];
|
||||
|
||||
const { component, module, serverRes, root_module } =
|
||||
await grabPageModules({
|
||||
file_path: file_path,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
url,
|
||||
});
|
||||
|
||||
return {
|
||||
component,
|
||||
routeParams,
|
||||
module,
|
||||
bundledMap,
|
||||
serverRes: {
|
||||
responseOptions: {
|
||||
status: is404 ? 404 : 500,
|
||||
},
|
||||
} as any,
|
||||
serverRes: _.merge(serverRes, default_server_res),
|
||||
root_module,
|
||||
};
|
||||
} catch {
|
||||
const DefaultNotFound: FC = () => (
|
||||
@@ -71,12 +98,7 @@ export default async function grabPageErrorComponent({
|
||||
component: <DefaultNotFound />,
|
||||
routeParams,
|
||||
module: { default: DefaultNotFound },
|
||||
bundledMap: undefined,
|
||||
serverRes: {
|
||||
responseOptions: {
|
||||
status: is404 ? 404 : 500,
|
||||
},
|
||||
} as any,
|
||||
serverRes: default_server_res,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import type {
|
||||
BunextPageModule,
|
||||
BunextPageModuleServerReturn,
|
||||
BunextPageServerModule,
|
||||
BunextRootModule,
|
||||
BunxRouteParams,
|
||||
} from "../../../types";
|
||||
import grabPageBundledReactComponent from "./grab-page-bundled-react-component";
|
||||
import _ from "lodash";
|
||||
import { log } from "../../../utils/log";
|
||||
import grabRootFilePath from "./grab-root-file-path";
|
||||
import grabPageServerRes from "./grab-page-server-res";
|
||||
import grabPageServerPath from "./grab-page-server-path";
|
||||
import type { JSX } from "react";
|
||||
|
||||
type Params = {
|
||||
file_path: string;
|
||||
debug?: boolean;
|
||||
url?: URL;
|
||||
query?: any;
|
||||
routeParams?: BunxRouteParams;
|
||||
};
|
||||
|
||||
export default async function grabPageModules({
|
||||
file_path,
|
||||
debug,
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
}: Params) {
|
||||
const now = Date.now();
|
||||
|
||||
const { root_file_path } = grabRootFilePath();
|
||||
const root_module: BunextRootModule | undefined = root_file_path
|
||||
? await import(`${root_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
const { server_file_path: root_server_file_path } = root_file_path
|
||||
? grabPageServerPath({ file_path: root_file_path })
|
||||
: {};
|
||||
const root_server_module: BunextPageServerModule = root_server_file_path
|
||||
? await import(`${root_server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
|
||||
const root_server_fn =
|
||||
root_server_module?.default || root_server_module?.server;
|
||||
|
||||
const rootServerRes: BunextPageModuleServerReturn | undefined =
|
||||
root_server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: root_server_fn,
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`rootServerRes:`, rootServerRes);
|
||||
}
|
||||
|
||||
const module: BunextPageModule = await import(`${file_path}?t=${now}`);
|
||||
const { server_file_path } = grabPageServerPath({ file_path });
|
||||
const server_module: BunextPageServerModule = server_file_path
|
||||
? await import(`${server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`module:`, module);
|
||||
}
|
||||
|
||||
const server_fn = server_module?.default || server_module?.server;
|
||||
|
||||
const serverRes: BunextPageModuleServerReturn | undefined = server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: server_fn,
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`serverRes:`, serverRes);
|
||||
}
|
||||
|
||||
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
|
||||
|
||||
const { component } =
|
||||
(await grabPageBundledReactComponent({
|
||||
file_path,
|
||||
root_file_path,
|
||||
server_res: mergedServerRes,
|
||||
})) || {};
|
||||
|
||||
if (!component) {
|
||||
throw new Error(`Couldn't grab page component`);
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
log.info(`component:`, component);
|
||||
}
|
||||
|
||||
return {
|
||||
component,
|
||||
serverRes: mergedServerRes,
|
||||
module,
|
||||
root_module,
|
||||
};
|
||||
}
|
||||
@@ -41,7 +41,7 @@ export default async function (params?: Params) {
|
||||
script += ` try {\n`;
|
||||
script += ` document.getElementById("__bunext_error_overlay")?.remove();\n`;
|
||||
script += ` const data = JSON.parse(event.data);\n`;
|
||||
// script += ` console.log("data", data);\n`;
|
||||
script += ` console.log("data", data);\n`;
|
||||
|
||||
script += ` if (data.reload) {\n`;
|
||||
script += ` console.log(\`Root Changes Detected. Reloading Page ...\`);\n`;
|
||||
|
||||
Reference in New Issue
Block a user