Open History to see the last commit
1/**
2 * Two consumers derive from this list, and they must never disagree:
3 *
4 * - apps/server/vite.config.ts decides what stays external to the bundle.
5 * - scripts/build-desktop-artifact.ts selects the runtime dependency roots for
6 * the Windows server sidecar.
7 *
8 */
9/**
10 * An external package is loaded from the real filesystem, so its own `require`
11 * also resolves from the real filesystem; a dependency that was bundled away
12 * exists only inside the emitted bundle and is unreachable there.
13 */
14export const CLI_RUNTIME_EXTERNAL_PREFIXES = [
15 // Cursor ships computed Webpack imports and platform helper packages.
16 "@cursor/sdk",
17 // Playwright reads package.json and browsers.json beside its runtime modules.
18 "playwright-core",
19 "node-pty",
20 "ffi-rs",
21 "@yuuang/",
22 "@ff-labs/",
23 "@napi-rs/keyring",
24 "@clerk/electron-passkeys",
25 "node-gyp-build",
26 "node-addon-api",
27 "bufferutil",
28 "utf-8-validate",
29] as const;
30
31// These are Cursor's disk-backed dependency closure. Match package boundaries
32// so "zod" does not also externalize unrelated packages such as zod-to-json-schema.
33const CURSOR_RUNTIME_DEPENDENCIES = [
34 "@bufbuild/protobuf",
35 "@connectrpc/connect",
36 "@connectrpc/connect-node",
37 "@connectrpc/connect-web",
38 "@statsig/js-client",
39 "@statsig/client-core",
40 "zod",
41 "undici",
42 "@fastify/busboy",
43] as const;
44
45export function isRuntimeExternalCliDependency(id: string): boolean {
46 return (
47 CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix)) ||
48 CURSOR_RUNTIME_DEPENDENCIES.some((name) => id === name || id.startsWith(`${name}/`))
49 );
50}
51
52/**
53 * This has to be wired to the bundler's `neverBundle`, not just to
54 * `alwaysBundle`. `alwaysBundle` only forces packages IN — returning false from
55 * it means "no opinion", and the default then applies: a declared dependency
56 * stays external, but a transitive one gets bundled.
57 */
58export function isExternalCliDependency(id: string): boolean {
59 return isRuntimeExternalCliDependency(id);
60}
61
62export function shouldBundleCliDependency(id: string): boolean {
63 if (id.startsWith("node:")) return false;
64 return !isExternalCliDependency(id);
65}
66
67export function selectCliRuntimeExternalDependencies(
68 dependencies: Readonly<Record<string, string>>,
69): Record<string, string> {
70 return Object.fromEntries(
71 Object.entries(dependencies).filter(([name]) => isRuntimeExternalCliDependency(name)),
72 );
73}
74
75/**
76 * `regionCount` is reported so the caller can tell "nothing was inlined" apart
77 * from "the marker format changed and this scan no longer sees anything".
78 */
79export function findInlinedExternalPackages(source: string): {
80 readonly regionCount: number;
81 readonly inlined: ReadonlyArray<string>;
82 readonly inlinedPackages: ReadonlyArray<string>;
83} {
84 // Rolldown marks each inlined module with a `//#region <path>` comment.
85 const regionPattern = /\/\/#region\s+(\S+)/g;
86 const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
87
88 let regionCount = 0;
89 const inlined = new Set<string>();
90 const inlinedPackages = new Set<string>();
91 for (const region of source.matchAll(regionPattern)) {
92 regionCount += 1;
93 const regionPath = region[1] ?? "";
94 for (const candidate of regionPath.matchAll(packagePattern)) {
95 const name = candidate[1];
96 if (name === undefined || name === ".pnpm") continue;
97 inlinedPackages.add(name);
98 if (isExternalCliDependency(name)) inlined.add(name);
99 }
100 }
101
102 return {
103 regionCount,
104 inlined: [...inlined].sort(),
105 inlinedPackages: [...inlinedPackages].sort(),
106 };
107}