Switch back to esbuild

This commit is contained in:
Benjamin Toby 2026-03-24 18:28:56 +01:00
parent 976ff5fec9
commit 47c262392c
25 changed files with 315 additions and 153 deletions

View File

@ -6,6 +6,7 @@ import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler";
import grabDirNames from "../../utils/grab-dir-names"; import grabDirNames from "../../utils/grab-dir-names";
import { rmSync } from "fs"; import { rmSync } from "fs";
import allPagesBundler from "../../functions/bundler/all-pages-bundler"; import allPagesBundler from "../../functions/bundler/all-pages-bundler";
import allPagesESBuildContextBundler from "../../functions/bundler/all-pages-esbuild-context-bundler";
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames(); const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
export default function () { export default function () {
return new Command("build") return new Command("build")
@ -21,7 +22,9 @@ export default function () {
await init(); await init();
log.banner(); log.banner();
log.build("Building Project ..."); log.build("Building Project ...");
await allPagesBunBundler(); // await allPagesBunBundler();
// await allPagesBundler(); // await allPagesBundler();
await allPagesESBuildContextBundler();
process.exit();
}); });
} }

View File

@ -5,6 +5,8 @@ import bunextInit from "../../functions/bunext-init";
import grabDirNames from "../../utils/grab-dir-names"; import grabDirNames from "../../utils/grab-dir-names";
import { rmSync } from "fs"; import { rmSync } from "fs";
import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler"; import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler";
import allPagesESBuildContextBundler from "../../functions/bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "../../functions/server/server-post-build-fn";
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames(); const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
export default function () { export default function () {
return new Command("dev") return new Command("dev")
@ -18,7 +20,8 @@ export default function () {
} }
catch (error) { } catch (error) { }
await bunextInit(); await bunextInit();
await allPagesBunBundler(); // await allPagesBunBundler();
await allPagesESBuildContextBundler();
await startServer(); await startServer();
}); });
} }

View File

@ -3,13 +3,16 @@ import startServer from "../../functions/server/start-server";
import { log } from "../../utils/log"; import { log } from "../../utils/log";
import bunextInit from "../../functions/bunext-init"; import bunextInit from "../../functions/bunext-init";
import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler"; import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler";
import allPagesESBuildContextBundler from "../../functions/bundler/all-pages-esbuild-context-bundler";
export default function () { export default function () {
return new Command("start") return new Command("start")
.description("Start production server") .description("Start production server")
.action(async () => { .action(async () => {
process.env.NODE_ENV = "production";
log.info("Starting production server ..."); log.info("Starting production server ...");
await bunextInit(); await bunextInit();
await allPagesBunBundler(); // await allPagesBunBundler();
await allPagesESBuildContextBundler();
await startServer(); await startServer();
}); });
} }

View File

@ -7,8 +7,7 @@ import path from "path";
import grabClientHydrationScript from "./grab-client-hydration-script"; import grabClientHydrationScript from "./grab-client-hydration-script";
import { mkdirSync, rmSync } from "fs"; import { mkdirSync, rmSync } from "fs";
import recordArtifacts from "./record-artifacts"; import recordArtifacts from "./record-artifacts";
import BunSkipNonBrowserPlugin from "./plugins/bun-skip-browser-plugin"; const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames();
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR, BUNX_TMP_DIR } = grabDirNames();
export default async function allPagesBunBundler(params) { export default async function allPagesBunBundler(params) {
const { target = "browser", page_file_paths } = params || {}; const { target = "browser", page_file_paths } = params || {};
const pages = grabAllPages({ exclude_api: true }); const pages = grabAllPages({ exclude_api: true });
@ -64,7 +63,6 @@ export default async function allPagesBunBundler(params) {
"react/jsx-runtime", "react/jsx-runtime",
], ],
}); });
await Bun.write(path.join(BUNX_TMP_DIR, "bundle.json"), JSON.stringify(result, null, 4), { createPath: true });
if (!result.success) { if (!result.success) {
for (const entry of result.logs) { for (const entry of result.logs) {
log.error(`[Build] ${entry.message}`); log.error(`[Build] ${entry.message}`);

View File

@ -129,13 +129,12 @@ export default async function allPagesBundler(params) {
} }
return; return;
} }
const artifacts = grabArtifactsFromBundledResults({ // const artifacts = grabArtifactsFromBundledResults({
pages: target_pages, // result,
result, // });
}); // if (artifacts?.[0]) {
if (artifacts?.[0]) { // await recordArtifacts({ artifacts });
await recordArtifacts({ artifacts }); // }
}
const elapsed = (performance.now() - buildStart).toFixed(0); const elapsed = (performance.now() - buildStart).toFixed(0);
log.success(`[Built] in ${elapsed}ms`); log.success(`[Built] in ${elapsed}ms`);
global.RECOMPILING = false; global.RECOMPILING = false;

View File

@ -1,7 +1,3 @@
type Params = { type Params = {};
post_build_fn?: (params: {
artifacts: any[];
}) => Promise<void> | void;
};
export default function allPagesESBuildContextBundler(params?: Params): Promise<void>; export default function allPagesESBuildContextBundler(params?: Params): Promise<void>;
export {}; export {};

View File

@ -7,40 +7,25 @@ import tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin";
import grabClientHydrationScript from "./grab-client-hydration-script"; import grabClientHydrationScript from "./grab-client-hydration-script";
import grabArtifactsFromBundledResults from "./grab-artifacts-from-bundled-result"; import grabArtifactsFromBundledResults from "./grab-artifacts-from-bundled-result";
import { writeFileSync } from "fs"; import { writeFileSync } from "fs";
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames(); import path from "path";
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE, BUNX_HYDRATION_SRC_DIR, } = grabDirNames();
let build_starts = 0; let build_starts = 0;
const MAX_BUILD_STARTS = 10; const MAX_BUILD_STARTS = 10;
export default async function allPagesESBuildContextBundler(params) { export default async function allPagesESBuildContextBundler(params) {
const pages = grabAllPages({ exclude_api: true }); const pages = grabAllPages({ exclude_api: true });
global.PAGE_FILES = pages; global.PAGE_FILES = pages;
const virtualEntries = {};
const dev = isDevelopment(); const dev = isDevelopment();
const entryToPage = new Map();
for (const page of pages) { for (const page of pages) {
const key = page.transformed_path;
const txt = await grabClientHydrationScript({ const txt = await grabClientHydrationScript({
page_local_path: page.local_path, page_local_path: page.local_path,
}); });
// if (page.url_path == "/index") {
// console.log("txt", txt);
// }
if (!txt) if (!txt)
continue; continue;
virtualEntries[key] = txt; const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`);
await Bun.write(entryFile, txt, { createPath: true });
entryToPage.set(path.resolve(entryFile), page);
} }
const virtualPlugin = {
name: "virtual-entrypoints",
setup(build) {
build.onResolve({ filter: /^virtual:/ }, (args) => ({
path: args.path.replace("virtual:", ""),
namespace: "virtual",
}));
build.onLoad({ filter: /.*/, namespace: "virtual" }, (args) => ({
contents: virtualEntries[args.path],
loader: "tsx",
resolveDir: process.cwd(),
}));
},
};
let buildStart = 0; let buildStart = 0;
const artifactTracker = { const artifactTracker = {
name: "artifact-tracker", name: "artifact-tracker",
@ -66,8 +51,8 @@ export default async function allPagesESBuildContextBundler(params) {
return; return;
} }
const artifacts = grabArtifactsFromBundledResults({ const artifacts = grabArtifactsFromBundledResults({
pages,
result, result,
entryToPage,
}); });
if (artifacts?.[0] && artifacts.length > 0) { if (artifacts?.[0] && artifacts.length > 0) {
for (let i = 0; i < artifacts.length; i++) { for (let i = 0; i < artifacts.length; i++) {
@ -77,8 +62,11 @@ export default async function allPagesESBuildContextBundler(params) {
artifact; artifact;
} }
} }
params?.post_build_fn?.({ artifacts }); // params?.post_build_fn?.({ artifacts });
writeFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, JSON.stringify(artifacts, null, 4)); // writeFileSync(
// HYDRATION_DST_DIR_MAP_JSON_FILE,
// JSON.stringify(artifacts, null, 4),
// );
} }
const elapsed = (performance.now() - buildStart).toFixed(0); const elapsed = (performance.now() - buildStart).toFixed(0);
log.success(`[Built] in ${elapsed}ms`); log.success(`[Built] in ${elapsed}ms`);
@ -87,12 +75,12 @@ export default async function allPagesESBuildContextBundler(params) {
}); });
}, },
}; };
const entryPoints = Object.keys(virtualEntries).map((k) => `virtual:${k}`); const entryPoints = [...entryToPage.keys()];
const ctx = await esbuild.context({ const ctx = await esbuild.context({
entryPoints, entryPoints,
outdir: HYDRATION_DST_DIR, outdir: HYDRATION_DST_DIR,
bundle: true, bundle: true,
minify: true, minify: !dev,
format: "esm", format: "esm",
target: "es2020", target: "es2020",
platform: "browser", platform: "browser",
@ -101,7 +89,7 @@ export default async function allPagesESBuildContextBundler(params) {
}, },
entryNames: "[dir]/[hash]", entryNames: "[dir]/[hash]",
metafile: true, metafile: true,
plugins: [tailwindEsbuildPlugin, virtualPlugin, artifactTracker], plugins: [tailwindEsbuildPlugin, artifactTracker],
jsx: "automatic", jsx: "automatic",
splitting: true, splitting: true,
// logLevel: "silent", // logLevel: "silent",
@ -113,5 +101,8 @@ export default async function allPagesESBuildContextBundler(params) {
], ],
}); });
await ctx.rebuild(); await ctx.rebuild();
// global.BUNDLER_CTX = ctx; // if (params?.watch) {
// await ctx.watch();
// }
global.BUNDLER_CTX = ctx;
} }

View File

@ -2,7 +2,7 @@ import * as esbuild from "esbuild";
import type { BundlerCTXMap, PageFiles } from "../../types"; import type { BundlerCTXMap, PageFiles } from "../../types";
type Params = { type Params = {
result: esbuild.BuildResult<esbuild.BuildOptions>; result: esbuild.BuildResult<esbuild.BuildOptions>;
pages: PageFiles[]; entryToPage: Map<string, PageFiles>;
}; };
export default function grabArtifactsFromBundledResults({ result, pages, }: Params): BundlerCTXMap[] | undefined; export default function grabArtifactsFromBundledResults({ result, entryToPage, }: Params): BundlerCTXMap[] | undefined;
export {}; export {};

View File

@ -1,27 +1,27 @@
import path from "path"; import path from "path";
import * as esbuild from "esbuild"; import * as esbuild from "esbuild";
export default function grabArtifactsFromBundledResults({ result, pages, }) { import grabDirNames from "../../utils/grab-dir-names";
const { ROOT_DIR } = grabDirNames();
export default function grabArtifactsFromBundledResults({ result, entryToPage, }) {
if (result.errors.length > 0) if (result.errors.length > 0)
return; return;
const artifacts = Object.entries(result.metafile.outputs) const artifacts = Object.entries(result.metafile.outputs)
.filter(([, meta]) => meta.entryPoint) .filter(([, meta]) => meta.entryPoint)
.map(([outputPath, meta]) => { .map(([outputPath, meta]) => {
const target_page = pages.find((p) => { const entrypoint = path.join(ROOT_DIR, meta.entryPoint || "");
return meta.entryPoint === `virtual:${p.transformed_path}`; const target_page = entryToPage.get(entrypoint);
});
if (!target_page || !meta.entryPoint) { if (!target_page || !meta.entryPoint) {
return undefined; return undefined;
} }
const { file_name, local_path, url_path, transformed_path } = target_page; const { file_name, local_path, url_path, transformed_path } = target_page;
const cssPath = meta.cssBundle || undefined;
return { return {
path: outputPath, path: outputPath,
hash: path.basename(outputPath, path.extname(outputPath)), hash: path.basename(outputPath, path.extname(outputPath)),
type: outputPath.endsWith(".css") type: outputPath.endsWith(".css")
? "text/css" ? "text/css"
: "text/javascript", : "text/javascript",
entrypoint: meta.entryPoint, entrypoint,
css_path: cssPath, css_path: meta.cssBundle,
file_name, file_name,
local_path, local_path,
url_path, url_path,

View File

@ -1,12 +1,11 @@
import { type Ora } from "ora";
import type { BundlerCTXMap, BunextConfig, GlobalHMRControllerObject, PageFiles } from "../types"; import type { BundlerCTXMap, BunextConfig, GlobalHMRControllerObject, PageFiles } from "../types";
import type { FileSystemRouter, Server } from "bun"; import type { FileSystemRouter, Server } from "bun";
import { type FSWatcher } from "fs"; import { type FSWatcher } from "fs";
import type { BuildContext } from "esbuild";
/** /**
* # Declare Global Variables * # Declare Global Variables
*/ */
declare global { declare global {
var ORA_SPINNER: Ora;
var CONFIG: BunextConfig; var CONFIG: BunextConfig;
var SERVER: Server<any> | undefined; var SERVER: Server<any> | undefined;
var RECOMPILING: boolean; var RECOMPILING: boolean;
@ -23,5 +22,6 @@ declare global {
var PAGE_FILES: PageFiles[]; var PAGE_FILES: PageFiles[];
var ROOT_FILE_UPDATED: boolean; var ROOT_FILE_UPDATED: boolean;
var SKIPPED_BROWSER_MODULES: Set<string>; var SKIPPED_BROWSER_MODULES: Set<string>;
var BUNDLER_CTX: BuildContext | undefined;
} }
export default function bunextInit(): Promise<void>; export default function bunextInit(): Promise<void>;

View File

@ -1,16 +1,12 @@
import ora, {} from "ora";
import grabDirNames from "../utils/grab-dir-names"; import grabDirNames from "../utils/grab-dir-names";
import {} from "fs"; import {} from "fs";
import init from "./init"; import init from "./init";
import isDevelopment from "../utils/is-development"; import isDevelopment from "../utils/is-development";
import watcher from "./server/watcher";
import { log } from "../utils/log"; import { log } from "../utils/log";
import cron from "./server/cron"; import cron from "./server/cron";
import allPagesBunBundler from "./bundler/all-pages-bun-bundler"; import watcherEsbuildCTX from "./server/watcher-esbuild-ctx";
const { PAGES_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames(); const { PAGES_DIR } = grabDirNames();
export default async function bunextInit() { export default async function bunextInit() {
global.ORA_SPINNER = ora();
global.ORA_SPINNER.clear();
global.HMR_CONTROLLERS = []; global.HMR_CONTROLLERS = [];
global.BUNDLER_CTX_MAP = {}; global.BUNDLER_CTX_MAP = {};
global.BUNDLER_REBUILDS = 0; global.BUNDLER_REBUILDS = 0;
@ -25,7 +21,7 @@ export default async function bunextInit() {
global.ROUTER = router; global.ROUTER = router;
const is_dev = isDevelopment(); const is_dev = isDevelopment();
if (is_dev) { if (is_dev) {
watcher(); watcherEsbuildCTX();
} }
else { else {
cron(); cron();

View File

@ -0,0 +1 @@
export default function watcherEsbuildCTX(): Promise<void>;

View File

@ -0,0 +1,82 @@
import { watch, existsSync } from "fs";
import path from "path";
import grabDirNames from "../../utils/grab-dir-names";
import { log } from "../../utils/log";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
const { ROOT_DIR } = grabDirNames();
export default async function watcherEsbuildCTX() {
const pages_src_watcher = watch(ROOT_DIR, {
recursive: true,
persistent: true,
}, async (event, filename) => {
if (!filename)
return;
const full_file_path = path.join(ROOT_DIR, filename);
if (full_file_path.match(/\/styles$/)) {
global.RECOMPILING = true;
await Bun.sleep(1000);
await fullRebuild({
msg: `Detected new \`styles\` directory. Rebuilding ...`,
});
return;
}
const excluded_match = /node_modules\/|^public\/|^\.bunext\/|^\.git\/|^dist\/|bun\.lockb$/;
if (filename.match(excluded_match))
return;
if (filename.match(/bunext.config\.ts/)) {
await fullRebuild({
msg: `bunext.config.ts file changed. Rebuilding server ...`,
});
return;
}
const target_files_match = /\.(tsx?|jsx?|css)$/;
if (event !== "rename") {
if (filename.match(target_files_match)) {
if (global.RECOMPILING)
return;
global.RECOMPILING = true;
await global.BUNDLER_CTX?.rebuild();
}
return;
}
const is_file_of_interest = Boolean(filename.match(target_files_match));
if (!is_file_of_interest) {
return;
}
if (!filename.match(/^src\/pages\/|\.css$/))
return;
if (filename.match(/\/(--|\()/))
return;
if (global.RECOMPILING)
return;
const action = existsSync(full_file_path) ? "created" : "deleted";
const type = filename.match(/\.css$/) ? "Sylesheet" : "Page";
await fullRebuild({
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
});
});
global.PAGES_SRC_WATCHER = pages_src_watcher;
}
async function fullRebuild(params) {
try {
const { msg } = params || {};
global.RECOMPILING = true;
if (msg) {
log.watch(msg);
}
global.ROUTER.reload();
await global.BUNDLER_CTX?.dispose();
global.BUNDLER_CTX = undefined;
await allPagesESBuildContextBundler();
}
catch (error) {
log.error(error);
}
finally {
global.RECOMPILING = false;
}
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
watcherEsbuildCTX();
}
}

View File

@ -3,7 +3,6 @@ import path from "path";
import grabDirNames from "../../utils/grab-dir-names"; import grabDirNames from "../../utils/grab-dir-names";
import rebuildBundler from "./rebuild-bundler"; import rebuildBundler from "./rebuild-bundler";
import { log } from "../../utils/log"; import { log } from "../../utils/log";
// import rewritePagesModule from "../../utils/rewrite-pages-module";
const { ROOT_DIR } = grabDirNames(); const { ROOT_DIR } = grabDirNames();
export default async function watcher() { export default async function watcher() {
const pages_src_watcher = watch(ROOT_DIR, { const pages_src_watcher = watch(ROOT_DIR, {
@ -62,15 +61,11 @@ async function fullRebuild(params) {
try { try {
const { msg } = params || {}; const { msg } = params || {};
global.RECOMPILING = true; global.RECOMPILING = true;
// const target_file_paths = global.HMR_CONTROLLERS.map( const target_file_paths = global.HMR_CONTROLLERS.map((hmr) => hmr.target_map?.local_path).filter((f) => typeof f == "string");
// (hmr) => hmr.target_map?.local_path,
// ).filter((f) => typeof f == "string");
// await rewritePagesModule();
if (msg) { if (msg) {
log.watch(msg); log.watch(msg);
} }
await rebuildBundler(); await rebuildBundler({ target_file_paths });
// await rebuildBundler({ target_file_paths });
} }
catch (error) { catch (error) {
log.error(error); log.error(error);

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.30", "version": "1.0.31",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"exports": { "exports": {

View File

@ -6,6 +6,7 @@ import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler";
import grabDirNames from "../../utils/grab-dir-names"; import grabDirNames from "../../utils/grab-dir-names";
import { rmSync } from "fs"; import { rmSync } from "fs";
import allPagesBundler from "../../functions/bundler/all-pages-bundler"; import allPagesBundler from "../../functions/bundler/all-pages-bundler";
import allPagesESBuildContextBundler from "../../functions/bundler/all-pages-esbuild-context-bundler";
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames(); const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
@ -26,8 +27,11 @@ export default function () {
log.banner(); log.banner();
log.build("Building Project ..."); log.build("Building Project ...");
await allPagesBunBundler(); // await allPagesBunBundler();
// await allPagesBundler(); // await allPagesBundler();
await allPagesESBuildContextBundler();
process.exit();
}); });
} }

View File

@ -5,6 +5,8 @@ import bunextInit from "../../functions/bunext-init";
import grabDirNames from "../../utils/grab-dir-names"; import grabDirNames from "../../utils/grab-dir-names";
import { rmSync } from "fs"; import { rmSync } from "fs";
import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler"; import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler";
import allPagesESBuildContextBundler from "../../functions/bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "../../functions/server/server-post-build-fn";
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames(); const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
@ -22,7 +24,10 @@ export default function () {
} catch (error) {} } catch (error) {}
await bunextInit(); await bunextInit();
await allPagesBunBundler();
// await allPagesBunBundler();
await allPagesESBuildContextBundler();
await startServer(); await startServer();
}); });

View File

@ -3,16 +3,19 @@ import startServer from "../../functions/server/start-server";
import { log } from "../../utils/log"; import { log } from "../../utils/log";
import bunextInit from "../../functions/bunext-init"; import bunextInit from "../../functions/bunext-init";
import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler"; import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler";
import allPagesESBuildContextBundler from "../../functions/bundler/all-pages-esbuild-context-bundler";
export default function () { export default function () {
return new Command("start") return new Command("start")
.description("Start production server") .description("Start production server")
.action(async () => { .action(async () => {
process.env.NODE_ENV = "production";
log.info("Starting production server ..."); log.info("Starting production server ...");
await bunextInit(); await bunextInit();
await allPagesBunBundler(); // await allPagesBunBundler();
await allPagesESBuildContextBundler();
await startServer(); await startServer();
}); });

View File

@ -8,10 +8,8 @@ import path from "path";
import grabClientHydrationScript from "./grab-client-hydration-script"; import grabClientHydrationScript from "./grab-client-hydration-script";
import { mkdirSync, rmSync } from "fs"; import { mkdirSync, rmSync } from "fs";
import recordArtifacts from "./record-artifacts"; import recordArtifacts from "./record-artifacts";
import BunSkipNonBrowserPlugin from "./plugins/bun-skip-browser-plugin";
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR, BUNX_TMP_DIR } = const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames();
grabDirNames();
type Params = { type Params = {
target?: "bun" | "browser"; target?: "bun" | "browser";
@ -86,12 +84,6 @@ export default async function allPagesBunBundler(params?: Params) {
], ],
}); });
await Bun.write(
path.join(BUNX_TMP_DIR, "bundle.json"),
JSON.stringify(result, null, 4),
{ createPath: true },
);
if (!result.success) { if (!result.success) {
for (const entry of result.logs) { for (const entry of result.logs) {
log.error(`[Build] ${entry.message}`); log.error(`[Build] ${entry.message}`);

View File

@ -166,14 +166,13 @@ export default async function allPagesBundler(params?: Params) {
return; return;
} }
const artifacts = grabArtifactsFromBundledResults({ // const artifacts = grabArtifactsFromBundledResults({
pages: target_pages, // result,
result, // });
});
if (artifacts?.[0]) { // if (artifacts?.[0]) {
await recordArtifacts({ artifacts }); // await recordArtifacts({ artifacts });
} // }
const elapsed = (performance.now() - buildStart).toFixed(0); const elapsed = (performance.now() - buildStart).toFixed(0);
log.success(`[Built] in ${elapsed}ms`); log.success(`[Built] in ${elapsed}ms`);

View File

@ -7,14 +7,21 @@ import tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin";
import grabClientHydrationScript from "./grab-client-hydration-script"; import grabClientHydrationScript from "./grab-client-hydration-script";
import grabArtifactsFromBundledResults from "./grab-artifacts-from-bundled-result"; import grabArtifactsFromBundledResults from "./grab-artifacts-from-bundled-result";
import { writeFileSync } from "fs"; import { writeFileSync } from "fs";
import type { PageFiles } from "../../types";
import path from "path";
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames(); const {
HYDRATION_DST_DIR,
HYDRATION_DST_DIR_MAP_JSON_FILE,
BUNX_HYDRATION_SRC_DIR,
} = grabDirNames();
let build_starts = 0; let build_starts = 0;
const MAX_BUILD_STARTS = 10; const MAX_BUILD_STARTS = 10;
type Params = { type Params = {
post_build_fn?: (params: { artifacts: any[] }) => Promise<void> | void; // post_build_fn?: (params: { artifacts: any[] }) => Promise<void> | void;
// watch?: boolean;
}; };
export default async function allPagesESBuildContextBundler(params?: Params) { export default async function allPagesESBuildContextBundler(params?: Params) {
@ -22,41 +29,24 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
global.PAGE_FILES = pages; global.PAGE_FILES = pages;
const virtualEntries: Record<string, string> = {};
const dev = isDevelopment(); const dev = isDevelopment();
for (const page of pages) { const entryToPage = new Map<string, PageFiles>();
const key = page.transformed_path;
for (const page of pages) {
const txt = await grabClientHydrationScript({ const txt = await grabClientHydrationScript({
page_local_path: page.local_path, page_local_path: page.local_path,
}); });
// if (page.url_path == "/index") {
// console.log("txt", txt);
// }
if (!txt) continue; if (!txt) continue;
virtualEntries[key] = txt; const entryFile = path.join(
BUNX_HYDRATION_SRC_DIR,
`${page.url_path}.tsx`,
);
await Bun.write(entryFile, txt, { createPath: true });
entryToPage.set(path.resolve(entryFile), page);
} }
const virtualPlugin: esbuild.Plugin = {
name: "virtual-entrypoints",
setup(build) {
build.onResolve({ filter: /^virtual:/ }, (args) => ({
path: args.path.replace("virtual:", ""),
namespace: "virtual",
}));
build.onLoad({ filter: /.*/, namespace: "virtual" }, (args) => ({
contents: virtualEntries[args.path],
loader: "tsx",
resolveDir: process.cwd(),
}));
},
};
let buildStart = 0; let buildStart = 0;
const artifactTracker: esbuild.Plugin = { const artifactTracker: esbuild.Plugin = {
@ -86,8 +76,8 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
} }
const artifacts = grabArtifactsFromBundledResults({ const artifacts = grabArtifactsFromBundledResults({
pages,
result, result,
entryToPage,
}); });
if (artifacts?.[0] && artifacts.length > 0) { if (artifacts?.[0] && artifacts.length > 0) {
@ -99,12 +89,12 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
} }
} }
params?.post_build_fn?.({ artifacts }); // params?.post_build_fn?.({ artifacts });
writeFileSync( // writeFileSync(
HYDRATION_DST_DIR_MAP_JSON_FILE, // HYDRATION_DST_DIR_MAP_JSON_FILE,
JSON.stringify(artifacts, null, 4), // JSON.stringify(artifacts, null, 4),
); // );
} }
const elapsed = (performance.now() - buildStart).toFixed(0); const elapsed = (performance.now() - buildStart).toFixed(0);
@ -117,13 +107,13 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
}, },
}; };
const entryPoints = Object.keys(virtualEntries).map((k) => `virtual:${k}`); const entryPoints = [...entryToPage.keys()];
const ctx = await esbuild.context({ const ctx = await esbuild.context({
entryPoints, entryPoints,
outdir: HYDRATION_DST_DIR, outdir: HYDRATION_DST_DIR,
bundle: true, bundle: true,
minify: true, minify: !dev,
format: "esm", format: "esm",
target: "es2020", target: "es2020",
platform: "browser", platform: "browser",
@ -134,7 +124,7 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
}, },
entryNames: "[dir]/[hash]", entryNames: "[dir]/[hash]",
metafile: true, metafile: true,
plugins: [tailwindEsbuildPlugin, virtualPlugin, artifactTracker], plugins: [tailwindEsbuildPlugin, artifactTracker],
jsx: "automatic", jsx: "automatic",
splitting: true, splitting: true,
// logLevel: "silent", // logLevel: "silent",
@ -148,5 +138,9 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
await ctx.rebuild(); await ctx.rebuild();
// global.BUNDLER_CTX = ctx; // if (params?.watch) {
// await ctx.watch();
// }
global.BUNDLER_CTX = ctx;
} }

View File

@ -1,15 +1,18 @@
import path from "path"; import path from "path";
import * as esbuild from "esbuild"; import * as esbuild from "esbuild";
import type { BundlerCTXMap, PageFiles } from "../../types"; import type { BundlerCTXMap, PageFiles } from "../../types";
import grabDirNames from "../../utils/grab-dir-names";
const { ROOT_DIR } = grabDirNames();
type Params = { type Params = {
result: esbuild.BuildResult<esbuild.BuildOptions>; result: esbuild.BuildResult<esbuild.BuildOptions>;
pages: PageFiles[]; entryToPage: Map<string, PageFiles>;
}; };
export default function grabArtifactsFromBundledResults({ export default function grabArtifactsFromBundledResults({
result, result,
pages, entryToPage,
}: Params) { }: Params) {
if (result.errors.length > 0) return; if (result.errors.length > 0) return;
@ -18,9 +21,9 @@ export default function grabArtifactsFromBundledResults({
) )
.filter(([, meta]) => meta.entryPoint) .filter(([, meta]) => meta.entryPoint)
.map(([outputPath, meta]) => { .map(([outputPath, meta]) => {
const target_page = pages.find((p) => { const entrypoint = path.join(ROOT_DIR, meta.entryPoint || "");
return meta.entryPoint === `virtual:${p.transformed_path}`;
}); const target_page = entryToPage.get(entrypoint);
if (!target_page || !meta.entryPoint) { if (!target_page || !meta.entryPoint) {
return undefined; return undefined;
@ -29,16 +32,14 @@ export default function grabArtifactsFromBundledResults({
const { file_name, local_path, url_path, transformed_path } = const { file_name, local_path, url_path, transformed_path } =
target_page; target_page;
const cssPath = meta.cssBundle || undefined;
return { return {
path: outputPath, path: outputPath,
hash: path.basename(outputPath, path.extname(outputPath)), hash: path.basename(outputPath, path.extname(outputPath)),
type: outputPath.endsWith(".css") type: outputPath.endsWith(".css")
? "text/css" ? "text/css"
: "text/javascript", : "text/javascript",
entrypoint: meta.entryPoint, entrypoint,
css_path: cssPath, css_path: meta.cssBundle,
file_name, file_name,
local_path, local_path,
url_path, url_path,

View File

@ -1,4 +1,3 @@
import ora, { type Ora } from "ora";
import type { import type {
BundlerCTXMap, BundlerCTXMap,
BunextConfig, BunextConfig,
@ -10,16 +9,15 @@ import grabDirNames from "../utils/grab-dir-names";
import { type FSWatcher } from "fs"; import { type FSWatcher } from "fs";
import init from "./init"; import init from "./init";
import isDevelopment from "../utils/is-development"; import isDevelopment from "../utils/is-development";
import watcher from "./server/watcher";
import { log } from "../utils/log"; import { log } from "../utils/log";
import cron from "./server/cron"; import cron from "./server/cron";
import allPagesBunBundler from "./bundler/all-pages-bun-bundler"; import type { BuildContext } from "esbuild";
import watcherEsbuildCTX from "./server/watcher-esbuild-ctx";
/** /**
* # Declare Global Variables * # Declare Global Variables
*/ */
declare global { declare global {
var ORA_SPINNER: Ora;
var CONFIG: BunextConfig; var CONFIG: BunextConfig;
var SERVER: Server<any> | undefined; var SERVER: Server<any> | undefined;
var RECOMPILING: boolean; var RECOMPILING: boolean;
@ -34,13 +32,12 @@ declare global {
var PAGE_FILES: PageFiles[]; var PAGE_FILES: PageFiles[];
var ROOT_FILE_UPDATED: boolean; var ROOT_FILE_UPDATED: boolean;
var SKIPPED_BROWSER_MODULES: Set<string>; var SKIPPED_BROWSER_MODULES: Set<string>;
var BUNDLER_CTX: BuildContext | undefined;
} }
const { PAGES_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames(); const { PAGES_DIR } = grabDirNames();
export default async function bunextInit() { export default async function bunextInit() {
global.ORA_SPINNER = ora();
global.ORA_SPINNER.clear();
global.HMR_CONTROLLERS = []; global.HMR_CONTROLLERS = [];
global.BUNDLER_CTX_MAP = {}; global.BUNDLER_CTX_MAP = {};
global.BUNDLER_REBUILDS = 0; global.BUNDLER_REBUILDS = 0;
@ -60,7 +57,7 @@ export default async function bunextInit() {
const is_dev = isDevelopment(); const is_dev = isDevelopment();
if (is_dev) { if (is_dev) {
watcher(); watcherEsbuildCTX();
} else { } else {
cron(); cron();
} }

View File

@ -0,0 +1,104 @@
import { watch, existsSync } from "fs";
import path from "path";
import grabDirNames from "../../utils/grab-dir-names";
import { log } from "../../utils/log";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
const { ROOT_DIR } = grabDirNames();
export default async function watcherEsbuildCTX() {
const pages_src_watcher = watch(
ROOT_DIR,
{
recursive: true,
persistent: true,
},
async (event, filename) => {
if (!filename) return;
const full_file_path = path.join(ROOT_DIR, filename);
if (full_file_path.match(/\/styles$/)) {
global.RECOMPILING = true;
await Bun.sleep(1000);
await fullRebuild({
msg: `Detected new \`styles\` directory. Rebuilding ...`,
});
return;
}
const excluded_match =
/node_modules\/|^public\/|^\.bunext\/|^\.git\/|^dist\/|bun\.lockb$/;
if (filename.match(excluded_match)) return;
if (filename.match(/bunext.config\.ts/)) {
await fullRebuild({
msg: `bunext.config.ts file changed. Rebuilding server ...`,
});
return;
}
const target_files_match = /\.(tsx?|jsx?|css)$/;
if (event !== "rename") {
if (filename.match(target_files_match)) {
if (global.RECOMPILING) return;
global.RECOMPILING = true;
await global.BUNDLER_CTX?.rebuild();
}
return;
}
const is_file_of_interest = Boolean(
filename.match(target_files_match),
);
if (!is_file_of_interest) {
return;
}
if (!filename.match(/^src\/pages\/|\.css$/)) return;
if (filename.match(/\/(--|\()/)) return;
if (global.RECOMPILING) return;
const action = existsSync(full_file_path) ? "created" : "deleted";
const type = filename.match(/\.css$/) ? "Sylesheet" : "Page";
await fullRebuild({
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
});
},
);
global.PAGES_SRC_WATCHER = pages_src_watcher;
}
async function fullRebuild(params?: { msg?: string }) {
try {
const { msg } = params || {};
global.RECOMPILING = true;
if (msg) {
log.watch(msg);
}
global.ROUTER.reload();
await global.BUNDLER_CTX?.dispose();
global.BUNDLER_CTX = undefined;
await allPagesESBuildContextBundler();
} catch (error: any) {
log.error(error);
} finally {
global.RECOMPILING = false;
}
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
watcherEsbuildCTX();
}
}

View File

@ -3,7 +3,6 @@ import path from "path";
import grabDirNames from "../../utils/grab-dir-names"; import grabDirNames from "../../utils/grab-dir-names";
import rebuildBundler from "./rebuild-bundler"; import rebuildBundler from "./rebuild-bundler";
import { log } from "../../utils/log"; import { log } from "../../utils/log";
// import rewritePagesModule from "../../utils/rewrite-pages-module";
const { ROOT_DIR } = grabDirNames(); const { ROOT_DIR } = grabDirNames();
@ -82,18 +81,15 @@ async function fullRebuild(params?: { msg?: string }) {
global.RECOMPILING = true; global.RECOMPILING = true;
// const target_file_paths = global.HMR_CONTROLLERS.map( const target_file_paths = global.HMR_CONTROLLERS.map(
// (hmr) => hmr.target_map?.local_path, (hmr) => hmr.target_map?.local_path,
// ).filter((f) => typeof f == "string"); ).filter((f) => typeof f == "string");
// await rewritePagesModule();
if (msg) { if (msg) {
log.watch(msg); log.watch(msg);
} }
await rebuildBundler(); await rebuildBundler({ target_file_paths });
// await rebuildBundler({ target_file_paths });
} catch (error: any) { } catch (error: any) {
log.error(error); log.error(error);
} finally { } finally {