Open History to see the last commit
1/** Keep bundler externals and staged runtime dependency roots aligned. */
2// Filesystem-loaded externals need dependencies outside the bundle.
3const CLI_RUNTIME_EXTERNAL_PREFIXES = [
4 // Cursor ships computed Webpack imports and platform helper packages.
5 "@cursor/sdk",
6 // Playwright reads package.json and browsers.json beside its runtime modules.
7 "playwright-core",
8 "node-pty",
9 "ffi-rs",
10 "@yuuang/",
11 "@ff-labs/",
12 "transcribe-cpp",
13 "@transcribe-cpp/",
14 "koffi",
15 "@koromix/",
16 "@napi-rs/keyring",
17 "@clerk/electron-passkeys",
18 "@trycua/cua-driver",
19 "@ubjs/",
20 "node-gyp-build",
21 "node-addon-api",
22 "bufferutil",
23 "utf-8-validate",
24] as const;
25
26// Match Cursor dependency boundaries so zod does not externalize zod-to-json-schema.
27const CURSOR_RUNTIME_DEPENDENCIES = [
28 "@bufbuild/protobuf",
29 "@connectrpc/connect",
30 "@connectrpc/connect-node",
31 "@connectrpc/connect-web",
32 "@statsig/js-client",
33 "@statsig/client-core",
34 "zod",
35 "undici",
36 "@fastify/busboy",
37] as const;
38
39export function isRuntimeExternalCliDependency(id: string): boolean {
40 return (
41 CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix)) ||
42 CURSOR_RUNTIME_DEPENDENCIES.some((name) => id === name || id.startsWith(`${name}/`))
43 );
44}
45
46/** Use neverBundle: false from alwaysBundle means no opinion and leaves transitive packages bundled. */
47export function isExternalCliDependency(id: string): boolean {
48 return isRuntimeExternalCliDependency(id);
49}
50
51export function shouldBundleCliDependency(id: string): boolean {
52 if (id.startsWith("node:")) return false;
53 return !isExternalCliDependency(id);
54}
55
56export function selectCliRuntimeExternalDependencies(
57 dependencies: Readonly<Record<string, string>>,
58): Record<string, string> {
59 return Object.fromEntries(
60 Object.entries(dependencies).filter(([name]) => isRuntimeExternalCliDependency(name)),
61 );
62}
63
64/** regionCount distinguishes a clean bundle from a changed marker format the scan cannot see. */
65export function findInlinedExternalPackages(source: string): {
66 readonly regionCount: number;
67 readonly inlined: ReadonlyArray<string>;
68 readonly inlinedPackages: ReadonlyArray<string>;
69} {
70 // Rolldown marks each inlined module with a `//#region <path>` line; newer
71 // releases (vite-plus 1.4) emit a bare `// <path>` line instead.
72 const regionPattern = /^\/\/(?:#region)?[ \t]+(\S+)[ \t]*$/gm;
73 const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
74
75 let regionCount = 0;
76 const inlined = new Set<string>();
77 const inlinedPackages = new Set<string>();
78 for (const region of source.matchAll(regionPattern)) {
79 regionCount += 1;
80 const regionPath = region[1] ?? "";
81 for (const candidate of regionPath.matchAll(packagePattern)) {
82 const name = candidate[1];
83 if (name === undefined || name === ".pnpm") continue;
84 inlinedPackages.add(name);
85 if (isExternalCliDependency(name)) inlined.add(name);
86 }
87 }
88
89 return {
90 regionCount,
91 inlined: [...inlined].sort(),
92 inlinedPackages: [...inlinedPackages].sort(),
93 };
94}