Component Bundler Bugfix

This commit is contained in:
Benjamin Toby 2026-04-04 20:30:40 +01:00
parent 1037bbb546
commit 5166ba037f
8 changed files with 118 additions and 91 deletions

View File

@ -12,7 +12,7 @@ export default async function grabPageBundledReactComponent({ file_path, root_fi
if (!tsx) { if (!tsx) {
return undefined; return undefined;
} }
const mod = await grabTsxStringModule({ tsx, file_path }); const mod = await grabTsxStringModule({ tsx });
const Main = mod.default; const Main = mod.default;
const component = _jsx(Main, {}); const component = _jsx(Main, {});
return { return {

View File

@ -1,12 +1,11 @@
import EJSON from "../../../utils/ejson"; import EJSON from "../../../utils/ejson";
import isDevelopment from "../../../utils/is-development";
import { log } from "../../../utils/log"; import { log } from "../../../utils/log";
import pagePathTransform from "../../../utils/page-path-transform";
export default function grabPageReactComponentString({ file_path, root_file_path, server_res, }) { export default function grabPageReactComponentString({ file_path, root_file_path, server_res, }) {
const now = Date.now();
const dev = isDevelopment();
try { try {
// const target_path = pagePathTransform({ page_path: file_path }); const import_suffix = dev ? `?t=${now}` : "";
// const target_root_path = root_file_path
// ? pagePathTransform({ page_path: root_file_path })
// : undefined;
let tsx = ``; let tsx = ``;
const server_res_json = JSON.stringify(EJSON.stringify(server_res || {}) ?? "{}"); const server_res_json = JSON.stringify(EJSON.stringify(server_res || {}) ?? "{}");
// Import Root from its original source path so that all sub-components // Import Root from its original source path so that all sub-components
@ -15,9 +14,9 @@ export default function grabPageReactComponentString({ file_path, root_file_path
// createContext() call, breaking context for any sub-component that // createContext() call, breaking context for any sub-component that
// imports AppContext via a relative path to the source __root. // imports AppContext via a relative path to the source __root.
if (root_file_path) { if (root_file_path) {
tsx += `import Root from "${root_file_path}"\n`; tsx += `import Root from "${root_file_path}${import_suffix}"\n`;
} }
tsx += `import Page from "${file_path}"\n`; tsx += `import Page from "${file_path}${import_suffix}"\n`;
tsx += `export default function Main() {\n\n`; tsx += `export default function Main() {\n\n`;
tsx += `const props = JSON.parse(${server_res_json})\n\n`; tsx += `const props = JSON.parse(${server_res_json})\n\n`;
tsx += ` return (\n`; tsx += ` return (\n`;

View File

@ -1,6 +1,5 @@
type Params = { type Params = {
tsx: string; tsx: string;
file_path: string;
}; };
export default function grabTsxStringModule<T extends any = any>({ tsx, file_path, }: Params): Promise<T>; export default function grabTsxStringModule<T extends any = any>({ tsx, }: Params): Promise<T>;
export {}; export {};

View File

@ -1,38 +1,55 @@
import isDevelopment from "../../../utils/is-development"; import isDevelopment from "../../../utils/is-development";
import tailwindcss from "bun-plugin-tailwind"; import { transform } from "esbuild";
import grabDirNames from "../../../utils/grab-dir-names"; export default async function grabTsxStringModule({ tsx, }) {
import path from "path";
import BunSkipNonBrowserPlugin from "../../bundler/plugins/bun-skip-browser-plugin";
export default async function grabTsxStringModule({ tsx, file_path, }) {
const dev = isDevelopment(); const dev = isDevelopment();
const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames(); const now = Date.now();
const trimmed_file_path = file_path const final_tsx = dev ? tsx + `\n// v_${now}` : tsx;
.replace(/.*\/src\/pages\//, "") const result = await transform(final_tsx, {
.replace(/\.tsx$/, ""); loader: "tsx",
const src_file_path = path.join(BUNX_CWD_MODULE_CACHE_DIR, `${trimmed_file_path}.tsx`);
const out_file_path = path.join(BUNX_CWD_MODULE_CACHE_DIR, `${trimmed_file_path}.js`);
await Bun.write(src_file_path, tsx);
const build = await Bun.build({
entrypoints: [src_file_path],
format: "esm", format: "esm",
target: "bun", jsx: "automatic",
external: ["react", "react-dom"], minify: !dev,
minify: true,
define: {
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
},
metafile: true,
plugins: [tailwindcss, BunSkipNonBrowserPlugin],
jsx: {
runtime: "automatic",
development: dev,
},
outdir: BUNX_CWD_MODULE_CACHE_DIR,
}); });
Loader.registry.delete(out_file_path); const blob = new Blob([result.code], { type: "text/javascript" });
const module = await import(`${out_file_path}?t=${Date.now()}`); const url = URL.createObjectURL(blob);
return module; const mod = await import(url);
URL.revokeObjectURL(url);
return mod;
} }
// const trimmed_file_path = file_path
// .replace(/.*\/src\/pages\//, "")
// .replace(/\.tsx$/, "");
// const src_file_path = path.join(
// BUNX_CWD_MODULE_CACHE_DIR,
// `${trimmed_file_path}.tsx`,
// );
// const out_file_path = path.join(
// BUNX_CWD_MODULE_CACHE_DIR,
// `${trimmed_file_path}.js`,
// );
// await Bun.write(src_file_path, tsx);
// const build = await Bun.build({
// entrypoints: [src_file_path],
// format: "esm",
// target: "bun",
// // external: ["react", "react-dom"],
// minify: true,
// define: {
// "process.env.NODE_ENV": JSON.stringify(
// dev ? "development" : "production",
// ),
// },
// metafile: true,
// plugins: [tailwindcss, BunSkipNonBrowserPlugin],
// jsx: {
// runtime: "automatic",
// development: dev,
// },
// outdir: BUNX_CWD_MODULE_CACHE_DIR,
// });
// Loader.registry.delete(out_file_path);
// const module = await import(`${out_file_path}?t=${Date.now()}`);
// return module as T;
// await esbuild.build({ // await esbuild.build({
// stdin: { // stdin: {
// contents: tsx, // contents: tsx,

View File

@ -2,7 +2,7 @@
"name": "@moduletrace/bunext", "name": "@moduletrace/bunext",
"module": "index.ts", "module": "index.ts",
"type": "module", "type": "module",
"version": "1.0.47", "version": "1.0.48",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"exports": { "exports": {

View File

@ -25,7 +25,7 @@ export default async function grabPageBundledReactComponent({
return undefined; return undefined;
} }
const mod = await grabTsxStringModule({ tsx, file_path }); const mod = await grabTsxStringModule({ tsx });
const Main = mod.default; const Main = mod.default;
const component = <Main />; const component = <Main />;

View File

@ -1,6 +1,6 @@
import EJSON from "../../../utils/ejson"; import EJSON from "../../../utils/ejson";
import isDevelopment from "../../../utils/is-development";
import { log } from "../../../utils/log"; import { log } from "../../../utils/log";
import pagePathTransform from "../../../utils/page-path-transform";
type Params = { type Params = {
file_path: string; file_path: string;
@ -13,11 +13,11 @@ export default function grabPageReactComponentString({
root_file_path, root_file_path,
server_res, server_res,
}: Params): string | undefined { }: Params): string | undefined {
const now = Date.now();
const dev = isDevelopment();
try { try {
// const target_path = pagePathTransform({ page_path: file_path }); const import_suffix = dev ? `?t=${now}` : "";
// const target_root_path = root_file_path
// ? pagePathTransform({ page_path: root_file_path })
// : undefined;
let tsx = ``; let tsx = ``;
@ -31,10 +31,10 @@ export default function grabPageReactComponentString({
// createContext() call, breaking context for any sub-component that // createContext() call, breaking context for any sub-component that
// imports AppContext via a relative path to the source __root. // imports AppContext via a relative path to the source __root.
if (root_file_path) { if (root_file_path) {
tsx += `import Root from "${root_file_path}"\n`; tsx += `import Root from "${root_file_path}${import_suffix}"\n`;
} }
tsx += `import Page from "${file_path}"\n`; tsx += `import Page from "${file_path}${import_suffix}"\n`;
tsx += `export default function Main() {\n\n`; tsx += `export default function Main() {\n\n`;
tsx += `const props = JSON.parse(${server_res_json})\n\n`; tsx += `const props = JSON.parse(${server_res_json})\n\n`;
tsx += ` return (\n`; tsx += ` return (\n`;

View File

@ -1,63 +1,75 @@
import isDevelopment from "../../../utils/is-development"; import isDevelopment from "../../../utils/is-development";
import tailwindcss from "bun-plugin-tailwind"; import { transform } from "esbuild";
import grabDirNames from "../../../utils/grab-dir-names";
import path from "path";
import BunSkipNonBrowserPlugin from "../../bundler/plugins/bun-skip-browser-plugin";
type Params = { type Params = {
tsx: string; tsx: string;
file_path: string;
}; };
export default async function grabTsxStringModule<T extends any = any>({ export default async function grabTsxStringModule<T extends any = any>({
tsx, tsx,
file_path,
}: Params): Promise<T> { }: Params): Promise<T> {
const dev = isDevelopment(); const dev = isDevelopment();
const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames(); const now = Date.now();
const trimmed_file_path = file_path const final_tsx = dev ? tsx + `\n// v_${now}` : tsx;
.replace(/.*\/src\/pages\//, "")
.replace(/\.tsx$/, "");
const src_file_path = path.join( const result = await transform(final_tsx, {
BUNX_CWD_MODULE_CACHE_DIR, loader: "tsx",
`${trimmed_file_path}.tsx`,
);
const out_file_path = path.join(
BUNX_CWD_MODULE_CACHE_DIR,
`${trimmed_file_path}.js`,
);
await Bun.write(src_file_path, tsx);
const build = await Bun.build({
entrypoints: [src_file_path],
format: "esm", format: "esm",
target: "bun", jsx: "automatic",
external: ["react", "react-dom"], minify: !dev,
minify: true,
define: {
"process.env.NODE_ENV": JSON.stringify(
dev ? "development" : "production",
),
},
metafile: true,
plugins: [tailwindcss, BunSkipNonBrowserPlugin],
jsx: {
runtime: "automatic",
development: dev,
},
outdir: BUNX_CWD_MODULE_CACHE_DIR,
}); });
Loader.registry.delete(out_file_path); const blob = new Blob([result.code], { type: "text/javascript" });
const module = await import(`${out_file_path}?t=${Date.now()}`); const url = URL.createObjectURL(blob);
const mod = await import(url);
return module as T; URL.revokeObjectURL(url);
return mod as T;
} }
// const trimmed_file_path = file_path
// .replace(/.*\/src\/pages\//, "")
// .replace(/\.tsx$/, "");
// const src_file_path = path.join(
// BUNX_CWD_MODULE_CACHE_DIR,
// `${trimmed_file_path}.tsx`,
// );
// const out_file_path = path.join(
// BUNX_CWD_MODULE_CACHE_DIR,
// `${trimmed_file_path}.js`,
// );
// await Bun.write(src_file_path, tsx);
// const build = await Bun.build({
// entrypoints: [src_file_path],
// format: "esm",
// target: "bun",
// // external: ["react", "react-dom"],
// minify: true,
// define: {
// "process.env.NODE_ENV": JSON.stringify(
// dev ? "development" : "production",
// ),
// },
// metafile: true,
// plugins: [tailwindcss, BunSkipNonBrowserPlugin],
// jsx: {
// runtime: "automatic",
// development: dev,
// },
// outdir: BUNX_CWD_MODULE_CACHE_DIR,
// });
// Loader.registry.delete(out_file_path);
// const module = await import(`${out_file_path}?t=${Date.now()}`);
// return module as T;
// await esbuild.build({ // await esbuild.build({
// stdin: { // stdin: {
// contents: tsx, // contents: tsx,