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

cli-external-packages.ts

87 lines2.8 KB

1/** Keep bundler externals and staged runtime dependency roots aligned. */
2// Filesystem-loaded externals need dependencies outside the bundle.
3export const 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>` comment.
65 const regionPattern = /\/\/#region\s+(\S+)/g;
66 const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
67​
68 let regionCount = 0;
69 const inlined = new Set<string>();
70 const inlinedPackages = new Set<string>();
71 for (const region of source.matchAll(regionPattern)) {
72 regionCount += 1;
73 const regionPath = region[1] ?? "";
74 for (const candidate of regionPath.matchAll(packagePattern)) {
75 const name = candidate[1];
76 if (name === undefined || name === ".pnpm") continue;
77 inlinedPackages.add(name);
78 if (isExternalCliDependency(name)) inlined.add(name);
79 }
80 }
81​
82 return {
83 regionCount,
84 inlined: [...inlined].sort(),
85 inlinedPackages: [...inlinedPackages].sort(),
86 };