+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
151 lines
6.1 KiB
TypeScript
151 lines
6.1 KiB
TypeScript
/**
|
|
* The single source of truth for packages the server CLI bundle must NOT inline.
|
|
*
|
|
* Two consumers derive from this list, and they must never disagree:
|
|
*
|
|
* - apps/server/vite.config.ts decides what stays external to the bundle.
|
|
* - scripts/build-desktop-artifact.ts selects the runtime dependency roots for
|
|
* the Windows server sidecar.
|
|
*
|
|
* A runtime package that is external but absent from the sidecar fails as soon
|
|
* as Node resolves it from the emitted bundle. Keeping both consumers on one
|
|
* list prevents packaging from drifting away from the bundle boundary.
|
|
*
|
|
* Entries are matched as prefixes (`id.startsWith(prefix)`), so they also cover
|
|
* a package's platform-specific siblings — `node-gyp-build` covers
|
|
* `node-gyp-build-optional-packages`, `@yuuang/` covers every `ffi-rs-*` binding.
|
|
*/
|
|
/**
|
|
* External because Node actually loads them from disk at runtime.
|
|
*
|
|
* Native addons (.node), the JS wrappers that dlopen them by real path, and —
|
|
* critically — the ordinary JS packages those wrappers require. An external
|
|
* package is loaded from the real filesystem, so its own `require` also
|
|
* resolves from the real filesystem; a dependency that was bundled away exists
|
|
* only inside the emitted bundle and is unreachable there. This closure is
|
|
* enforced by a test, not by inspection.
|
|
*/
|
|
export const CLI_RUNTIME_EXTERNAL_PREFIXES = [
|
|
// Cursor ships computed Webpack imports and platform helper packages.
|
|
"@cursor/sdk",
|
|
"node-pty",
|
|
"ffi-rs",
|
|
"@yuuang/",
|
|
"@ff-labs/",
|
|
"@napi-rs/keyring",
|
|
"@clerk/electron-passkeys",
|
|
"node-gyp-build",
|
|
"node-addon-api",
|
|
// ws's optional accelerators. Nothing in this repo declares them, so they are
|
|
// not in the staged production install and the packaged app does not ship
|
|
// them either way -- ws wraps the require in try/catch and falls back to its
|
|
// JS paths. They are listed because they were being inlined from the dev
|
|
// store: both carry binding.gyp and prebuilds and load through
|
|
// node-gyp-build, and a native loader inlined into a bundle chunk searches
|
|
// for prebuilds that cannot be beside it. Listing them keeps that from
|
|
// becoming real if either is ever declared as a dependency.
|
|
"bufferutil",
|
|
"utf-8-validate",
|
|
] as const;
|
|
|
|
// These are Cursor's disk-backed dependency closure. Match package boundaries
|
|
// so "zod" does not also externalize unrelated packages such as zod-to-json-schema.
|
|
const CURSOR_RUNTIME_DEPENDENCIES = [
|
|
"@bufbuild/protobuf",
|
|
"@connectrpc/connect",
|
|
"@connectrpc/connect-node",
|
|
"@connectrpc/connect-web",
|
|
"@statsig/js-client",
|
|
"@statsig/client-core",
|
|
"zod",
|
|
"undici",
|
|
"@fastify/busboy",
|
|
] as const;
|
|
|
|
export function isRuntimeExternalCliDependency(id: string): boolean {
|
|
return (
|
|
CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix)) ||
|
|
CURSOR_RUNTIME_DEPENDENCIES.some((name) => id === name || id.startsWith(`${name}/`))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* True when `id` must stay out of the bundle.
|
|
*
|
|
* This has to be wired to the bundler's `neverBundle`, not just to
|
|
* `alwaysBundle`. `alwaysBundle` only forces packages IN — returning false from
|
|
* it means "no opinion", and the default then applies: a declared dependency
|
|
* stays external, but a transitive one gets bundled. That is how a native
|
|
* loader such as node-gyp-build ended up inlined while node-pty (a declared
|
|
* dependency) stayed external.
|
|
*/
|
|
export function isExternalCliDependency(id: string): boolean {
|
|
return isRuntimeExternalCliDependency(id);
|
|
}
|
|
|
|
/** True when the CLI bundle should inline `id` rather than leave it external. */
|
|
export function shouldBundleCliDependency(id: string): boolean {
|
|
if (id.startsWith("node:")) return false;
|
|
return !isExternalCliDependency(id);
|
|
}
|
|
|
|
/** Select direct dependency roots whose runtime closure belongs in the sidecar. */
|
|
export function selectCliRuntimeExternalDependencies(
|
|
dependencies: Readonly<Record<string, string>>,
|
|
): Record<string, string> {
|
|
return Object.fromEntries(
|
|
Object.entries(dependencies).filter(([name]) => isRuntimeExternalCliDependency(name)),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Scan an emitted bundle chunk for runtime-external packages that were inlined.
|
|
*
|
|
* Configuring the bundler is not the same as checking what it produced. The
|
|
* `alwaysBundle` predicate only forces packages IN; returning false from it
|
|
* means "no opinion", so a transitive dependency still gets bundled by default.
|
|
* A native loader and its helper (node-gyp-build-optional-packages and
|
|
* detect-libc, when msgpackr-extract was still a dependency) were inlined that
|
|
* way while every list-based test passed, which is why this reads the artifact
|
|
* instead.
|
|
*
|
|
* `regionCount` is reported so the caller can tell "nothing was inlined" apart
|
|
* from "the marker format changed and this scan no longer sees anything".
|
|
*
|
|
* `inlinedPackages` is every package seen in a region, which lets the caller
|
|
* check the opposite direction too. Verifying only that externals are absent
|
|
* would still pass if the bundler reverted to leaving everything external: the
|
|
* scan would see source-file regions, report nothing inlined, and the packaged
|
|
* backends would then fail with ERR_MODULE_NOT_FOUND because those packages
|
|
* are not in the selected sidecar closure either.
|
|
*/
|
|
export function findInlinedExternalPackages(source: string): {
|
|
readonly regionCount: number;
|
|
readonly inlined: ReadonlyArray<string>;
|
|
readonly inlinedPackages: ReadonlyArray<string>;
|
|
} {
|
|
// Rolldown marks each inlined module with a `//#region <path>` comment.
|
|
const regionPattern = /\/\/#region\s+(\S+)/g;
|
|
const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
|
|
|
|
let regionCount = 0;
|
|
const inlined = new Set<string>();
|
|
const inlinedPackages = new Set<string>();
|
|
for (const region of source.matchAll(regionPattern)) {
|
|
regionCount += 1;
|
|
const regionPath = region[1] ?? "";
|
|
for (const candidate of regionPath.matchAll(packagePattern)) {
|
|
const name = candidate[1];
|
|
if (name === undefined || name === ".pnpm") continue;
|
|
inlinedPackages.add(name);
|
|
if (isExternalCliDependency(name)) inlined.add(name);
|
|
}
|
|
}
|
|
|
|
return {
|
|
regionCount,
|
|
inlined: [...inlined].sort(),
|
|
inlinedPackages: [...inlinedPackages].sort(),
|
|
};
|
|
}
|