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