Bugfix: SSR bundler errors fixed. Caching still faulty

This commit is contained in:
2026-04-09 22:33:49 +01:00
parent f3b087a1f3
commit 40fc7778a8
12 changed files with 139 additions and 81 deletions
-4
View File
@@ -33,10 +33,6 @@ export default async function initPages(params) {
tsx,
page_file_path: page.local_path,
});
// const component = await grabPageComponent({
// file_path: page.local_path,
// skip_server_res: true,
// });
}
await grabTsxStringModule({ tsx_map });
}
+1
View File
@@ -10,6 +10,7 @@ declare global {
var CONFIG: BunextConfig;
var SERVER: Server<any> | undefined;
var RECOMPILING: boolean;
var BUILDING_SSR: boolean;
var IS_SERVER_COMPONENT: boolean;
var WATCHER_TIMEOUT: any;
var ROUTER: FileSystemRouter;
@@ -13,14 +13,12 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
? await import(`${root_server_file_path}?t=${now}`)
: undefined;
const root_server_fn = root_server_module?.default || root_server_module?.server;
const rootServerRes = root_server_fn
? await grabPageServerRes({
server_function: root_server_fn,
url,
query,
routeParams,
})
: undefined;
const rootServerRes = await grabPageServerRes({
server_function: root_server_fn,
url,
query,
routeParams,
});
if (debug) {
log.info(`rootServerRes:`, rootServerRes);
}
@@ -29,14 +27,12 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
? await import(`${server_file_path}?t=${now}`)
: undefined;
const server_fn = server_module?.default || server_module?.server;
const serverRes = server_fn
? await grabPageServerRes({
server_function: server_fn,
url,
query,
routeParams,
})
: undefined;
const serverRes = await grabPageServerRes({
server_function: server_fn,
url,
query,
routeParams,
});
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
return { serverRes: mergedServerRes };
}
+1 -1
View File
@@ -1,7 +1,7 @@
import type { BunextPageModuleServerReturn, BunextPageServerFn, BunxRouteParams } from "../../../types";
type Params = {
url?: URL;
server_function: BunextPageServerFn;
server_function?: BunextPageServerFn;
query?: Record<string, string>;
routeParams?: BunxRouteParams;
};
+1 -1
View File
@@ -23,7 +23,7 @@ export default async function grabPageServerRes({ url, query, routeParams, serve
query,
};
try {
if (routeParams) {
if (routeParams && server_function) {
const serverData = await server_function({
...routeParams,
query: { ...routeParams.query, ...query },
+48 -17
View File
@@ -3,8 +3,8 @@ import * as esbuild from "esbuild";
import grabDirNames from "../../../utils/grab-dir-names";
import path from "path";
import tailwindEsbuildPlugin from "./tailwind-esbuild-plugin";
import { existsSync, unlinkSync } from "fs";
const { PAGES_DIR, BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames();
import { existsSync, rmSync, unlinkSync } from "fs";
const { PAGES_DIR, BUNX_CWD_MODULE_CACHE_DIR, ROOT_DIR } = grabDirNames();
function toModPath(page_file_path) {
return path.join(BUNX_CWD_MODULE_CACHE_DIR, page_file_path.replace(PAGES_DIR, "").replace(/\.(t|j)sx?$/, ".js"));
}
@@ -16,21 +16,19 @@ async function buildEntries({ entries, clean_cache }) {
const toBuild = [];
for (const entry of entries) {
const mod_file_path = toModPath(entry.page_file_path);
if (!global.REACT_DOM_MODULE_CACHE.has(entry.page_file_path) &&
!(await Bun.file(mod_file_path).exists())) {
// try {
// if (clean_cache && existsSync(mod_file_path)) {
// console.log(`Removing ${mod_file_path}`);
// await Bun.file(mod_file_path).delete();
// }
// } catch (error) {}
const does_mod_file_path_exists = existsSync(mod_file_path);
if (!does_mod_file_path_exists) {
toBuild.push({
tsx: entry.tsx,
mod_file_path,
});
}
else {
try {
if (clean_cache && existsSync(mod_file_path)) {
unlinkSync(mod_file_path);
}
}
catch (error) { }
}
}
if (toBuild.length === 0)
return;
@@ -44,7 +42,10 @@ async function buildEntries({ entries, clean_cache }) {
const entryPaths = new Set(Object.keys(virtualEntries));
build.onResolve({ filter: /.*/ }, (args) => {
if (entryPaths.has(args.path)) {
return { path: args.path, namespace: "virtual" };
return {
path: args.path,
namespace: "virtual",
};
}
});
build.onLoad({ filter: /.*/, namespace: "virtual" }, (args) => ({
@@ -52,10 +53,36 @@ async function buildEntries({ entries, clean_cache }) {
resolveDir: process.cwd(),
loader: "tsx",
}));
build.onEnd((result) => {
if (result.errors.length > 0) {
console.log(`Build Errors =>`, result.errors);
return;
}
// const artifacts: any[] = Object.entries(
// result.metafile!.outputs,
// )
// .filter(([, meta]) => meta.entryPoint)
// .map(([outputPath, meta]) => {
// return {
// path: outputPath,
// hash: path.basename(
// outputPath,
// path.extname(outputPath),
// ),
// type: outputPath.endsWith(".css")
// ? "text/css"
// : "text/javascript",
// entrypoint: meta.entryPoint,
// css_path: meta.cssBundle,
// };
// });
// console.log("artifacts", artifacts);
});
},
};
await esbuild.build({
entryPoints: Object.keys(virtualEntries),
const entryPoints = Object.keys(virtualEntries);
const build = await esbuild.build({
entryPoints,
bundle: true,
format: "esm",
target: "es2020",
@@ -73,7 +100,8 @@ async function buildEntries({ entries, clean_cache }) {
jsx: "automatic",
outdir: BUNX_CWD_MODULE_CACHE_DIR,
plugins: [virtualPlugin, tailwindEsbuildPlugin],
logLevel: "silent",
metafile: true,
// logLevel: "silent",
});
}
async function loadEntry(page_file_path) {
@@ -102,7 +130,10 @@ export default async function grabTsxStringModule(params) {
return Promise.all(params.tsx_map.map((entry) => loadEntry(entry.page_file_path)));
}
try {
await buildEntries({ entries: [params], clean_cache: true });
await buildEntries({
entries: [params],
clean_cache: true,
});
}
catch (error) {
console.error(`SSR Single Build Error\n`);