Bugfix: SSR bundler errors fixed. Caching still faulty

This commit is contained in:
Benjamin Toby 2026-04-09 22:33:49 +01:00
parent f3b087a1f3
commit 40fc7778a8
12 changed files with 139 additions and 81 deletions

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 });
}

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;

View File

@ -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 };
}

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;
};

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 },

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`);

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/bunext",
"version": "1.0.61",
"version": "1.0.62",
"main": "dist/index.js",
"module": "index.ts",
"dependencies": {

View File

@ -52,11 +52,6 @@ export default async function initPages(params?: 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 });

View File

@ -25,6 +25,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;

View File

@ -38,14 +38,12 @@ export default async function grabPageCombinedServerRes({
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;
await grabPageServerRes({
server_function: root_server_fn,
url,
query,
routeParams,
});
if (debug) {
log.info(`rootServerRes:`, rootServerRes);
@ -58,14 +56,13 @@ export default async function grabPageCombinedServerRes({
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;
const serverRes: BunextPageModuleServerReturn | undefined =
await grabPageServerRes({
server_function: server_fn,
url,
query,
routeParams,
});
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});

View File

@ -10,7 +10,7 @@ import { log } from "../../../utils/log";
type Params = {
url?: URL;
server_function: BunextPageServerFn;
server_function?: BunextPageServerFn;
query?: Record<string, string>;
routeParams?: BunxRouteParams;
};
@ -44,7 +44,7 @@ export default async function grabPageServerRes({
};
try {
if (routeParams) {
if (routeParams && server_function) {
const serverData = await server_function({
...routeParams,
query: { ...routeParams.query, ...query },

View File

@ -7,9 +7,9 @@ import type {
GrabTSXModuleBatchParams,
GrabTSXModuleSingleParams,
} from "../../../types";
import { existsSync, unlinkSync } from "fs";
import { existsSync, rmSync, unlinkSync } from "fs";
const { PAGES_DIR, BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames();
const { PAGES_DIR, BUNX_CWD_MODULE_CACHE_DIR, ROOT_DIR } = grabDirNames();
type Params = GrabTSXModuleSingleParams | GrabTSXModuleBatchParams;
@ -32,31 +32,35 @@ type BuildEntriesParams = {
async function buildEntries({ entries, clean_cache }: BuildEntriesParams) {
const dev = isDevelopment();
const toBuild: { tsx: string; mod_file_path: string }[] = [];
const toBuild: {
tsx: string;
mod_file_path: string;
}[] = [];
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;
const virtualEntries: Record<string, string> = {};
for (const { tsx, mod_file_path } of toBuild) {
virtualEntries[mod_file_path] = tsx;
}
@ -68,7 +72,10 @@ async function buildEntries({ entries, clean_cache }: BuildEntriesParams) {
build.onResolve({ filter: /.*/ }, (args) => {
if (entryPaths.has(args.path)) {
return { path: args.path, namespace: "virtual" };
return {
path: args.path,
namespace: "virtual",
};
}
});
@ -77,11 +84,41 @@ async function buildEntries({ entries, clean_cache }: BuildEntriesParams) {
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",
@ -101,7 +138,8 @@ async function buildEntries({ entries, clean_cache }: BuildEntriesParams) {
jsx: "automatic",
outdir: BUNX_CWD_MODULE_CACHE_DIR,
plugins: [virtualPlugin, tailwindEsbuildPlugin],
logLevel: "silent",
metafile: true,
// logLevel: "silent",
});
}
@ -140,7 +178,10 @@ export default async function grabTsxStringModule<T>(
}
try {
await buildEntries({ entries: [params], clean_cache: true });
await buildEntries({
entries: [params],
clean_cache: true,
});
} catch (error) {
console.error(`SSR Single Build Error\n`);
console.log(error);