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, tsx,
page_file_path: page.local_path, page_file_path: page.local_path,
}); });
// const component = await grabPageComponent({
// file_path: page.local_path,
// skip_server_res: true,
// });
} }
await grabTsxStringModule({ tsx_map }); await grabTsxStringModule({ tsx_map });
} }

View File

@ -10,6 +10,7 @@ declare global {
var CONFIG: BunextConfig; var CONFIG: BunextConfig;
var SERVER: Server<any> | undefined; var SERVER: Server<any> | undefined;
var RECOMPILING: boolean; var RECOMPILING: boolean;
var BUILDING_SSR: boolean;
var IS_SERVER_COMPONENT: boolean; var IS_SERVER_COMPONENT: boolean;
var WATCHER_TIMEOUT: any; var WATCHER_TIMEOUT: any;
var ROUTER: FileSystemRouter; 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}`) ? await import(`${root_server_file_path}?t=${now}`)
: undefined; : undefined;
const root_server_fn = root_server_module?.default || root_server_module?.server; const root_server_fn = root_server_module?.default || root_server_module?.server;
const rootServerRes = root_server_fn const rootServerRes = await grabPageServerRes({
? await grabPageServerRes({
server_function: root_server_fn, server_function: root_server_fn,
url, url,
query, query,
routeParams, routeParams,
}) });
: undefined;
if (debug) { if (debug) {
log.info(`rootServerRes:`, rootServerRes); log.info(`rootServerRes:`, rootServerRes);
} }
@ -29,14 +27,12 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
? await import(`${server_file_path}?t=${now}`) ? await import(`${server_file_path}?t=${now}`)
: undefined; : undefined;
const server_fn = server_module?.default || server_module?.server; const server_fn = server_module?.default || server_module?.server;
const serverRes = server_fn const serverRes = await grabPageServerRes({
? await grabPageServerRes({
server_function: server_fn, server_function: server_fn,
url, url,
query, query,
routeParams, routeParams,
}) });
: undefined;
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {}); const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
return { serverRes: mergedServerRes }; return { serverRes: mergedServerRes };
} }

View File

@ -1,7 +1,7 @@
import type { BunextPageModuleServerReturn, BunextPageServerFn, BunxRouteParams } from "../../../types"; import type { BunextPageModuleServerReturn, BunextPageServerFn, BunxRouteParams } from "../../../types";
type Params = { type Params = {
url?: URL; url?: URL;
server_function: BunextPageServerFn; server_function?: BunextPageServerFn;
query?: Record<string, string>; query?: Record<string, string>;
routeParams?: BunxRouteParams; routeParams?: BunxRouteParams;
}; };

View File

@ -23,7 +23,7 @@ export default async function grabPageServerRes({ url, query, routeParams, serve
query, query,
}; };
try { try {
if (routeParams) { if (routeParams && server_function) {
const serverData = await server_function({ const serverData = await server_function({
...routeParams, ...routeParams,
query: { ...routeParams.query, ...query }, query: { ...routeParams.query, ...query },

View File

@ -3,8 +3,8 @@ import * as esbuild from "esbuild";
import grabDirNames from "../../../utils/grab-dir-names"; import grabDirNames from "../../../utils/grab-dir-names";
import path from "path"; import path from "path";
import tailwindEsbuildPlugin from "./tailwind-esbuild-plugin"; import tailwindEsbuildPlugin from "./tailwind-esbuild-plugin";
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();
function toModPath(page_file_path) { function toModPath(page_file_path) {
return path.join(BUNX_CWD_MODULE_CACHE_DIR, page_file_path.replace(PAGES_DIR, "").replace(/\.(t|j)sx?$/, ".js")); 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 = []; const toBuild = [];
for (const entry of entries) { for (const entry of entries) {
const mod_file_path = toModPath(entry.page_file_path); const mod_file_path = toModPath(entry.page_file_path);
if (!global.REACT_DOM_MODULE_CACHE.has(entry.page_file_path) && // try {
!(await Bun.file(mod_file_path).exists())) { // 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({ toBuild.push({
tsx: entry.tsx, tsx: entry.tsx,
mod_file_path, mod_file_path,
}); });
} }
else {
try {
if (clean_cache && existsSync(mod_file_path)) {
unlinkSync(mod_file_path);
}
}
catch (error) { }
}
} }
if (toBuild.length === 0) if (toBuild.length === 0)
return; return;
@ -44,7 +42,10 @@ async function buildEntries({ entries, clean_cache }) {
const entryPaths = new Set(Object.keys(virtualEntries)); const entryPaths = new Set(Object.keys(virtualEntries));
build.onResolve({ filter: /.*/ }, (args) => { build.onResolve({ filter: /.*/ }, (args) => {
if (entryPaths.has(args.path)) { if (entryPaths.has(args.path)) {
return { path: args.path, namespace: "virtual" }; return {
path: args.path,
namespace: "virtual",
};
} }
}); });
build.onLoad({ filter: /.*/, namespace: "virtual" }, (args) => ({ build.onLoad({ filter: /.*/, namespace: "virtual" }, (args) => ({
@ -52,10 +53,36 @@ async function buildEntries({ entries, clean_cache }) {
resolveDir: process.cwd(), resolveDir: process.cwd(),
loader: "tsx", 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({ const entryPoints = Object.keys(virtualEntries);
entryPoints: Object.keys(virtualEntries), const build = await esbuild.build({
entryPoints,
bundle: true, bundle: true,
format: "esm", format: "esm",
target: "es2020", target: "es2020",
@ -73,7 +100,8 @@ async function buildEntries({ entries, clean_cache }) {
jsx: "automatic", jsx: "automatic",
outdir: BUNX_CWD_MODULE_CACHE_DIR, outdir: BUNX_CWD_MODULE_CACHE_DIR,
plugins: [virtualPlugin, tailwindEsbuildPlugin], plugins: [virtualPlugin, tailwindEsbuildPlugin],
logLevel: "silent", metafile: true,
// logLevel: "silent",
}); });
} }
async function loadEntry(page_file_path) { 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))); return Promise.all(params.tsx_map.map((entry) => loadEntry(entry.page_file_path)));
} }
try { try {
await buildEntries({ entries: [params], clean_cache: true }); await buildEntries({
entries: [params],
clean_cache: true,
});
} }
catch (error) { catch (error) {
console.error(`SSR Single Build Error\n`); console.error(`SSR Single Build Error\n`);

View File

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

View File

@ -52,11 +52,6 @@ export default async function initPages(params?: Params) {
tsx, tsx,
page_file_path: page.local_path, page_file_path: page.local_path,
}); });
// const component = await grabPageComponent({
// file_path: page.local_path,
// skip_server_res: true,
// });
} }
await grabTsxStringModule({ tsx_map }); await grabTsxStringModule({ tsx_map });

View File

@ -25,6 +25,7 @@ declare global {
var CONFIG: BunextConfig; var CONFIG: BunextConfig;
var SERVER: Server<any> | undefined; var SERVER: Server<any> | undefined;
var RECOMPILING: boolean; var RECOMPILING: boolean;
var BUILDING_SSR: boolean;
var IS_SERVER_COMPONENT: boolean; var IS_SERVER_COMPONENT: boolean;
var WATCHER_TIMEOUT: any; var WATCHER_TIMEOUT: any;
var ROUTER: FileSystemRouter; var ROUTER: FileSystemRouter;

View File

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

View File

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

View File

@ -7,9 +7,9 @@ import type {
GrabTSXModuleBatchParams, GrabTSXModuleBatchParams,
GrabTSXModuleSingleParams, GrabTSXModuleSingleParams,
} from "../../../types"; } 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; type Params = GrabTSXModuleSingleParams | GrabTSXModuleBatchParams;
@ -32,31 +32,35 @@ type BuildEntriesParams = {
async function buildEntries({ entries, clean_cache }: BuildEntriesParams) { async function buildEntries({ entries, clean_cache }: BuildEntriesParams) {
const dev = isDevelopment(); const dev = isDevelopment();
const toBuild: { tsx: string; mod_file_path: string }[] = []; const toBuild: {
tsx: string;
mod_file_path: string;
}[] = [];
for (const entry of entries) { for (const entry of entries) {
const mod_file_path = toModPath(entry.page_file_path); const mod_file_path = toModPath(entry.page_file_path);
if ( // try {
!global.REACT_DOM_MODULE_CACHE.has(entry.page_file_path) && // if (clean_cache && existsSync(mod_file_path)) {
!(await Bun.file(mod_file_path).exists()) // 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({ toBuild.push({
tsx: entry.tsx, tsx: entry.tsx,
mod_file_path, mod_file_path,
}); });
} else {
try {
if (clean_cache && existsSync(mod_file_path)) {
unlinkSync(mod_file_path);
}
} catch (error) {}
} }
} }
if (toBuild.length === 0) return; if (toBuild.length === 0) return;
const virtualEntries: Record<string, string> = {}; const virtualEntries: Record<string, string> = {};
for (const { tsx, mod_file_path } of toBuild) { for (const { tsx, mod_file_path } of toBuild) {
virtualEntries[mod_file_path] = tsx; virtualEntries[mod_file_path] = tsx;
} }
@ -68,7 +72,10 @@ async function buildEntries({ entries, clean_cache }: BuildEntriesParams) {
build.onResolve({ filter: /.*/ }, (args) => { build.onResolve({ filter: /.*/ }, (args) => {
if (entryPaths.has(args.path)) { 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(), resolveDir: process.cwd(),
loader: "tsx", 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({ const entryPoints = Object.keys(virtualEntries);
entryPoints: Object.keys(virtualEntries),
const build = await esbuild.build({
entryPoints,
bundle: true, bundle: true,
format: "esm", format: "esm",
target: "es2020", target: "es2020",
@ -101,7 +138,8 @@ async function buildEntries({ entries, clean_cache }: BuildEntriesParams) {
jsx: "automatic", jsx: "automatic",
outdir: BUNX_CWD_MODULE_CACHE_DIR, outdir: BUNX_CWD_MODULE_CACHE_DIR,
plugins: [virtualPlugin, tailwindEsbuildPlugin], plugins: [virtualPlugin, tailwindEsbuildPlugin],
logLevel: "silent", metafile: true,
// logLevel: "silent",
}); });
} }
@ -140,7 +178,10 @@ export default async function grabTsxStringModule<T>(
} }
try { try {
await buildEntries({ entries: [params], clean_cache: true }); await buildEntries({
entries: [params],
clean_cache: true,
});
} catch (error) { } catch (error) {
console.error(`SSR Single Build Error\n`); console.error(`SSR Single Build Error\n`);
console.log(error); console.log(error);