diff --git a/dist/functions/bundler/init-pages.js b/dist/functions/bundler/init-pages.js index d2a0c0c..4740397 100644 --- a/dist/functions/bundler/init-pages.js +++ b/dist/functions/bundler/init-pages.js @@ -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 }); } diff --git a/dist/functions/bunext-init.d.ts b/dist/functions/bunext-init.d.ts index 9d822e4..91a3511 100644 --- a/dist/functions/bunext-init.d.ts +++ b/dist/functions/bunext-init.d.ts @@ -10,6 +10,7 @@ declare global { var CONFIG: BunextConfig; var SERVER: Server | undefined; var RECOMPILING: boolean; + var BUILDING_SSR: boolean; var IS_SERVER_COMPONENT: boolean; var WATCHER_TIMEOUT: any; var ROUTER: FileSystemRouter; 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 9d2e183..57099b8 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 @@ -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 }; } diff --git a/dist/functions/server/web-pages/grab-page-server-res.d.ts b/dist/functions/server/web-pages/grab-page-server-res.d.ts index 10025db..059c9fd 100644 --- a/dist/functions/server/web-pages/grab-page-server-res.d.ts +++ b/dist/functions/server/web-pages/grab-page-server-res.d.ts @@ -1,7 +1,7 @@ import type { BunextPageModuleServerReturn, BunextPageServerFn, BunxRouteParams } from "../../../types"; type Params = { url?: URL; - server_function: BunextPageServerFn; + server_function?: BunextPageServerFn; query?: Record; routeParams?: BunxRouteParams; }; diff --git a/dist/functions/server/web-pages/grab-page-server-res.js b/dist/functions/server/web-pages/grab-page-server-res.js index f2a33f1..b0254f4 100644 --- a/dist/functions/server/web-pages/grab-page-server-res.js +++ b/dist/functions/server/web-pages/grab-page-server-res.js @@ -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 }, diff --git a/dist/functions/server/web-pages/grab-tsx-string-module.js b/dist/functions/server/web-pages/grab-tsx-string-module.js index b972ba0..a664ca4 100644 --- a/dist/functions/server/web-pages/grab-tsx-string-module.js +++ b/dist/functions/server/web-pages/grab-tsx-string-module.js @@ -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`); diff --git a/package.json b/package.json index bc2ee9d..87157f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bunext", - "version": "1.0.61", + "version": "1.0.62", "main": "dist/index.js", "module": "index.ts", "dependencies": { diff --git a/src/functions/bundler/init-pages.ts b/src/functions/bundler/init-pages.ts index 54477d8..8ec7225 100644 --- a/src/functions/bundler/init-pages.ts +++ b/src/functions/bundler/init-pages.ts @@ -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 }); diff --git a/src/functions/bunext-init.ts b/src/functions/bunext-init.ts index 0f9ec20..612264e 100644 --- a/src/functions/bunext-init.ts +++ b/src/functions/bunext-init.ts @@ -25,6 +25,7 @@ declare global { var CONFIG: BunextConfig; var SERVER: Server | undefined; var RECOMPILING: boolean; + var BUILDING_SSR: boolean; var IS_SERVER_COMPONENT: boolean; var WATCHER_TIMEOUT: any; var ROUTER: FileSystemRouter; 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 d62e8c1..433be53 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 @@ -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 || {}); diff --git a/src/functions/server/web-pages/grab-page-server-res.tsx b/src/functions/server/web-pages/grab-page-server-res.tsx index 1a55f02..627edfc 100644 --- a/src/functions/server/web-pages/grab-page-server-res.tsx +++ b/src/functions/server/web-pages/grab-page-server-res.tsx @@ -10,7 +10,7 @@ import { log } from "../../../utils/log"; type Params = { url?: URL; - server_function: BunextPageServerFn; + server_function?: BunextPageServerFn; query?: Record; 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 }, diff --git a/src/functions/server/web-pages/grab-tsx-string-module.tsx b/src/functions/server/web-pages/grab-tsx-string-module.tsx index 8b95d18..89b141c 100644 --- a/src/functions/server/web-pages/grab-tsx-string-module.tsx +++ b/src/functions/server/web-pages/grab-tsx-string-module.tsx @@ -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 = {}; + 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( } 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);