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 "node-gyp-build",
19 "node-addon-api",
20 "bufferutil",
21 "utf-8-validate",
22] as const;
23
24// Match Cursor dependency boundaries so zod does not externalize zod-to-json-schema.
25const CURSOR_RUNTIME_DEPENDENCIES = [
26 "@bufbuild/protobuf",
27 "@connectrpc/connect",
28 "@connectrpc/connect-node",
29 "@connectrpc/connect-web",
30 "@statsig/js-client",
31 "@statsig/client-core",
32 "zod",
33 "undici",
34 "@fastify/busboy",
35] as const;
36
37export function isRuntimeExternalCliDependency(id: string): boolean {
38 return (
39 CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix)) ||
40 CURSOR_RUNTIME_DEPENDENCIES.some((name) => id === name || id.startsWith(`${name}/`))
41 );
42}
43
44/** Use neverBundle: false from alwaysBundle means no opinion and leaves transitive packages bundled. */
45export function isExternalCliDependency(id: string): boolean {
46 return isRuntimeExternalCliDependency(id);
47}
48
49export function shouldBundleCliDependency(id: string): boolean {
50 if (id.startsWith("node:")) return false;
51 return !isExternalCliDependency(id);
52}
53
54export function selectCliRuntimeExternalDependencies(
55 dependencies: Readonly<Record<string, string>>,
56): Record<string, string> {
57 return Object.fromEntries(
58 Object.entries(dependencies).filter(([name]) => isRuntimeExternalCliDependency(name)),
59 );
60}
61
62/** regionCount distinguishes a clean bundle from a changed marker format the scan cannot see. */
63export function findInlinedExternalPackages(source: string): {
64 readonly regionCount: number;
65 readonly inlined: ReadonlyArray<string>;
66 readonly inlinedPackages: ReadonlyArray<string>;
67} {
68 // Rolldown marks each inlined module with a `//#region <path>` line; newer
69 // releases (vite-plus 1.4) emit a bare `// <path>` line instead.
70 const regionPattern = /^\/\/(?:#region)?[ \t]+(\S+)[ \t]*$/gm;
71 const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
72
73 let regionCount = 0;
74 const inlined = new Set<string>();
75 const inlinedPackages = new Set<string>();
76 for (const region of source.matchAll(regionPattern)) {
77 regionCount += 1;
78 const regionPath = region[1] ?? "";
79 for (const candidate of regionPath.matchAll(packagePattern)) {
80 const name = candidate[1];
81 if (name === undefined || name === ".pnpm") continue;
82 inlinedPackages.add(name);
83 if (isExternalCliDependency(name)) inlined.add(name);
84 }
85 }
86
87 return {
88 regionCount,
89 inlined: [...inlined].sort(),
90 inlinedPackages: [...inlinedPackages].sort(),
91 };
92}