Refactor artifacts to use relative directory for build step and retrieval

This commit is contained in:
2026-09-23 05:08:54 +01:00
parent 2803dfa0be
commit 2335c60cc3
26 changed files with 117 additions and 204 deletions
+3 -1
View File
@@ -1,3 +1,5 @@
import grabRelativePath from "../../utils/grab-relative-path";
type Params = {
req: Request;
};
@@ -29,7 +31,7 @@ export default async function ({ req }: Params): Promise<Response> {
const match = global.BUNEXT_ROUTER.match(referer_url.pathname);
const target_map = match?.filePath
? global.BUNEXT_BUNDLER_CTX_MAP?.[match.filePath]
? global.BUNEXT_BUNDLER_CTX_MAP?.[grabRelativePath(match.filePath)]
: undefined;
let controller: ReadableStreamDefaultController<string>;
+8 -34
View File
@@ -10,6 +10,7 @@ import isDevelopment from "../../utils/is-development";
import _ from "lodash";
import path from "path";
import grabDirNames from "../../utils/grab-dir-names";
import grabRelativePath from "../../utils/grab-relative-path";
const { ROOT_DIR } = grabDirNames();
@@ -52,10 +53,15 @@ export default async function ({ req }: Params): Promise<Response> {
let module: any;
const now = Date.now();
if (is_dev && global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[match.filePath]?.path) {
const relative_match_path = grabRelativePath(match.filePath);
if (
is_dev &&
global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[relative_match_path]?.path
) {
const target_import = path.join(
ROOT_DIR,
global.BUNEXT_SSR_BUNDLER_CTX_MAP[match.filePath].path,
global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_match_path].path,
);
module = await import(`${target_import}?t=${now}`);
@@ -148,35 +154,3 @@ export default async function ({ req }: Params): Promise<Response> {
return Response.json({ err: `Route handler error` });
}
// const relative_path = match.filePath.replace(API_DIR, "");
// const relative_module_js_file = relative_path.replace(/\.tsx?$/, ".js");
// const bun_module_file = path.join(
// BUNX_CWD_MODULE_CACHE_DIR,
// "api",
// relative_module_js_file,
// );
// if (existsSync(bun_module_file)) {
// module = await import(`${bun_module_file}?t=${now}`);
// } else {
// const import_path = is_dev
// ? `${match.filePath}?t=${now}`
// : match.filePath;
// module = await import(import_path);
// }
// if (is_dev) {
// const tmp_path = `${match.filePath}.${now}${AppData["BunextTmpFileExt"]}`;
// cpSync(match.filePath, tmp_path);
// module = await import(`${tmp_path}?t=${now}`);
// try {
// unlinkSync(tmp_path);
// } catch (error) {}
// } else {
// // const import_path = is_dev ? `${match.filePath}?t=${now}` : match.filePath;
// module = await import(match.filePath);
// }
// const import_path = is_dev ? `${match.filePath}?t=${now}` : match.filePath;
// module = await import(import_path);
@@ -6,6 +6,7 @@ import type { FC } from "react";
import grabRootFilePath from "./grab-root-file-path";
import path from "path";
import grabDirNames from "../../../utils/grab-dir-names";
import grabRelativePath from "../../../utils/grab-relative-path";
const { ROOT_DIR } = grabDirNames();
@@ -19,10 +20,12 @@ export default async function grabPageBundledReactComponent({
return_tsx_only,
}: Params): Promise<GrabPageReactBundledComponentRes | undefined> {
try {
if (global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[file_path]) {
const relative_path = grabRelativePath(file_path);
if (global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[relative_path]) {
const abs = path.join(
ROOT_DIR,
global.BUNEXT_SSR_BUNDLER_CTX_MAP[file_path].path,
global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_path].path,
);
// Loader.registry.delete(abs);
const mod = await import(`${abs}?t=${Date.now()}`);
@@ -10,6 +10,7 @@ import grabPageServerRes from "./grab-page-server-res";
import grabPageServerPath from "./grab-page-server-path";
import path from "path";
import grabDirNames from "../../../utils/grab-dir-names";
import grabRelativePath from "../../../utils/grab-relative-path";
const { ROOT_DIR } = grabDirNames();
@@ -34,8 +35,12 @@ export default async function grabPageCombinedServerRes({
const { server_file_path: root_server_file_path } = root_file_path
? grabPageServerPath({ file_path: root_file_path })
: {};
const root_server_ctx_map =
global.BUNEXT_SSR_BUNDLER_CTX_MAP[root_server_file_path || ""];
const root_server_ctx_map = root_server_file_path
? global.BUNEXT_SSR_BUNDLER_CTX_MAP[
grabRelativePath(root_server_file_path)
]
: undefined;
const final_root_server_path = root_server_ctx_map?.local_path
? path.join(ROOT_DIR, root_server_ctx_map.path)
: root_server_file_path;
@@ -63,8 +68,9 @@ export default async function grabPageCombinedServerRes({
}
const { server_file_path } = grabPageServerPath({ file_path });
const page_server_ctx =
global.BUNEXT_SSR_BUNDLER_CTX_MAP[server_file_path || ""];
const page_server_ctx = server_file_path
? global.BUNEXT_SSR_BUNDLER_CTX_MAP[grabRelativePath(server_file_path)]
: undefined;
const final_page_server_path = page_server_ctx?.local_path
? path.join(ROOT_DIR, page_server_ctx.path)
: server_file_path;
@@ -10,9 +10,9 @@ import serverPostBuildFn from "../server-post-build-fn";
import isDevelopment from "../../../utils/is-development";
import { existsSync } from "fs";
import grabDirNames from "../../../utils/grab-dir-names";
import watcherEsbuildCTX from "../watcher-esbuild-ctx";
import grabRelativePath from "../../../utils/grab-relative-path";
const { BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
const { BUNX_BUNDLER_ERROR_EXIT_FILE, ROOT_DIR } = grabDirNames();
class NotFoundError extends Error {
status = 404;
@@ -90,7 +90,9 @@ export default async function grabPageComponent(
throw new Error(errMsg);
}
let bundledMap = global.BUNEXT_BUNDLER_CTX_MAP[file_path];
const relative_file_path = grabRelativePath(file_path);
let bundledMap = global.BUNEXT_BUNDLER_CTX_MAP[relative_file_path];
if (!bundledMap?.path) {
if (does_error_file_exist) {
@@ -104,10 +106,10 @@ export default async function grabPageComponent(
while (retries < MAX_RETRIES) {
await fullRebuild({
msg: `Retrying Bundle map for file \`${file_path}\``,
msg: `Retrying Bundle map for file \`${relative_file_path}\``,
});
await Bun.sleep(1000);
bundledMap = global.BUNEXT_BUNDLER_CTX_MAP[file_path];
bundledMap = global.BUNEXT_BUNDLER_CTX_MAP[relative_file_path];
if (bundledMap?.path) break;
}
@@ -119,7 +121,7 @@ export default async function grabPageComponent(
}
if (req && !is_hydration) {
global.BUNEXT_BUNDLER_CTX_MAP[file_path].req_url = req.url;
global.BUNEXT_BUNDLER_CTX_MAP[relative_file_path].req_url = req.url;
}
if (debug) {
@@ -9,6 +9,7 @@ import grabPageModules from "./grab-page-modules";
import _ from "lodash";
import fullRebuild from "../full-rebuild";
import isDevelopment from "../../../utils/is-development";
import grabRelativePath from "../../../utils/grab-relative-path";
type Params = {
error?: any;
@@ -62,7 +63,8 @@ export default async function grabPageErrorComponent({
const file_path = match.filePath;
const bundledMap = global.BUNEXT_BUNDLER_CTX_MAP?.[file_path];
const bundledMap =
global.BUNEXT_BUNDLER_CTX_MAP?.[grabRelativePath(file_path)];
const page_component = await grabPageModules({
file_path: file_path,