diff --git a/dist/functions/bundler/pages-ssr-bundler.d.ts b/dist/functions/bundler/pages-ssr-bundler.d.ts new file mode 100644 index 0000000..ace662f --- /dev/null +++ b/dist/functions/bundler/pages-ssr-bundler.d.ts @@ -0,0 +1,7 @@ +type Params = { + post_build_fn?: (params: { + artifacts: any[]; + }) => Promise | void; +}; +export default function pagesSSRBundler(params?: Params): Promise; +export {}; diff --git a/dist/functions/bundler/pages-ssr-bundler.js b/dist/functions/bundler/pages-ssr-bundler.js new file mode 100644 index 0000000..4db0b30 --- /dev/null +++ b/dist/functions/bundler/pages-ssr-bundler.js @@ -0,0 +1,65 @@ +import * as esbuild from "esbuild"; +import grabAllPages from "../../utils/grab-all-pages"; +import grabDirNames from "../../utils/grab-dir-names"; +import isDevelopment from "../../utils/is-development"; +import tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin"; +import grabPageReactComponentString from "../server/web-pages/grab-page-react-component-string"; +import grabRootFilePath from "../server/web-pages/grab-root-file-path"; +import ssrVirtualFilesPlugin from "./plugins/ssr-virtual-files-plugin"; +import ssrCTXArtifactTracker from "./plugins/ssr-ctx-artifact-tracker"; +const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames(); +export default async function pagesSSRBundler(params) { + const pages = grabAllPages(); + const dev = isDevelopment(); + const entryToPage = new Map(); + const { root_file_path } = grabRootFilePath(); + for (const page of pages) { + if (page.local_path.match(/\/pages\/api\//)) { + const ts = await Bun.file(page.local_path).text(); + entryToPage.set(page.local_path, { ...page, tsx: ts }); + continue; + } + const tsx = grabPageReactComponentString({ + file_path: page.local_path, + root_file_path, + }); + if (!tsx) + continue; + entryToPage.set(page.local_path, { ...page, tsx }); + } + const entryPoints = [...entryToPage.keys()].map((e) => `ssr-virtual:${e}`); + await esbuild.build({ + entryPoints, + outdir: BUNX_CWD_MODULE_CACHE_DIR, + bundle: true, + minify: !dev, + format: "esm", + target: "esnext", + platform: "node", + define: { + "process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"), + }, + entryNames: "[dir]/[hash]", + metafile: true, + plugins: [ + tailwindEsbuildPlugin, + ssrVirtualFilesPlugin({ + entryToPage, + }), + ssrCTXArtifactTracker({ + entryToPage, + post_build_fn: params?.post_build_fn, + }), + ], + jsx: "automatic", + external: [ + "react", + "react-dom", + "react/jsx-runtime", + "react/jsx-dev-runtime", + "bun:*", + ], + splitting: true, + // logLevel: "silent", + }); +} diff --git a/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js b/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js index 09d216e..195bb7a 100644 --- a/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js +++ b/dist/functions/bundler/plugins/esbuild-ctx-artifact-tracker.js @@ -1,10 +1,9 @@ import {} from "esbuild"; import { log } from "../../../utils/log"; import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result"; -import pagesSSRContextBundler from "../pages-ssr-context-bundler"; import buildOnstartErrorHandler from "../build-on-start-error-handler"; -import apiRoutesContextBundler from "../api-routes-context-bundler"; import _ from "lodash"; +import pagesSSRBundler from "../pages-ssr-bundler"; let build_start = 0; let build_starts = 0; const MAX_BUILD_STARTS = 2; @@ -42,12 +41,12 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, global.RECOMPILING = false; global.IS_SERVER_COMPONENT = false; build_starts = 0; - if (global.SSR_BUNDLER_CTX) { - global.SSR_BUNDLER_CTX.rebuild(); - } - else { - pagesSSRContextBundler(); - } + pagesSSRBundler(); + // if (global.SSR_BUNDLER_CTX) { + // global.SSR_BUNDLER_CTX.rebuild(); + // } else { + // pagesSSRContextBundler(); + // } }); }, }; diff --git a/dist/functions/bunext-init.js b/dist/functions/bunext-init.js index 32e4385..590bea4 100644 --- a/dist/functions/bunext-init.js +++ b/dist/functions/bunext-init.js @@ -48,3 +48,9 @@ export default async function bunextInit() { cron(); } } +process.on("exit", (code) => { + Bun.spawn([process.execPath, ...process.argv.slice(1)], { + stdio: ["inherit", "inherit", "inherit"], + env: process.env, + }); +}); diff --git a/dist/functions/server/web-pages/generate-web-html.js b/dist/functions/server/web-pages/generate-web-html.js index 6919211..74beb06 100644 --- a/dist/functions/server/web-pages/generate-web-html.js +++ b/dist/functions/server/web-pages/generate-web-html.js @@ -43,8 +43,14 @@ export default async function genWebHTML({ component: Main, pageProps, bundledMa const RootHead = root_module?.Head; const dev = isDevelopment(); const final_meta = _.merge(root_meta, page_meta); + // const public_envs = Object.keys(process.env).filter((e) => + // e.startsWith(`NEXT_PUBLIC_`), + // ); + const client_process = { + env: {}, + }; let final_component = (_jsxs("html", { ...html_props, children: [_jsxs("head", { children: [_jsx("meta", { charSet: "utf-8", "data-bunext-head": true }), _jsx("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0", "data-bunext-head": true }), final_meta ? grabWebMetaHTML({ meta: final_meta }) : null, bundledMap?.css_path ? (_jsx("link", { rel: "stylesheet", href: `/${bundledMap.css_path}`, "data-bunext-head": true })) : null, _jsx("script", { dangerouslySetInnerHTML: { - __html: `window.${ClientWindowPagePropsName} = ${serializedProps}`, + __html: `window.${ClientWindowPagePropsName} = ${serializedProps};\nwindow.process = ${JSON.stringify(client_process)}`, }, "data-bunext-head": true }), RootHead ? (_jsx(RootHead, { serverRes: pageProps, ctx: routeParams })) : null, Head ? _jsx(Head, { serverRes: pageProps, ctx: routeParams }) : null, bundledMap?.path ? (_jsxs(_Fragment, { children: [_jsx("script", { type: "importmap", dangerouslySetInnerHTML: { __html: JSON.stringify(global.REACT_IMPORTS_MAP), }, defer: true, "data-bunext-head": true }), _jsx("script", { src: `/${bundledMap.path}`, type: "module", id: AppData["BunextClientHydrationScriptID"], defer: true, "data-bunext-head": true })] })) : null, is_dev ? (_jsx("script", { defer: true, dangerouslySetInnerHTML: { diff --git a/dist/functions/server/web-pages/handle-web-pages.js b/dist/functions/server/web-pages/handle-web-pages.js index 332b439..eea926a 100644 --- a/dist/functions/server/web-pages/handle-web-pages.js +++ b/dist/functions/server/web-pages/handle-web-pages.js @@ -6,7 +6,8 @@ import grabPageComponent from "./grab-page-component"; import grabPageErrorComponent from "./grab-page-error-component"; export default async function handleWebPages({ req, }) { try { - if (!isDevelopment()) { + const is_dev = isDevelopment(); + if (!is_dev) { const url = new URL(req.url); const key = url.pathname + (url.search || ""); const existing_cache = getCache({ key, paradigm: "html" }); diff --git a/package.json b/package.json index f37517d..337cb9f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bunext", - "version": "1.0.77", + "version": "1.0.78", "main": "dist/index.js", "module": "index.ts", "dependencies": { diff --git a/src/functions/bundler/pages-ssr-bundler.ts b/src/functions/bundler/pages-ssr-bundler.ts new file mode 100644 index 0000000..7078707 --- /dev/null +++ b/src/functions/bundler/pages-ssr-bundler.ts @@ -0,0 +1,80 @@ +import * as esbuild from "esbuild"; +import grabAllPages from "../../utils/grab-all-pages"; +import grabDirNames from "../../utils/grab-dir-names"; +import isDevelopment from "../../utils/is-development"; +import tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin"; +import type { PageFiles } from "../../types"; +import grabPageReactComponentString from "../server/web-pages/grab-page-react-component-string"; +import grabRootFilePath from "../server/web-pages/grab-root-file-path"; +import ssrVirtualFilesPlugin from "./plugins/ssr-virtual-files-plugin"; +import ssrCTXArtifactTracker from "./plugins/ssr-ctx-artifact-tracker"; + +const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames(); + +type Params = { + post_build_fn?: (params: { artifacts: any[] }) => Promise | void; +}; + +export default async function pagesSSRBundler(params?: Params) { + const pages = grabAllPages(); + const dev = isDevelopment(); + + const entryToPage = new Map(); + const { root_file_path } = grabRootFilePath(); + + for (const page of pages) { + if (page.local_path.match(/\/pages\/api\//)) { + const ts = await Bun.file(page.local_path).text(); + entryToPage.set(page.local_path, { ...page, tsx: ts }); + continue; + } + + const tsx = grabPageReactComponentString({ + file_path: page.local_path, + root_file_path, + }); + + if (!tsx) continue; + + entryToPage.set(page.local_path, { ...page, tsx }); + } + + const entryPoints = [...entryToPage.keys()].map((e) => `ssr-virtual:${e}`); + + await esbuild.build({ + entryPoints, + outdir: BUNX_CWD_MODULE_CACHE_DIR, + bundle: true, + minify: !dev, + format: "esm", + target: "esnext", + platform: "node", + define: { + "process.env.NODE_ENV": JSON.stringify( + dev ? "development" : "production", + ), + }, + entryNames: "[dir]/[hash]", + metafile: true, + plugins: [ + tailwindEsbuildPlugin, + ssrVirtualFilesPlugin({ + entryToPage, + }), + ssrCTXArtifactTracker({ + entryToPage, + post_build_fn: params?.post_build_fn, + }), + ], + jsx: "automatic", + external: [ + "react", + "react-dom", + "react/jsx-runtime", + "react/jsx-dev-runtime", + "bun:*", + ], + splitting: true, + // logLevel: "silent", + }); +} diff --git a/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts b/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts index 97df0dd..f39a1ff 100644 --- a/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts +++ b/src/functions/bundler/plugins/esbuild-ctx-artifact-tracker.ts @@ -2,10 +2,9 @@ import { type Plugin } from "esbuild"; import type { PageFiles } from "../../../types"; import { log } from "../../../utils/log"; import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result"; -import pagesSSRContextBundler from "../pages-ssr-context-bundler"; import buildOnstartErrorHandler from "../build-on-start-error-handler"; -import apiRoutesContextBundler from "../api-routes-context-bundler"; import _ from "lodash"; +import pagesSSRBundler from "../pages-ssr-bundler"; let build_start = 0; let build_starts = 0; @@ -70,11 +69,13 @@ export default function esbuildCTXArtifactTracker({ build_starts = 0; - if (global.SSR_BUNDLER_CTX) { - global.SSR_BUNDLER_CTX.rebuild(); - } else { - pagesSSRContextBundler(); - } + pagesSSRBundler(); + + // if (global.SSR_BUNDLER_CTX) { + // global.SSR_BUNDLER_CTX.rebuild(); + // } else { + // pagesSSRContextBundler(); + // } }); }, }; diff --git a/src/functions/bunext-init.ts b/src/functions/bunext-init.ts index d906c65..bbc846f 100644 --- a/src/functions/bunext-init.ts +++ b/src/functions/bunext-init.ts @@ -100,3 +100,10 @@ export default async function bunextInit() { cron(); } } + +process.on("exit", (code) => { + Bun.spawn([process.execPath, ...process.argv.slice(1)], { + stdio: ["inherit", "inherit", "inherit"], + env: process.env, + }); +}); diff --git a/src/functions/server/web-pages/generate-web-html.tsx b/src/functions/server/web-pages/generate-web-html.tsx index e77939a..9cf45dc 100644 --- a/src/functions/server/web-pages/generate-web-html.tsx +++ b/src/functions/server/web-pages/generate-web-html.tsx @@ -70,6 +70,14 @@ export default async function genWebHTML({ const final_meta = _.merge(root_meta, page_meta); + // const public_envs = Object.keys(process.env).filter((e) => + // e.startsWith(`NEXT_PUBLIC_`), + // ); + + const client_process = { + env: {}, + }; + let final_component = ( @@ -92,7 +100,7 @@ export default async function genWebHTML({