Files
bunext/dist/functions/bunext-init.js
T
2026-10-16 16:54:55 +01:00

97 lines
4.0 KiB
JavaScript

import grabDirNames, {} from "../utils/grab-dir-names";
import init from "./init";
import isDevelopment from "../utils/is-development";
import { log } from "../utils/log";
import cron from "./server/cron";
import allPagesESBuildContextBundler from "./bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "./server/server-post-build-fn";
import reactModulesBundler from "./bundler/react-modules-bundler";
import grabConstants from "../utils/grab-constants";
import watcherEsbuildCTX from "./server/watcher-esbuild-ctx";
import disposeBundlerContexts from "./bundler/dispose-bundler-contexts";
import { readFileSync } from "fs";
const dirNames = grabDirNames();
const { PAGES_DIR, BUNDLER_CTX_MAP_JSON, SSR_BUNDLER_CTX_MAP_JSON } = dirNames;
export default async function bunextInit(params) {
const is_dev = isDevelopment();
if (!is_dev || params?.start) {
try {
const bundler_ctx_json = JSON.parse(readFileSync(BUNDLER_CTX_MAP_JSON, "utf-8"));
if (bundler_ctx_json) {
global.BUNEXT_BUNDLER_CTX_MAP = bundler_ctx_json;
}
}
catch (error) { }
try {
const ssr_bundler_ctx_json = JSON.parse(readFileSync(SSR_BUNDLER_CTX_MAP_JSON, "utf-8"));
if (ssr_bundler_ctx_json) {
global.BUNEXT_SSR_BUNDLER_CTX_MAP = ssr_bundler_ctx_json;
}
}
catch (error) { }
}
const are_modules_aready_built = global.BUNEXT_BUNDLER_CTX_MAP && global.BUNEXT_SSR_BUNDLER_CTX_MAP;
global.BUNEXT_HMR_CONTROLLERS = [];
if (!global.BUNEXT_BUNDLER_CTX_MAP) {
global.BUNEXT_BUNDLER_CTX_MAP = {};
}
if (!global.BUNEXT_SSR_BUNDLER_CTX_MAP) {
global.BUNEXT_SSR_BUNDLER_CTX_MAP = {};
}
// global.BUNEXT_API_ROUTES_BUNDLER_CTX_MAP = {};
global.BUNEXT_BUNDLER_REBUILDS = 0;
global.BUNEXT_REBUILD_RETRIES = 0;
global.BUNEXT_BUILD_FAILED = false;
global.BUNEXT_PAGE_FILES = [];
global.BUNEXT_SKIPPED_BROWSER_MODULES = new Set();
global.BUNEXT_DIR_NAMES = dirNames;
global.BUNEXT_REACT_IMPORTS_MAP = { imports: {} };
global.BUNEXT_REACT_DOM_MODULE_CACHE = new Map();
global.BUNEXT_MAIN_CTX_BUILD_STARTS = 0;
await init();
log.banner();
global.BUNEXT_CONSTANTS = grabConstants();
await reactModulesBundler();
const router = new Bun.FileSystemRouter({
style: "nextjs",
dir: PAGES_DIR,
});
global.BUNEXT_ROUTER = router;
if (params?.build_only) {
log.build(`Building Modules ...`);
await allPagesESBuildContextBundler({ build_only: params.build_only });
}
else if (is_dev) {
log.build(`Building Modules ...`);
// Do NOT block on the full-site build: `Bun.serve` (or the start
// server) opens its port immediately and requests await
// BUNEXT_INITIAL_BUILD_PROMISE, so the site becomes reachable while
// the one-time compile still runs in the background.
const initial_build_promise = allPagesESBuildContextBundler({
post_build_fn: async (params) => {
await serverPostBuildFn(params);
},
});
global.BUNEXT_INITIAL_BUILD_PROMISE = initial_build_promise;
// Errors are logged (deduped) by the build pipeline itself; swallow
// the rejection here so it isn't an unhandled rejection.
initial_build_promise.catch(() => { });
// Start watching immediately so edits made during the initial build
// are not missed (rebuilds coalesce with the in-flight build).
watcherEsbuildCTX();
}
else {
if (are_modules_aready_built) {
log.success(`All modules already built.`);
}
else {
log.build(`Building Modules ...`);
await allPagesESBuildContextBundler({ start: true });
// The server only needs the artifact maps; don't keep
// esbuild's build state alive for the rest of the process.
await disposeBundlerContexts({ stop_service: true });
}
cron();
}
}