Bugfix. Fix SSR pages filters. Remove dedicated API bundler, use SSR bundler for pages and api routes.

This commit is contained in:
2026-04-12 06:48:10 +01:00
parent 532d0d6b56
commit 84d490b189
21 changed files with 131 additions and 138 deletions
+33 -31
View File
@@ -8,35 +8,37 @@ const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames();
export default async function apiRoutesContextBundler() {
const pages = grabAllPages({ api_only: true });
const dev = isDevelopment();
if (global.API_ROUTES_BUNDLER_CTX) {
await global.API_ROUTES_BUNDLER_CTX.dispose();
global.API_ROUTES_BUNDLER_CTX = undefined;
}
global.API_ROUTES_BUNDLER_CTX = await esbuild.context({
entryPoints: pages.map((p) => p.local_path),
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: "api/[dir]/[hash]",
metafile: true,
plugins: [
tailwindEsbuildPlugin,
apiRoutesCTXArtifactTracker({ pages }),
],
jsx: "automatic",
external: [
"react",
"react-dom",
"react/jsx-runtime",
"react/jsx-dev-runtime",
"bun:*",
],
});
await global.API_ROUTES_BUNDLER_CTX.rebuild();
// if (global.API_ROUTES_BUNDLER_CTX) {
// await global.API_ROUTES_BUNDLER_CTX.dispose();
// global.API_ROUTES_BUNDLER_CTX = undefined;
// }
// global.API_ROUTES_BUNDLER_CTX = await esbuild.context({
// entryPoints: pages.map((p) => p.local_path),
// 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: "api/[dir]/[hash]",
// metafile: true,
// plugins: [
// tailwindEsbuildPlugin,
// apiRoutesCTXArtifactTracker({ pages }),
// ],
// jsx: "automatic",
// external: [
// "react",
// "react-dom",
// "react/jsx-runtime",
// "react/jsx-dev-runtime",
// "bun:*",
// ],
// });
// await global.API_ROUTES_BUNDLER_CTX.rebuild();
}
+1 -1
View File
@@ -5,7 +5,7 @@ export default async function buildOnstartErrorHandler(params) {
global.BUNDLER_CTX_DISPOSED = true;
global.RECOMPILING = false;
global.IS_SERVER_COMPONENT = false;
await Promise.all([
Promise.all([
global.SSR_BUNDLER_CTX?.dispose(),
global.BUNDLER_CTX?.dispose(),
]);
+2 -1
View File
@@ -9,7 +9,7 @@ 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 pagesSSRContextBundler(params) {
const pages = grabAllPages({ exclude_api: true });
const pages = grabAllPages();
const dev = isDevelopment();
if (global.SSR_BUNDLER_CTX) {
await global.SSR_BUNDLER_CTX.dispose();
@@ -61,6 +61,7 @@ export default async function pagesSSRContextBundler(params) {
"react-dom",
"react/jsx-runtime",
"react/jsx-dev-runtime",
"bun:*",
],
// logLevel: "silent",
});
@@ -44,17 +44,21 @@ export default function apiRoutesCTXArtifactTracker({ pages }) {
url_path,
};
});
if (artifacts?.[0] && artifacts.length > 0) {
for (let i = 0; i < artifacts.length; i++) {
const artifact = artifacts[i];
if (artifact?.local_path &&
global.API_ROUTES_BUNDLER_CTX_MAP) {
global.API_ROUTES_BUNDLER_CTX_MAP[artifact.local_path] = artifact;
}
}
}
// const elapsed = (performance.now() - build_start).toFixed(0);
// log.success(`API Routes [Built] in ${elapsed}ms`);
// if (artifacts?.[0] && artifacts.length > 0) {
// for (let i = 0; i < artifacts.length; i++) {
// const artifact = artifacts[i];
// if (
// artifact?.local_path &&
// global.API_ROUTES_BUNDLER_CTX_MAP
// ) {
// global.API_ROUTES_BUNDLER_CTX_MAP[
// artifact.local_path
// ] = artifact;
// }
// }
// }
const elapsed = (performance.now() - build_start).toFixed(0);
log.success(`API Routes [Built] in ${elapsed}ms`);
});
},
};
@@ -48,12 +48,6 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
else {
pagesSSRContextBundler();
}
if (global.API_ROUTES_BUNDLER_CTX) {
global.API_ROUTES_BUNDLER_CTX.rebuild();
}
else {
apiRoutesContextBundler();
}
});
},
};
+6 -1
View File
@@ -1,6 +1,7 @@
import {} from "esbuild";
import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result";
import buildOnstartErrorHandler from "../build-on-start-error-handler";
import { log } from "../../../utils/log";
let build_start = 0;
let build_starts = 0;
const MAX_BUILD_STARTS = 2;
@@ -34,7 +35,11 @@ export default function ssrCTXArtifactTracker({ entryToPage, post_build_fn, }) {
artifact;
}
}
post_build_fn?.({ artifacts });
// post_build_fn?.({ artifacts });
// const elapsed = (performance.now() - build_start).toFixed(
// 0,
// );
// log.success(`SSR [Built] in ${elapsed}ms`);
}
});
},
-4
View File
@@ -23,9 +23,6 @@ declare global {
var SSR_BUNDLER_CTX_MAP: {
[k: string]: BundlerCTXMap;
};
var API_ROUTES_BUNDLER_CTX_MAP: {
[k: string]: BundlerCTXMap;
};
var BUNDLER_REBUILDS: 0;
var PAGES_SRC_WATCHER: FSWatcher | undefined;
var CURRENT_VERSION: string | undefined;
@@ -34,7 +31,6 @@ declare global {
var SKIPPED_BROWSER_MODULES: Set<string>;
var BUNDLER_CTX: BuildContext | undefined;
var SSR_BUNDLER_CTX: BuildContext | undefined;
var API_ROUTES_BUNDLER_CTX: BuildContext | undefined;
var DIR_NAMES: ReturnType<typeof grabDirNames>;
var REACT_IMPORTS_MAP: {
imports: Record<string, string>;
+1 -1
View File
@@ -15,7 +15,7 @@ export default async function bunextInit() {
global.HMR_CONTROLLERS = [];
global.BUNDLER_CTX_MAP = {};
global.SSR_BUNDLER_CTX_MAP = {};
global.API_ROUTES_BUNDLER_CTX_MAP = {};
// global.API_ROUTES_BUNDLER_CTX_MAP = {};
global.BUNDLER_REBUILDS = 0;
global.REBUILD_RETRIES = 0;
global.PAGE_FILES = [];
+2 -2
View File
@@ -30,8 +30,8 @@ export default async function ({ req }) {
});
let module;
const now = Date.now();
if (is_dev && global.API_ROUTES_BUNDLER_CTX_MAP?.[match.filePath]?.path) {
const target_import = path.join(ROOT_DIR, global.API_ROUTES_BUNDLER_CTX_MAP[match.filePath].path);
if (is_dev && global.SSR_BUNDLER_CTX_MAP?.[match.filePath]?.path) {
const target_import = path.join(ROOT_DIR, global.SSR_BUNDLER_CTX_MAP[match.filePath].path);
module = await import(`${target_import}?t=${now}`);
}
else {