Refactor artifacts to use relative directory for build step and retrieval
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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<void>;
|
||||
export {};
|
||||
-18
@@ -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),
|
||||
// );
|
||||
}
|
||||
Vendored
+2
-1
@@ -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;
|
||||
|
||||
+5
-30
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
+7
-6
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user