Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

cli-external-packages.ts

88 lines2.9 KB

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