Open History to see the last commit
1/**
2 * The single source of truth for packages the server CLI bundle must NOT inline.
3 *
4 * Two consumers derive from this list, and they must never disagree:
5 *
6 * - apps/server/vite.config.ts decides what stays external to the bundle.
7 * - scripts/build-desktop-artifact.ts selects the runtime dependency roots for
8 * the Windows server sidecar.
9 *
10 * A runtime package that is external but absent from the sidecar fails as soon
11 * as Node resolves it from the emitted bundle. Keeping both consumers on one
12 * list prevents packaging from drifting away from the bundle boundary.
13 *
14 * Entries are matched as prefixes (`id.startsWith(prefix)`), so they also cover
15 * a package's platform-specific siblings — `node-gyp-build` covers
16 * `node-gyp-build-optional-packages`, `@yuuang/` covers every `ffi-rs-*` binding.
17 */
18/**
19 * External because Node actually loads them from disk at runtime.
20 *
21 * Native addons (.node), the JS wrappers that dlopen them by real path, and —
22 * critically — the ordinary JS packages those wrappers require. An external
23 * package is loaded from the real filesystem, so its own `require` also
24 * resolves from the real filesystem; a dependency that was bundled away exists
25 * only inside the emitted bundle and is unreachable there. This closure is
26 * enforced by a test, not by inspection.
27 */
28export const CLI_RUNTIME_EXTERNAL_PREFIXES = [
29 "node-pty",
30 "ffi-rs",
31 "@yuuang/",
32 "@ff-labs/",
33 "@napi-rs/keyring",
34 "@clerk/electron-passkeys",
35 "node-gyp-build",
36 "node-addon-api",
37 // ws's optional accelerators. Nothing in this repo declares them, so they are
38 // not in the staged production install and the packaged app does not ship
39 // them either way -- ws wraps the require in try/catch and falls back to its
40 // JS paths. They are listed because they were being inlined from the dev
41 // store: both carry binding.gyp and prebuilds and load through
42 // node-gyp-build, and a native loader inlined into a bundle chunk searches
43 // for prebuilds that cannot be beside it. Listing them keeps that from
44 // becoming real if either is ever declared as a dependency.
45 "bufferutil",
46 "utf-8-validate",
47] as const;
48
49export function isRuntimeExternalCliDependency(id: string): boolean {
50 return CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix));
51}
52
53/**
54 * True when `id` must stay out of the bundle.
55 *
56 * This has to be wired to the bundler's `neverBundle`, not just to
57 * `alwaysBundle`. `alwaysBundle` only forces packages IN — returning false from
58 * it means "no opinion", and the default then applies: a declared dependency
59 * stays external, but a transitive one gets bundled. That is how a native
60 * loader such as node-gyp-build ended up inlined while node-pty (a declared
61 * dependency) stayed external.
62 */
63export function isExternalCliDependency(id: string): boolean {
64 return isRuntimeExternalCliDependency(id);
65}
66
67/** True when the CLI bundle should inline `id` rather than leave it external. */
68export function shouldBundleCliDependency(id: string): boolean {
69 if (id.startsWith("node:")) return false;
70 return !isExternalCliDependency(id);
71}
72
73/** Select direct dependency roots whose runtime closure belongs in the sidecar. */
74export function selectCliRuntimeExternalDependencies(
75 dependencies: Readonly<Record<string, string>>,
76): Record<string, string> {
77 return Object.fromEntries(
78 Object.entries(dependencies).filter(([name]) => isRuntimeExternalCliDependency(name)),
79 );
80}
81
82/**
83 * Scan an emitted bundle chunk for runtime-external packages that were inlined.
84 *
85 * Configuring the bundler is not the same as checking what it produced. The
86 * `alwaysBundle` predicate only forces packages IN; returning false from it
87 * means "no opinion", so a transitive dependency still gets bundled by default.
88 * A native loader and its helper (node-gyp-build-optional-packages and
89 * detect-libc, when msgpackr-extract was still a dependency) were inlined that
90 * way while every list-based test passed, which is why this reads the artifact
91 * instead.
92 *
93 * `regionCount` is reported so the caller can tell "nothing was inlined" apart
94 * from "the marker format changed and this scan no longer sees anything".
95 *
96 * `inlinedPackages` is every package seen in a region, which lets the caller
97 * check the opposite direction too. Verifying only that externals are absent
98 * would still pass if the bundler reverted to leaving everything external: the
99 * scan would see source-file regions, report nothing inlined, and the packaged
100 * backends would then fail with ERR_MODULE_NOT_FOUND because those packages
101 * are not in the selected sidecar closure either.
102 */
103export function findInlinedExternalPackages(source: string): {
104 readonly regionCount: number;
105 readonly inlined: ReadonlyArray<string>;
106 readonly inlinedPackages: ReadonlyArray<string>;
107} {
108 // Rolldown marks each inlined module with a `//#region <path>` comment.
109 const regionPattern = /\/\/#region\s+(\S+)/g;
110 const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
111
112 let regionCount = 0;
113 const inlined = new Set<string>();
114 const inlinedPackages = new Set<string>();
115 for (const region of source.matchAll(regionPattern)) {
116 regionCount += 1;
117 const regionPath = region[1] ?? "";
118 for (const candidate of regionPath.matchAll(packagePattern)) {
119 const name = candidate[1];
120 if (name === undefined || name === ".pnpm") continue;
121 inlinedPackages.add(name);
122 if (isExternalCliDependency(name)) inlined.add(name);
123 }
124 }
125
126 return {
127 regionCount,
128 inlined: [...inlined].sort(),
129 inlinedPackages: [...inlinedPackages].sort(),
130 };
131}