build(server): make the CLI bundle loadable as a Node single-executable (#11316)

Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Fable 5 authored and GitHub committed 2026-09-13 21:24:20 -07:00
1 parent 683aa87096
commit 06de59b3d6
20 files changed
+255 -407

No files matched your search

+39 -6
View File
@@ -11,6 +11,7 @@ import serverPackageJson from "../../apps/server/package.json" with { type: "jso
import {
CLI_RUNTIME_EXTERNAL_PREFIXES,
findEsmImportsOfExternalPackages,
findInlinedExternalPackages,
selectCliRuntimeExternalDependencies,
shouldBundleCliDependency,
@@ -55,11 +56,6 @@ describe("shouldBundleCliDependency", () => {
}
});
it("leaves bun-only entry points external", () => {
assert.strictEqual(shouldBundleCliDependency("@effect/platform-bun"), false);
assert.strictEqual(shouldBundleCliDependency("@effect/sql-sqlite-bun"), false);
});
// The real package is `node-gyp-build-optional-packages`, reached by prefix.
// It is transitive to a selected dependency root, so the runtime closure test
// below ensures it follows that root into the sidecar.
@@ -72,7 +68,6 @@ describe("selectCliRuntimeExternalDependencies", () => {
it("keeps only runtime-external dependency roots for the Windows sidecar", () => {
assert.deepStrictEqual(
selectCliRuntimeExternalDependencies({
"@effect/platform-bun": "1.0.0",
"@ff-labs/fff-node": "2.0.0",
effect: "3.0.0",
"node-pty": "4.0.0",
@@ -281,3 +276,41 @@ var x = 1;
assert.deepStrictEqual(result.inlined, []);
});
});
// The single-executable build can only `import` built-ins. A file-backed
// import of an external package passes every bundler check and the regular
// `node dist/bin.mjs` path, then fails inside the executable, so the scan
// reads the emitted module graph instead.
describe("findEsmImportsOfExternalPackages", () => {
it("flags static and dynamic imports of file-backed packages", () => {
const source = [
'import { FileFinder } from "@ff-labs/fff-node";',
'import * as fs from "fs";',
'import { createRequire } from "node:module";',
'const pty = () => import("node-pty");',
'const data = () => import("@ff-labs/fff-bin-linux-x64-gnu", { with: { type: "json" } });',
'const lazy = () => import(/* @vite-ignore */ "ffi-rs");',
'const local = () => import("./chunk-abc.mjs");',
].join("\n");
assert.deepStrictEqual(findEsmImportsOfExternalPackages(source), [
"@ff-labs/fff-bin-linux-x64-gnu",
"@ff-labs/fff-node",
"ffi-rs",
"node-pty",
]);
});
it("flags side-effect imports and re-exports too", () => {
const source = ['import "msgpackr-extract";', 'export { load } from "ffi-rs";'].join("\n");
assert.deepStrictEqual(findEsmImportsOfExternalPackages(source), [
"ffi-rs",
"msgpackr-extract",
]);
});
it("does not mistake createRequire calls for imports", () => {
const source = 'const { FileFinder } = createRequire(import.meta.url)("@ff-labs/fff-node");';
assert.deepStrictEqual(findEsmImportsOfExternalPackages(source), []);
});
});
+38 -19
View File
@@ -1,3 +1,6 @@
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeModule from "node:module";
/**
* The single source of truth for packages the server CLI bundle must NOT inline.
*
@@ -50,24 +53,6 @@ export const CLI_RUNTIME_EXTERNAL_PREFIXES = [
"utf-8-validate",
] as const;
/**
* External only so the bundler never has to resolve them.
*
* These are reached through a runtime-conditional dynamic import that Node
* never takes, and they resolve `bun:*` specifiers that do not exist when
* bundling for Node. Because Node never loads them, their dependency closure
* does not need to be external — only the entry point must stay unbundled.
*/
export const CLI_BUILD_ONLY_EXTERNAL_PREFIXES = [
"@effect/platform-bun",
"@effect/sql-sqlite-bun",
] as const;
export const CLI_EXTERNAL_PACKAGE_PREFIXES = [
...CLI_RUNTIME_EXTERNAL_PREFIXES,
...CLI_BUILD_ONLY_EXTERNAL_PREFIXES,
] as const;
export function isRuntimeExternalCliDependency(id: string): boolean {
return CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix));
}
@@ -83,7 +68,7 @@ export function isRuntimeExternalCliDependency(id: string): boolean {
* inlined while node-pty (a declared dependency) stayed external.
*/
export function isExternalCliDependency(id: string): boolean {
return CLI_EXTERNAL_PACKAGE_PREFIXES.some((prefix) => id.startsWith(prefix));
return isRuntimeExternalCliDependency(id);
}
/** True when the CLI bundle should inline `id` rather than leave it external. */
@@ -101,6 +86,40 @@ export function selectCliRuntimeExternalDependencies(
);
}
/**
* Scan an emitted bundle chunk for ESM imports of packages that are not Node
* built-ins.
*
* Inside a Node single-executable, `import` statements and `import()` can only
* resolve built-in modules; any file-backed specifier throws at module
* evaluation (static) or at first use (dynamic). External packages therefore
* have to be reached through `createRequire`, which reads the real filesystem
* in every runtime. The bundler cannot enforce this, so the check reads what it
* produced.
*/
export function findEsmImportsOfExternalPackages(source: string): ReadonlyArray<string> {
const specifiers = new Set<string>();
// `import x from`, `import "side-effect"`, `export ... from`, and `import()`
// all resolve through the module loader.
const patterns = [
/^import\s[^;]*?\sfrom\s+["']([^"']+)["']/gm,
/^import\s+["']([^"']+)["']/gm,
/^export\s[^;]*?\sfrom\s+["']([^"']+)["']/gm,
// Rolldown may leave a `/* @vite-ignore */` style comment before the specifier.
/\bimport\(\s*(?:\/\*[\s\S]*?\*\/\s*)*["']([^"']+)["']\s*[,)]/g,
];
for (const pattern of patterns) {
for (const match of source.matchAll(pattern)) {
const specifier = match[1];
if (specifier === undefined) continue;
if (NodeModule.isBuiltin(specifier)) continue;
if (specifier.startsWith("./") || specifier.startsWith("../")) continue;
specifiers.add(specifier);
}
}
return [...specifiers].sort();
}
/**
* Scan an emitted bundle chunk for runtime-external packages that were inlined.
*