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
@@ -11,39 +11,39 @@ 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;
}
// 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:*",
],
});
// 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();
// await global.API_ROUTES_BUNDLER_CTX.rebuild();
}
@@ -11,7 +11,7 @@ export default async function buildOnstartErrorHandler(params?: Params) {
global.RECOMPILING = false;
global.IS_SERVER_COMPONENT = false;
await Promise.all([
Promise.all([
global.SSR_BUNDLER_CTX?.dispose(),
global.BUNDLER_CTX?.dispose(),
]);
@@ -16,7 +16,7 @@ type Params = {
};
export default async function pagesSSRContextBundler(params?: Params) {
const pages = grabAllPages({ exclude_api: true });
const pages = grabAllPages();
const dev = isDevelopment();
if (global.SSR_BUNDLER_CTX) {
@@ -77,6 +77,7 @@ export default async function pagesSSRContextBundler(params?: Params) {
"react-dom",
"react/jsx-runtime",
"react/jsx-dev-runtime",
"bun:*",
],
// logLevel: "silent",
});
@@ -66,22 +66,22 @@ export default function apiRoutesCTXArtifactTracker({ pages }: Params) {
};
});
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;
}
}
}
// 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`);
const elapsed = (performance.now() - build_start).toFixed(0);
log.success(`API Routes [Built] in ${elapsed}ms`);
});
},
};
@@ -31,6 +31,7 @@ export default function esbuildCTXArtifactTracker({
build.onStart(async () => {
build_starts++;
build_start = performance.now();
if (build_starts == MAX_BUILD_STARTS) {
await buildOnstartErrorHandler();
}
@@ -74,12 +75,6 @@ export default function esbuildCTXArtifactTracker({
} else {
pagesSSRContextBundler();
}
if (global.API_ROUTES_BUNDLER_CTX) {
global.API_ROUTES_BUNDLER_CTX.rebuild();
} else {
apiRoutesContextBundler();
}
});
},
};
@@ -2,6 +2,7 @@ import { type Plugin } from "esbuild";
import type { PageFiles } from "../../../types";
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;
@@ -56,7 +57,12 @@ export default function ssrCTXArtifactTracker({
}
}
post_build_fn?.({ artifacts });
// post_build_fn?.({ artifacts });
// const elapsed = (performance.now() - build_start).toFixed(
// 0,
// );
// log.success(`SSR [Built] in ${elapsed}ms`);
}
});
},
+3 -3
View File
@@ -33,7 +33,7 @@ declare global {
var LAST_BUILD_TIME: number;
var BUNDLER_CTX_MAP: { [k: string]: BundlerCTXMap };
var SSR_BUNDLER_CTX_MAP: { [k: string]: BundlerCTXMap };
var API_ROUTES_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;
@@ -42,7 +42,7 @@ 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 API_ROUTES_BUNDLER_CTX: BuildContext | undefined;
var DIR_NAMES: ReturnType<typeof grabDirNames>;
var REACT_IMPORTS_MAP: { imports: Record<string, string> };
var REACT_DOM_SERVER: any;
@@ -60,7 +60,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
@@ -52,10 +52,10 @@ export default async function ({ req }: Params): Promise<Response | undefined> {
let module: any;
const now = Date.now();
if (is_dev && 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.API_ROUTES_BUNDLER_CTX_MAP[match.filePath].path,
global.SSR_BUNDLER_CTX_MAP[match.filePath].path,
);
module = await import(`${target_import}?t=${now}`);
-2
View File
@@ -1,5 +1,3 @@
import type { APIResponseObject } from "../types";
type Params = {
path: string;
};
+3 -8
View File
@@ -3,7 +3,6 @@ import grabDirNames from "./grab-dir-names";
import path from "path";
import type { PageFiles } from "../types";
import AppNames from "./grab-app-names";
import pagePathTransform from "./page-path-transform";
import checkExcludedPatterns from "./check-excluded-patterns";
type Params = {
@@ -50,22 +49,18 @@ function grabPageDirRecursively({ page_dir }: { page_dir: string }) {
continue;
}
if (checkExcludedPatterns({ path: page })) {
if (checkExcludedPatterns({ path: full_page_path })) {
continue;
}
if (page.match(/\/api\//)) {
continue;
}
if (page_name.split(".").length > 2) {
if (page_name.match(/\.server\.tsx?/)) {
continue;
}
const page_stat = statSync(full_page_path);
if (page_stat.isDirectory()) {
if (checkExcludedPatterns({ path: page })) continue;
if (checkExcludedPatterns({ path: full_page_path })) continue;
const new_page_files = grabPageDirRecursively({
page_dir: full_page_path,
});