Open History to see the last commit
1/**
2 * Shared by the CLI bundler and Windows sidecar dependency selection.
3 * Prefixes include platform siblings and native loaders' transitive dependencies:
4 * external packages resolve their imports from disk, outside the bundle.
5 */
6export const CLI_RUNTIME_EXTERNAL_PREFIXES = [
7 "node-pty",
8 "ffi-rs",
9 "@yuuang/",
10 "@ff-labs/",
11 "@clerk/electron-passkeys",
12 "@msgpackr-extract/",
13 "msgpackr-extract",
14 "node-gyp-build",
15 "node-addon-api",
16 // node-gyp-build-optional-packages requires this on Linux/WSL.
17 "detect-libc",
18 // Optional ws addons must stay external so native loaders can locate prebuilds.
19 "bufferutil",
20 "utf-8-validate",
21] as const;
22
23/** Bun-only imports stay unbundled because Node cannot resolve their bun:* specifiers. */
24export const CLI_BUILD_ONLY_EXTERNAL_PREFIXES = [
25 "@effect/platform-bun",
26 "@effect/sql-sqlite-bun",
27] as const;
28
29export const CLI_EXTERNAL_PACKAGE_PREFIXES = [
30 ...CLI_RUNTIME_EXTERNAL_PREFIXES,
31 ...CLI_BUILD_ONLY_EXTERNAL_PREFIXES,
32] as const;
33
34export function isRuntimeExternalCliDependency(id: string): boolean {
35 return CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix));
36}
37
38/** Use with neverBundle; returning false from alwaysBundle still bundles transitive imports. */
39export function isExternalCliDependency(id: string): boolean {
40 return CLI_EXTERNAL_PACKAGE_PREFIXES.some((prefix) => id.startsWith(prefix));
41}
42
43/** True when the CLI bundle should inline `id` rather than leave it external. */
44export function shouldBundleCliDependency(id: string): boolean {
45 if (id.startsWith("node:")) return false;
46 return !isExternalCliDependency(id);
47}
48
49/** Select direct dependency roots whose runtime closure belongs in the sidecar. */
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 );
56}
57
58/**
59 * Scan Rolldown regions for inlined packages. regionCount detects missing markers;
60 * inlinedPackages lets callers verify that ordinary dependencies were bundled too.
61 */
62export function findInlinedExternalPackages(source: string): {
63 readonly regionCount: number;
64 readonly inlined: ReadonlyArray<string>;
65 readonly inlinedPackages: ReadonlyArray<string>;
66} {
67 // Rolldown marks each inlined module with a `//#region <path>` comment.
68 const regionPattern = /\/\/#region\s+(\S+)/g;
69 const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
70
71 let regionCount = 0;
72 const inlined = new Set<string>();
73 const inlinedPackages = new Set<string>();
74 for (const region of source.matchAll(regionPattern)) {
75 regionCount += 1;
76 const regionPath = region[1] ?? "";
77 for (const candidate of regionPath.matchAll(packagePattern)) {
78 const name = candidate[1];
79 if (name === undefined || name === ".pnpm") continue;
80 inlinedPackages.add(name);
81 if (isExternalCliDependency(name)) inlined.add(name);
82 }
83 }
84
85 return {
86 regionCount,
87 inlined: [...inlined].sort(),
88 inlinedPackages: [...inlinedPackages].sort(),
89 };
90}