// @effect-diagnostics nodeBuiltinImport:off import * as NodeModule from "node:module"; /** * The single source of truth for packages the server CLI bundle must NOT inline. * * Two consumers derive from this list, and they must never disagree: * * - apps/server/vite.config.ts decides what stays external to the bundle. * - scripts/build-desktop-artifact.ts selects the runtime dependency roots for * the Windows server sidecar. * * A runtime package that is external but absent from the sidecar fails as soon * as Node resolves it from the emitted bundle. Keeping both consumers on one * list prevents packaging from drifting away from the bundle boundary. * * Entries are matched as prefixes (`id.startsWith(prefix)`), so they also cover * a package's platform-specific siblings — `node-gyp-build` covers * `node-gyp-build-optional-packages`, `@yuuang/` covers every `ffi-rs-*` binding. */ /** * External because Node actually loads them from disk at runtime. * * Native addons (.node), the JS wrappers that dlopen them by real path, and — * critically — the ordinary JS packages those wrappers require. An external * package is loaded from the real filesystem, so its own `require` also * resolves from the real filesystem; a dependency that was bundled away exists * only inside the emitted bundle and is unreachable there. This closure is * enforced by a test, not by inspection. */ export const CLI_RUNTIME_EXTERNAL_PREFIXES = [ "node-pty", "ffi-rs", "@yuuang/", "@ff-labs/", "@clerk/electron-passkeys", "@msgpackr-extract/", "msgpackr-extract", "node-gyp-build", "node-addon-api", // Required by node-gyp-build-optional-packages. Not native, but in the // closure: without it, WSL gets MODULE_NOT_FOUND while Windows is fine. "detect-libc", // ws's optional accelerators. Nothing in this repo declares them, so they are // not in the staged production install and the packaged app does not ship // them either way -- ws wraps the require in try/catch and falls back to its // JS paths. They are listed because they were being inlined from the dev // store: both carry binding.gyp and prebuilds and load through // node-gyp-build, and a native loader inlined into a bundle chunk searches // for prebuilds that cannot be beside it. Listing them keeps that from // becoming real if either is ever declared as a dependency. "bufferutil", "utf-8-validate", ] as const; export function isRuntimeExternalCliDependency(id: string): boolean { return CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix)); } /** * True when `id` must stay out of the bundle. * * This has to be wired to the bundler's `neverBundle`, not just to * `alwaysBundle`. `alwaysBundle` only forces packages IN — returning false from * it means "no opinion", and the default then applies: a declared dependency * stays external, but a transitive one gets bundled. That is how * msgpackr-extract, node-gyp-build-optional-packages and detect-libc ended up * inlined while node-pty (a declared dependency) stayed external. */ export function isExternalCliDependency(id: string): boolean { return isRuntimeExternalCliDependency(id); } /** True when the CLI bundle should inline `id` rather than leave it external. */ export function shouldBundleCliDependency(id: string): boolean { if (id.startsWith("node:")) return false; return !isExternalCliDependency(id); } /** Select direct dependency roots whose runtime closure belongs in the sidecar. */ export function selectCliRuntimeExternalDependencies( dependencies: Readonly>, ): Record { return Object.fromEntries( Object.entries(dependencies).filter(([name]) => isRuntimeExternalCliDependency(name)), ); } /** * Scan an emitted bundle chunk for ESM imports of packages that are not Node * built-ins. * * Inside a Node single-executable, `import` statements and `import()` can only * resolve built-in modules; any file-backed specifier throws at module * evaluation (static) or at first use (dynamic). External packages therefore * have to be reached through `createRequire`, which reads the real filesystem * in every runtime. The bundler cannot enforce this, so the check reads what it * produced. */ export function findEsmImportsOfExternalPackages(source: string): ReadonlyArray { const specifiers = new Set(); // `import x from`, `import "side-effect"`, `export ... from`, and `import()` // all resolve through the module loader. const patterns = [ /^import\s[^;]*?\sfrom\s+["']([^"']+)["']/gm, /^import\s+["']([^"']+)["']/gm, /^export\s[^;]*?\sfrom\s+["']([^"']+)["']/gm, // Rolldown may leave a `/* @vite-ignore */` style comment before the specifier. /\bimport\(\s*(?:\/\*[\s\S]*?\*\/\s*)*["']([^"']+)["']\s*[,)]/g, ]; for (const pattern of patterns) { for (const match of source.matchAll(pattern)) { const specifier = match[1]; if (specifier === undefined) continue; if (NodeModule.isBuiltin(specifier)) continue; if (specifier.startsWith("./") || specifier.startsWith("../")) continue; specifiers.add(specifier); } } return [...specifiers].sort(); } /** * Scan an emitted bundle chunk for runtime-external packages that were inlined. * * Configuring the bundler is not the same as checking what it produced. The * `alwaysBundle` predicate only forces packages IN; returning false from it * means "no opinion", so a transitive dependency still gets bundled by default. * msgpackr-extract, node-gyp-build-optional-packages and detect-libc were * inlined that way while every list-based test passed, which is why this reads * the artifact instead. * * `regionCount` is reported so the caller can tell "nothing was inlined" apart * from "the marker format changed and this scan no longer sees anything". * * `inlinedPackages` is every package seen in a region, which lets the caller * check the opposite direction too. Verifying only that externals are absent * would still pass if the bundler reverted to leaving everything external: the * scan would see source-file regions, report nothing inlined, and the packaged * backends would then fail with ERR_MODULE_NOT_FOUND because those packages * are not in the selected sidecar closure either. */ export function findInlinedExternalPackages(source: string): { readonly regionCount: number; readonly inlined: ReadonlyArray; readonly inlinedPackages: ReadonlyArray; } { // Rolldown marks each inlined module with a `//#region ` comment. const regionPattern = /\/\/#region\s+(\S+)/g; const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g; let regionCount = 0; const inlined = new Set(); const inlinedPackages = new Set(); for (const region of source.matchAll(regionPattern)) { regionCount += 1; const regionPath = region[1] ?? ""; for (const candidate of regionPath.matchAll(packagePattern)) { const name = candidate[1]; if (name === undefined || name === ".pnpm") continue; inlinedPackages.add(name); if (isExternalCliDependency(name)) inlined.add(name); } } return { regionCount, inlined: [...inlined].sort(), inlinedPackages: [...inlinedPackages].sort(), }; }