Open History to see the last commit
1/**
2 * The single source of truth for packages the server CLI bundle must NOT inline.
3 *
4 * Two consumers derive from this list, and they must never disagree:
5 *
6 * - apps/server/vite.config.ts decides what stays external to the bundle.
7 * - scripts/build-desktop-artifact.ts selects the runtime dependency roots for
8 * the Windows server sidecar.
9 *
10 * A runtime package that is external but absent from the sidecar fails as soon
11 * as Node resolves it from the emitted bundle. Keeping both consumers on one
12 * list prevents packaging from drifting away from the bundle boundary.
13 *
14 * Entries are matched as prefixes (`id.startsWith(prefix)`), so they also cover
15 * a package's platform-specific siblings — `node-gyp-build` covers
16 * `node-gyp-build-optional-packages`, `@yuuang/` covers every `ffi-rs-*` binding.
17 */
18/**
19 * External because Node actually loads them from disk at runtime.
20 *
21 * Native addons (.node), the JS wrappers that dlopen them by real path, and —
22 * critically — the ordinary JS packages those wrappers require. An external
23 * package is loaded from the real filesystem, so its own `require` also
24 * resolves from the real filesystem; a dependency that was bundled away exists
25 * only inside the emitted bundle and is unreachable there. This closure is
26 * enforced by a test, not by inspection.
27 */
28export const CLI_RUNTIME_EXTERNAL_PREFIXES = [
29 "node-pty",
30 "ffi-rs",
31 "@yuuang/",
32 "@ff-labs/",
33 "@clerk/electron-passkeys",
34 "@msgpackr-extract/",
35 "msgpackr-extract",
36 "node-gyp-build",
37 "node-addon-api",
38 // Required by node-gyp-build-optional-packages. Not native, but in the
39 // closure: without it, WSL gets MODULE_NOT_FOUND while Windows is fine.
40 "detect-libc",
41 // ws's optional accelerators. Nothing in this repo declares them, so they are
42 // not in the staged production install and the packaged app does not ship
43 // them either way -- ws wraps the require in try/catch and falls back to its
44 // JS paths. They are listed because they were being inlined from the dev
45 // store: both carry binding.gyp and prebuilds and load through
46 // node-gyp-build, and a native loader inlined into a bundle chunk searches
47 // for prebuilds that cannot be beside it. Listing them keeps that from
48 // becoming real if either is ever declared as a dependency.
49 "bufferutil",
50 "utf-8-validate",
51] as const;
52
53/**
54 * External only so the bundler never has to resolve them.
55 *
56 * These are reached through a runtime-conditional dynamic import that Node
57 * never takes, and they resolve `bun:*` specifiers that do not exist when
58 * bundling for Node. Because Node never loads them, their dependency closure
59 * does not need to be external — only the entry point must stay unbundled.
60 */
61export const CLI_BUILD_ONLY_EXTERNAL_PREFIXES = [
62 "@effect/platform-bun",
63 "@effect/sql-sqlite-bun",
64] as const;
65
66export const CLI_EXTERNAL_PACKAGE_PREFIXES = [
67 ...CLI_RUNTIME_EXTERNAL_PREFIXES,
68 ...CLI_BUILD_ONLY_EXTERNAL_PREFIXES,
69] as const;
70
71export function isRuntimeExternalCliDependency(id: string): boolean {
72 return CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix));
73}
74
75/**
76 * True when `id` must stay out of the bundle.
77 *
78 * This has to be wired to the bundler's `neverBundle`, not just to
79 * `alwaysBundle`. `alwaysBundle` only forces packages IN — returning false from
80 * it means "no opinion", and the default then applies: a declared dependency
81 * stays external, but a transitive one gets bundled. That is how
82 * msgpackr-extract, node-gyp-build-optional-packages and detect-libc ended up
83 * inlined while node-pty (a declared dependency) stayed external.
84 */
85export function isExternalCliDependency(id: string): boolean {
86 return CLI_EXTERNAL_PACKAGE_PREFIXES.some((prefix) => id.startsWith(prefix));
87}
88
89/** True when the CLI bundle should inline `id` rather than leave it external. */
90export function shouldBundleCliDependency(id: string): boolean {
91 if (id.startsWith("node:")) return false;
92 return !isExternalCliDependency(id);
93}
94
95/** Select direct dependency roots whose runtime closure belongs in the sidecar. */
96export function selectCliRuntimeExternalDependencies(
97 dependencies: Readonly<Record<string, string>>,
98): Record<string, string> {
99 return Object.fromEntries(
100 Object.entries(dependencies).filter(([name]) => isRuntimeExternalCliDependency(name)),
101 );
102}
103
104/**
105 * Scan an emitted bundle chunk for runtime-external packages that were inlined.
106 *
107 * Configuring the bundler is not the same as checking what it produced. The
108 * `alwaysBundle` predicate only forces packages IN; returning false from it
109 * means "no opinion", so a transitive dependency still gets bundled by default.
110 * msgpackr-extract, node-gyp-build-optional-packages and detect-libc were
111 * inlined that way while every list-based test passed, which is why this reads
112 * the artifact instead.
113 *
114 * `regionCount` is reported so the caller can tell "nothing was inlined" apart
115 * from "the marker format changed and this scan no longer sees anything".
116 *
117 * `inlinedPackages` is every package seen in a region, which lets the caller
118 * check the opposite direction too. Verifying only that externals are absent
119 * would still pass if the bundler reverted to leaving everything external: the
120 * scan would see source-file regions, report nothing inlined, and the packaged
121 * backends would then fail with ERR_MODULE_NOT_FOUND because those packages
122 * are not in the selected sidecar closure either.
123 */
124export function findInlinedExternalPackages(source: string): {
125 readonly regionCount: number;
126 readonly inlined: ReadonlyArray<string>;
127 readonly inlinedPackages: ReadonlyArray<string>;
128} {
129 // Rolldown marks each inlined module with a `//#region <path>` comment.
130 const regionPattern = /\/\/#region\s+(\S+)/g;
131 const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
132
133 let regionCount = 0;
134 const inlined = new Set<string>();
135 const inlinedPackages = new Set<string>();
136 for (const region of source.matchAll(regionPattern)) {
137 regionCount += 1;
138 const regionPath = region[1] ?? "";
139 for (const candidate of regionPath.matchAll(packagePattern)) {
140 const name = candidate[1];
141 if (name === undefined || name === ".pnpm") continue;
142 inlinedPackages.add(name);
143 if (isExternalCliDependency(name)) inlined.add(name);
144 }
145 }
146
147 return {
148 regionCount,
149 inlined: [...inlined].sort(),
150 inlinedPackages: [...inlinedPackages].sort(),
151 };
152}