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 // Cursor ships computed Webpack imports and platform helper packages.
30 "@cursor/sdk",
31 "node-pty",
32 "ffi-rs",
33 "@yuuang/",
34 "@ff-labs/",
35 "@clerk/electron-passkeys",
36 "node-gyp-build",
37 "node-addon-api",
38 // ws's optional accelerators. Nothing in this repo declares them, so they are
39 // not in the staged production install and the packaged app does not ship
40 // them either way -- ws wraps the require in try/catch and falls back to its
41 // JS paths. They are listed because they were being inlined from the dev
42 // store: both carry binding.gyp and prebuilds and load through
43 // node-gyp-build, and a native loader inlined into a bundle chunk searches
44 // for prebuilds that cannot be beside it. Listing them keeps that from
45 // becoming real if either is ever declared as a dependency.
46 "bufferutil",
47 "utf-8-validate",
48] as const;
49
50// These are Cursor's disk-backed dependency closure. Match package boundaries
51// so "zod" does not also externalize unrelated packages such as zod-to-json-schema.
52const CURSOR_RUNTIME_DEPENDENCIES = [
53 "@bufbuild/protobuf",
54 "@connectrpc/connect",
55 "@connectrpc/connect-node",
56 "@connectrpc/connect-web",
57 "@statsig/js-client",
58 "@statsig/client-core",
59 "zod",
60 "undici",
61 "@fastify/busboy",
62] as const;
63
64export function isRuntimeExternalCliDependency(id: string): boolean {
65 return (
66 CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix)) ||
67 CURSOR_RUNTIME_DEPENDENCIES.some((name) => id === name || id.startsWith(`${name}/`))
68 );
69}
70
71/**
72 * True when `id` must stay out of the bundle.
73 *
74 * This has to be wired to the bundler's `neverBundle`, not just to
75 * `alwaysBundle`. `alwaysBundle` only forces packages IN — returning false from
76 * it means "no opinion", and the default then applies: a declared dependency
77 * stays external, but a transitive one gets bundled. That is how a native
78 * loader such as node-gyp-build ended up inlined while node-pty (a declared
79 * dependency) stayed external.
80 */
81export function isExternalCliDependency(id: string): boolean {
82 return isRuntimeExternalCliDependency(id);
83}
84
85/** True when the CLI bundle should inline `id` rather than leave it external. */
86export function shouldBundleCliDependency(id: string): boolean {
87 if (id.startsWith("node:")) return false;
88 return !isExternalCliDependency(id);
89}
90
91/** Select direct dependency roots whose runtime closure belongs in the sidecar. */
92export function selectCliRuntimeExternalDependencies(
93 dependencies: Readonly<Record<string, string>>,
94): Record<string, string> {
95 return Object.fromEntries(
96 Object.entries(dependencies).filter(([name]) => isRuntimeExternalCliDependency(name)),
97 );
98}
99
100/**
101 * Scan an emitted bundle chunk for runtime-external packages that were inlined.
102 *
103 * Configuring the bundler is not the same as checking what it produced. The
104 * `alwaysBundle` predicate only forces packages IN; returning false from it
105 * means "no opinion", so a transitive dependency still gets bundled by default.
106 * A native loader and its helper (node-gyp-build-optional-packages and
107 * detect-libc, when msgpackr-extract was still a dependency) were inlined that
108 * way while every list-based test passed, which is why this reads the artifact
109 * instead.
110 *
111 * `regionCount` is reported so the caller can tell "nothing was inlined" apart
112 * from "the marker format changed and this scan no longer sees anything".
113 *
114 * `inlinedPackages` is every package seen in a region, which lets the caller
115 * check the opposite direction too. Verifying only that externals are absent
116 * would still pass if the bundler reverted to leaving everything external: the
117 * scan would see source-file regions, report nothing inlined, and the packaged
118 * backends would then fail with ERR_MODULE_NOT_FOUND because those packages
119 * are not in the selected sidecar closure either.
120 */
121export function findInlinedExternalPackages(source: string): {
122 readonly regionCount: number;
123 readonly inlined: ReadonlyArray<string>;
124 readonly inlinedPackages: ReadonlyArray<string>;
125} {
126 // Rolldown marks each inlined module with a `//#region <path>` comment.
127 const regionPattern = /\/\/#region\s+(\S+)/g;
128 const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
129
130 let regionCount = 0;
131 const inlined = new Set<string>();
132 const inlinedPackages = new Set<string>();
133 for (const region of source.matchAll(regionPattern)) {
134 regionCount += 1;
135 const regionPath = region[1] ?? "";
136 for (const candidate of regionPath.matchAll(packagePattern)) {
137 const name = candidate[1];
138 if (name === undefined || name === ".pnpm") continue;
139 inlinedPackages.add(name);
140 if (isExternalCliDependency(name)) inlined.add(name);
141 }
142 }
143
144 return {
145 regionCount,
146 inlined: [...inlined].sort(),
147 inlinedPackages: [...inlinedPackages].sort(),
148 };
149}