From 2335c60cc3f5547389050ef7d6fa3bc6cd96bca1 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Wed, 23 Sep 2026 05:08:54 +0100 Subject: [PATCH] Refactor artifacts to use relative directory for build step and retrieval --- README.md | 15 +------ dist/commands/build/index.js | 7 ---- .../plugins/esbuild-ctx-artifact-tracker.js | 8 ++-- .../plugins/ssr-ctx-artifact-tracker.js | 9 +++- dist/functions/bundler/record-artifacts.d.ts | 7 ---- dist/functions/bundler/record-artifacts.js | 18 -------- dist/functions/server/handle-hmr.js | 3 +- dist/functions/server/handle-routes.js | 35 +++------------- .../grab-page-bundled-react-component.js | 6 ++- .../grab-page-combined-server-res.js | 9 +++- .../server/web-pages/grab-page-component.js | 13 +++--- .../web-pages/grab-page-error-component.js | 3 +- dist/utils/grab-relative-path.d.ts | 1 + dist/utils/grab-relative-path.js | 6 +++ package.json | 2 +- src/commands/build/index.ts | 15 ------- .../plugins/esbuild-ctx-artifact-tracker.ts | 20 +++++---- .../plugins/ssr-ctx-artifact-tracker.ts | 15 +++++-- src/functions/bundler/record-artifacts.ts | 35 ---------------- src/functions/server/handle-hmr.ts | 4 +- src/functions/server/handle-routes.ts | 42 ++++--------------- .../grab-page-bundled-react-component.tsx | 7 +++- .../grab-page-combined-server-res.ts | 14 +++++-- .../server/web-pages/grab-page-component.tsx | 14 ++++--- .../web-pages/grab-page-error-component.tsx | 4 +- src/utils/grab-relative-path.ts | 9 ++++ 26 files changed, 117 insertions(+), 204 deletions(-) delete mode 100644 dist/functions/bundler/record-artifacts.d.ts delete mode 100644 dist/functions/bundler/record-artifacts.js create mode 100644 dist/utils/grab-relative-path.d.ts create mode 100644 dist/utils/grab-relative-path.js delete mode 100644 src/functions/bundler/record-artifacts.ts create mode 100644 src/utils/grab-relative-path.ts diff --git a/README.md b/README.md index f01ff9e..a36c8f0 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ bun run dev | Command | Description | | -------------- | ------------------------------------------------------------------------------ | | `bunext dev` | Start the development server with HMR and file watching. | -| `bunext build` | Bundle all pages for production. Outputs artifacts to `.bunext/public/pages/`. Resolves absolute paths in the artifact maps at the configured build root — see [BUNEXT_BUILD_ROOT_DIR](#environment-variables). | +| `bunext build` | Bundle all pages for production. Outputs artifacts to `.bunext/public/pages/` using paths relative to the project root. | | `bunext start` | Start the production server using pre-built artifacts. | ### Running the CLI @@ -917,17 +917,6 @@ bun run server.ts | ---------------------- | ------------------------------------------------------------------------------------------------ | | `PORT` | Override the server port (takes precedence over config) | | `BUNEXT_PUBLIC_*` | Any env var prefixed with `BUNEXT_PUBLIC_` is exposed to the client via `window.process.env` | -| `BUNEXT_BUILD_ROOT_DIR` | Read by `bunext build` only. When set, the project root's absolute path is replaced with this value inside the artifact maps (`map.json` and `ssr-bundler-ctx-map.json`) before they are written. Use it when the project will be served from a different path than the one it was built at (e.g. building in CI at `/build/app` and running in a container mounted at `/app`). | - -### Build Root Directory - -Artifact maps store absolute filesystem paths. By default, `bunext build` records the build machine's current working directory as the root. If the project will be deployed or served from a different location, point the maps at the runtime root instead: - -```bash -BUNEXT_BUILD_ROOT_DIR=/app bun run build -``` - -Run `bunext start` from the directory matching `BUNEXT_BUILD_ROOT_DIR` so all recorded artifact paths resolve at request time. If unset, the path is left as-is and the runtime working directory must match the build directory. ### Public Environment Variables @@ -977,7 +966,7 @@ Running `bunext build`: 1. Sets `NODE_ENV=production`. 2. Runs ESBuild once with minification enabled. 3. Writes all bundled artifacts to `.bunext/public/pages/` plus the artifact maps `.bunext/public/pages/map.json` and `.bunext/public/pages/ssr-bundler-ctx-map.json`. -4. Artifact maps store absolute paths anchored at the project root. If `BUNEXT_BUILD_ROOT_DIR` is set, every occurrence of the build directory is replaced with that value so the maps point at the deploy-time location (see [Build Root Directory](#build-root-directory)). +4. Artifact maps record paths relative to the project root so they resolve at request time regardless of where the build ran. 5. Exits. ### Production Server diff --git a/dist/commands/build/index.js b/dist/commands/build/index.js index 83a2254..e3d42b5 100644 --- a/dist/commands/build/index.js +++ b/dist/commands/build/index.js @@ -23,17 +23,10 @@ export default function () { // await pagesSSRBundler(); if (global.BUNEXT_BUNDLER_CTX_MAP) { let bundler_ctx_json_str = JSON.stringify(global.BUNEXT_BUNDLER_CTX_MAP); - if (process.env.BUNEXT_BUILD_ROOT_DIR) { - bundler_ctx_json_str = bundler_ctx_json_str.replaceAll(ROOT_DIR, process.env.BUNEXT_BUILD_ROOT_DIR); - } writeFileSync(BUNDLER_CTX_MAP_JSON, bundler_ctx_json_str, "utf-8"); } if (global.BUNEXT_SSR_BUNDLER_CTX_MAP) { let ssr_bundler_ctx_json_str = JSON.stringify(global.BUNEXT_SSR_BUNDLER_CTX_MAP); - if (process.env.BUNEXT_BUILD_ROOT_DIR) { - ssr_bundler_ctx_json_str = - ssr_bundler_ctx_json_str.replaceAll(ROOT_DIR, process.env.BUNEXT_BUILD_ROOT_DIR); - } writeFileSync(SSR_BUNDLER_CTX_MAP_JSON, ssr_bundler_ctx_json_str, "utf-8"); } log.success("Modules Built Successfully!"); diff --git a/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js b/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js index 30a7d45..b928901 100644 --- a/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js +++ b/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js @@ -9,7 +9,8 @@ import { cpSync, existsSync, mkdirSync, rmSync, writeFileSync } from "fs"; import fullRebuild from "../../server/full-rebuild"; import path from "path"; import cleanupLogsDirs from "../../cleanup-logs-dir"; -const { BUNX_BUNDLER_ERROR_EXIT_FILE, BUNX_ERROR_LOGS_DIR, BUNDLER_CTX_MAP_JSON, SSR_BUNDLER_CTX_MAP_JSON, } = grabDirNames(); +import grabRelativePath from "../../../utils/grab-relative-path"; +const { BUNX_BUNDLER_ERROR_EXIT_FILE, BUNX_ERROR_LOGS_DIR, ROOT_DIR } = grabDirNames(); let build_start = 0; const MAX_BUILD_STARTS = 2; export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, build_only, }) { @@ -53,8 +54,9 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, const artifact = artifacts[i]; if (artifact?.local_path && global.BUNEXT_BUNDLER_CTX_MAP) { - global.BUNEXT_BUNDLER_CTX_MAP[artifact.local_path] = - _.merge(global.BUNEXT_BUNDLER_CTX_MAP[artifact.local_path], artifact); + const relative_path = grabRelativePath(artifact.local_path); + global.BUNEXT_BUNDLER_CTX_MAP[relative_path] = + _.merge(global.BUNEXT_BUNDLER_CTX_MAP[relative_path], { ...artifact, local_path: relative_path }); } } } diff --git a/dist/functions/bundler/plugins/ssr-ctx-artifact-tracker.js b/dist/functions/bundler/plugins/ssr-ctx-artifact-tracker.js index 7d5cf0e..d3763b7 100644 --- a/dist/functions/bundler/plugins/ssr-ctx-artifact-tracker.js +++ b/dist/functions/bundler/plugins/ssr-ctx-artifact-tracker.js @@ -3,10 +3,11 @@ import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-resu import { writeFileSync } from "fs"; import path from "path"; import grabDirNames from "../../../utils/grab-dir-names"; +import grabRelativePath from "../../../utils/grab-relative-path"; let build_start = 0; let build_starts = 0; const MAX_BUILD_STARTS = 2; -const { BUNX_TMP_DIR } = grabDirNames(); +const { BUNX_TMP_DIR, ROOT_DIR } = grabDirNames(); export default function ssrCTXArtifactTracker({ entryToPage, post_build_fn, }) { const artifactTracker = { name: "ssr-artifact-tracker", @@ -44,7 +45,11 @@ export default function ssrCTXArtifactTracker({ entryToPage, post_build_fn, }) { const artifact = artifacts[i]; if (artifact?.local_path && global.BUNEXT_SSR_BUNDLER_CTX_MAP) { - global.BUNEXT_SSR_BUNDLER_CTX_MAP[artifact.local_path] = artifact; + const relative_path = grabRelativePath(artifact.local_path); + global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_path] = { + ...artifact, + local_path: relative_path, + }; } } // post_build_fn?.({ artifacts }); diff --git a/dist/functions/bundler/record-artifacts.d.ts b/dist/functions/bundler/record-artifacts.d.ts deleted file mode 100644 index 05cfdd0..0000000 --- a/dist/functions/bundler/record-artifacts.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { BundlerCTXMap } from "../../types"; -type Params = { - artifacts: BundlerCTXMap[]; - page_file_paths?: string[]; -}; -export default function recordArtifacts({ artifacts, page_file_paths, }: Params): Promise; -export {}; diff --git a/dist/functions/bundler/record-artifacts.js b/dist/functions/bundler/record-artifacts.js deleted file mode 100644 index 629c718..0000000 --- a/dist/functions/bundler/record-artifacts.js +++ /dev/null @@ -1,18 +0,0 @@ -import grabDirNames from "../../utils/grab-dir-names"; -import _ from "lodash"; -const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames(); -export default async function recordArtifacts({ artifacts, page_file_paths, }) { - const artifacts_map = {}; - for (const artifact of artifacts) { - if (artifact?.local_path) { - artifacts_map[artifact.local_path] = artifact; - } - } - if (global.BUNEXT_BUNDLER_CTX_MAP) { - global.BUNEXT_BUNDLER_CTX_MAP = _.merge(global.BUNEXT_BUNDLER_CTX_MAP, artifacts_map); - } - // await Bun.write( - // HYDRATION_DST_DIR_MAP_JSON_FILE, - // JSON.stringify(artifacts_map, null, 4), - // ); -} diff --git a/dist/functions/server/handle-hmr.js b/dist/functions/server/handle-hmr.js index e455bde..1ae2bc0 100644 --- a/dist/functions/server/handle-hmr.js +++ b/dist/functions/server/handle-hmr.js @@ -1,3 +1,4 @@ +import grabRelativePath from "../../utils/grab-relative-path"; function removeController(controller) { const idx = global.BUNEXT_HMR_CONTROLLERS.findIndex((c) => c.controller == controller); if (typeof idx == "number" && idx >= 0) { @@ -19,7 +20,7 @@ export default async function ({ req }) { } 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; let heartbeat; diff --git a/dist/functions/server/handle-routes.js b/dist/functions/server/handle-routes.js index 5e1a7d8..e3527e0 100644 --- a/dist/functions/server/handle-routes.js +++ b/dist/functions/server/handle-routes.js @@ -5,6 +5,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(); export default async function ({ req }) { const url = new URL(req.url); @@ -30,8 +31,10 @@ export default async function ({ req }) { }); let module; const now = Date.now(); - if (is_dev && global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[match.filePath]?.path) { - const target_import = path.join(ROOT_DIR, 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[relative_match_path].path); module = await import(`${target_import}?t=${now}`); } else { @@ -99,31 +102,3 @@ export default async function ({ req }) { } 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); diff --git a/dist/functions/server/web-pages/grab-page-bundled-react-component.js b/dist/functions/server/web-pages/grab-page-bundled-react-component.js index 237a64d..a70f66e 100644 --- a/dist/functions/server/web-pages/grab-page-bundled-react-component.js +++ b/dist/functions/server/web-pages/grab-page-bundled-react-component.js @@ -4,11 +4,13 @@ import { log } from "../../../utils/log"; 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(); export default async function grabPageBundledReactComponent({ file_path, return_tsx_only, }) { try { - if (global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[file_path]) { - const abs = path.join(ROOT_DIR, global.BUNEXT_SSR_BUNDLER_CTX_MAP[file_path].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[relative_path].path); // Loader.registry.delete(abs); const mod = await import(`${abs}?t=${Date.now()}`); const Main = mod.default; diff --git a/dist/functions/server/web-pages/grab-page-combined-server-res.js b/dist/functions/server/web-pages/grab-page-combined-server-res.js index 0b60089..4eb726b 100644 --- a/dist/functions/server/web-pages/grab-page-combined-server-res.js +++ b/dist/functions/server/web-pages/grab-page-combined-server-res.js @@ -5,6 +5,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(); export default async function grabPageCombinedServerRes({ file_path, debug, url, query, routeParams, }) { const now = Date.now(); @@ -12,7 +13,9 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url, 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; @@ -33,7 +36,9 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url, log.info(`rootServerRes:`, rootServerRes); } 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; diff --git a/dist/functions/server/web-pages/grab-page-component.js b/dist/functions/server/web-pages/grab-page-component.js index 20b0845..654fdec 100644 --- a/dist/functions/server/web-pages/grab-page-component.js +++ b/dist/functions/server/web-pages/grab-page-component.js @@ -9,8 +9,8 @@ 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"; -const { BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames(); +import grabRelativePath from "../../../utils/grab-relative-path"; +const { BUNX_BUNDLER_ERROR_EXIT_FILE, ROOT_DIR } = grabDirNames(); class NotFoundError extends Error { status = 404; constructor(message) { @@ -51,7 +51,8 @@ export default async function grabPageComponent(params) { // log.error(errMsg); 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) { throw new Error(`Application Error. Please Check your components. ${match?.filePath} likely exists but has no exported module.`); @@ -60,10 +61,10 @@ export default async function grabPageComponent(params) { const MAX_RETRIES = 2; 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; } @@ -74,7 +75,7 @@ export default async function grabPageComponent(params) { } } 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) { log.info(`bundledMap:`, bundledMap); diff --git a/dist/functions/server/web-pages/grab-page-error-component.js b/dist/functions/server/web-pages/grab-page-error-component.js index e8a2587..7815cc6 100644 --- a/dist/functions/server/web-pages/grab-page-error-component.js +++ b/dist/functions/server/web-pages/grab-page-error-component.js @@ -4,6 +4,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"; export default async function grabPageErrorComponent({ error, routeParams, is404, url, }) { const router = global.BUNEXT_ROUTER; const is_dev = isDevelopment(); @@ -33,7 +34,7 @@ export default async function grabPageErrorComponent({ error, routeParams, is404 }; } 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, query: match?.query, diff --git a/dist/utils/grab-relative-path.d.ts b/dist/utils/grab-relative-path.d.ts new file mode 100644 index 0000000..1e97d42 --- /dev/null +++ b/dist/utils/grab-relative-path.d.ts @@ -0,0 +1 @@ +export default function grabRelativePath(path: string): string; diff --git a/dist/utils/grab-relative-path.js b/dist/utils/grab-relative-path.js new file mode 100644 index 0000000..3acefae --- /dev/null +++ b/dist/utils/grab-relative-path.js @@ -0,0 +1,6 @@ +import grabDirNames from "./grab-dir-names"; +const { ROOT_DIR } = grabDirNames(); +export default function grabRelativePath(path) { + const relative_path = path.replace(ROOT_DIR, "").replace(/^\//, ""); + return relative_path; +} diff --git a/package.json b/package.json index 4fbf0ad..c6ebb4d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bunext", - "version": "1.1.11", + "version": "1.1.12", "main": "dist/index.js", "module": "index.ts", "dependencies": { diff --git a/src/commands/build/index.ts b/src/commands/build/index.ts index 3a28ab9..c4be43d 100644 --- a/src/commands/build/index.ts +++ b/src/commands/build/index.ts @@ -37,13 +37,6 @@ export default function () { global.BUNEXT_BUNDLER_CTX_MAP, ); - if (process.env.BUNEXT_BUILD_ROOT_DIR) { - bundler_ctx_json_str = bundler_ctx_json_str.replaceAll( - ROOT_DIR, - process.env.BUNEXT_BUILD_ROOT_DIR, - ); - } - writeFileSync( BUNDLER_CTX_MAP_JSON, bundler_ctx_json_str, @@ -56,14 +49,6 @@ export default function () { global.BUNEXT_SSR_BUNDLER_CTX_MAP, ); - if (process.env.BUNEXT_BUILD_ROOT_DIR) { - ssr_bundler_ctx_json_str = - ssr_bundler_ctx_json_str.replaceAll( - ROOT_DIR, - process.env.BUNEXT_BUILD_ROOT_DIR, - ); - } - writeFileSync( SSR_BUNDLER_CTX_MAP_JSON, ssr_bundler_ctx_json_str, diff --git a/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts b/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts index 143636a..ae699ef 100644 --- a/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts +++ b/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts @@ -10,13 +10,10 @@ import { cpSync, existsSync, mkdirSync, rmSync, writeFileSync } from "fs"; import fullRebuild from "../../server/full-rebuild"; import path from "path"; import cleanupLogsDirs from "../../cleanup-logs-dir"; +import grabRelativePath from "../../../utils/grab-relative-path"; -const { - BUNX_BUNDLER_ERROR_EXIT_FILE, - BUNX_ERROR_LOGS_DIR, - BUNDLER_CTX_MAP_JSON, - SSR_BUNDLER_CTX_MAP_JSON, -} = grabDirNames(); +const { BUNX_BUNDLER_ERROR_EXIT_FILE, BUNX_ERROR_LOGS_DIR, ROOT_DIR } = + grabDirNames(); let build_start = 0; const MAX_BUILD_STARTS = 2; @@ -94,16 +91,21 @@ export default function esbuildCTXArtifactTracker({ if (artifacts?.[0] && artifacts.length > 0) { for (let i = 0; i < artifacts.length; i++) { const artifact = artifacts[i]; + if ( artifact?.local_path && global.BUNEXT_BUNDLER_CTX_MAP ) { - global.BUNEXT_BUNDLER_CTX_MAP[artifact.local_path] = + const relative_path = grabRelativePath( + artifact.local_path, + ); + + global.BUNEXT_BUNDLER_CTX_MAP[relative_path] = _.merge( global.BUNEXT_BUNDLER_CTX_MAP[ - artifact.local_path + relative_path ], - artifact, + { ...artifact, local_path: relative_path }, ); } } diff --git a/src/functions/bundler/plugins/ssr-ctx-artifact-tracker.ts b/src/functions/bundler/plugins/ssr-ctx-artifact-tracker.ts index afd3865..cd93a7c 100644 --- a/src/functions/bundler/plugins/ssr-ctx-artifact-tracker.ts +++ b/src/functions/bundler/plugins/ssr-ctx-artifact-tracker.ts @@ -4,12 +4,13 @@ import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-resu import { writeFileSync } from "fs"; import path from "path"; import grabDirNames from "../../../utils/grab-dir-names"; +import grabRelativePath from "../../../utils/grab-relative-path"; let build_start = 0; let build_starts = 0; const MAX_BUILD_STARTS = 2; -const { BUNX_TMP_DIR } = grabDirNames(); +const { BUNX_TMP_DIR, ROOT_DIR } = grabDirNames(); type Params = { entryToPage: Map< @@ -63,13 +64,19 @@ export default function ssrCTXArtifactTracker({ if (artifacts?.[0] && artifacts.length > 0) { for (let i = 0; i < artifacts.length; i++) { const artifact = artifacts[i]; + if ( artifact?.local_path && global.BUNEXT_SSR_BUNDLER_CTX_MAP ) { - global.BUNEXT_SSR_BUNDLER_CTX_MAP[ - artifact.local_path - ] = artifact; + const relative_path = grabRelativePath( + artifact.local_path, + ); + + global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_path] = { + ...artifact, + local_path: relative_path, + }; } } diff --git a/src/functions/bundler/record-artifacts.ts b/src/functions/bundler/record-artifacts.ts deleted file mode 100644 index 517ff9c..0000000 --- a/src/functions/bundler/record-artifacts.ts +++ /dev/null @@ -1,35 +0,0 @@ -import grabDirNames from "../../utils/grab-dir-names"; -import type { BundlerCTXMap } from "../../types"; -import _ from "lodash"; - -const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames(); - -type Params = { - artifacts: BundlerCTXMap[]; - page_file_paths?: string[]; -}; - -export default async function recordArtifacts({ - artifacts, - page_file_paths, -}: Params) { - const artifacts_map: { [k: string]: BundlerCTXMap } = {}; - - for (const artifact of artifacts) { - if (artifact?.local_path) { - artifacts_map[artifact.local_path] = artifact; - } - } - - if (global.BUNEXT_BUNDLER_CTX_MAP) { - global.BUNEXT_BUNDLER_CTX_MAP = _.merge( - global.BUNEXT_BUNDLER_CTX_MAP, - artifacts_map, - ); - } - - // await Bun.write( - // HYDRATION_DST_DIR_MAP_JSON_FILE, - // JSON.stringify(artifacts_map, null, 4), - // ); -} diff --git a/src/functions/server/handle-hmr.ts b/src/functions/server/handle-hmr.ts index 07b33d8..0a69b8c 100644 --- a/src/functions/server/handle-hmr.ts +++ b/src/functions/server/handle-hmr.ts @@ -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 { 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; diff --git a/src/functions/server/handle-routes.ts b/src/functions/server/handle-routes.ts index 3d745d1..f6c53e3 100644 --- a/src/functions/server/handle-routes.ts +++ b/src/functions/server/handle-routes.ts @@ -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 { 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 { 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); diff --git a/src/functions/server/web-pages/grab-page-bundled-react-component.tsx b/src/functions/server/web-pages/grab-page-bundled-react-component.tsx index e2d97f3..e391ae7 100644 --- a/src/functions/server/web-pages/grab-page-bundled-react-component.tsx +++ b/src/functions/server/web-pages/grab-page-bundled-react-component.tsx @@ -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 { 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()}`); diff --git a/src/functions/server/web-pages/grab-page-combined-server-res.ts b/src/functions/server/web-pages/grab-page-combined-server-res.ts index 9f6d139..31afac0 100644 --- a/src/functions/server/web-pages/grab-page-combined-server-res.ts +++ b/src/functions/server/web-pages/grab-page-combined-server-res.ts @@ -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; diff --git a/src/functions/server/web-pages/grab-page-component.tsx b/src/functions/server/web-pages/grab-page-component.tsx index f08f9c2..f7d084f 100644 --- a/src/functions/server/web-pages/grab-page-component.tsx +++ b/src/functions/server/web-pages/grab-page-component.tsx @@ -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) { diff --git a/src/functions/server/web-pages/grab-page-error-component.tsx b/src/functions/server/web-pages/grab-page-error-component.tsx index 728d03e..586df70 100644 --- a/src/functions/server/web-pages/grab-page-error-component.tsx +++ b/src/functions/server/web-pages/grab-page-error-component.tsx @@ -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, diff --git a/src/utils/grab-relative-path.ts b/src/utils/grab-relative-path.ts new file mode 100644 index 0000000..a01811d --- /dev/null +++ b/src/utils/grab-relative-path.ts @@ -0,0 +1,9 @@ +import grabDirNames from "./grab-dir-names"; + +const { ROOT_DIR } = grabDirNames(); + +export default function grabRelativePath(path: string): string { + const relative_path = path.replace(ROOT_DIR, "").replace(/^\//, ""); + + return relative_path; +}