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

+49 -1
View File
@@ -15,6 +15,7 @@ import {
resolveWebAssetBrandForPackageVersion,
resolveWebIconOverrides,
} from "../../../scripts/lib/brand-assets.ts";
import { findEsmImportsOfExternalPackages } from "../../../scripts/lib/cli-external-packages.ts";
import { resolveCatalogDependencies } from "../../../scripts/lib/resolve-catalog.ts";
import { fromJsonStringPretty } from "@t3tools/shared/schemaJson";
import { fromYaml } from "@t3tools/shared/schemaYaml";
@@ -25,6 +26,7 @@ import {
ServerCliCommandExitError,
ServerCliDevelopmentIconSourceMissingError,
ServerCliDevelopmentIconTargetMissingError,
ServerCliExecutableImportError,
ServerCliPublishIconSourceMissingError,
ServerCliPublishIconTargetMissingError,
} from "./cliErrors.ts";
@@ -175,6 +177,52 @@ const buildCmd = Command.make(
}),
).pipe(Command.withDescription("Build the server package (tsdown + bundle web client)."));
// ---------------------------------------------------------------------------
// build-exe subcommand
// ---------------------------------------------------------------------------
const buildExeCmd = Command.make(
"build-exe",
{
verbose: Flag.boolean("verbose").pipe(Flag.withDefault(false)),
},
(config) =>
Effect.gen(function* () {
const path = yield* Path.Path;
const fs = yield* FileSystem.FileSystem;
const repoRoot = yield* RepoRoot;
const serverDir = path.join(repoRoot, "apps/server");
yield* Effect.log("[cli] Building single-executable...");
const spawnCommand = yield* resolveSpawnCommand("vp", ["pack"]);
yield* runCommand(
ChildProcess.make(spawnCommand.command, spawnCommand.args, {
cwd: serverDir,
env: { ...process.env, T3CODE_PACK_EXE: "1" },
stdout: config.verbose ? "inherit" : "ignore",
stderr: "inherit",
shell: spawnCommand.shell,
}),
);
// The executable can only `import` built-ins. A file-backed import
// passes the bundler and `node dist/bin.mjs`, then throws inside the
// binary, so read the emitted module graph rather than trusting config.
const bundlePath = path.join(serverDir, "dist-exe/bin.mjs");
const specifiers = findEsmImportsOfExternalPackages(yield* fs.readFileString(bundlePath));
if (specifiers.length > 0) {
return yield* new ServerCliExecutableImportError({ bundlePath, specifiers });
}
yield* Effect.log(
"[cli] Built dist-exe/t3 (expects client/, resource-monitor/, and the runtime-external node_modules beside it; scripts/build-cli-archive.ts assembles that tree)",
);
}),
).pipe(
Command.withDescription(
"Build the server as a Node single-executable (needs a Node 25.7+ host for --build-sea). The binary still resolves native packages from a node_modules tree beside it.",
),
);
// ---------------------------------------------------------------------------
// publish subcommand
// ---------------------------------------------------------------------------
@@ -309,7 +357,7 @@ const publishCmd = Command.make(
const cli = Command.make("cli").pipe(
Command.withDescription("T3 server build & publish CLI."),
Command.withSubcommands([buildCmd, publishCmd]),
Command.withSubcommands([buildCmd, buildExeCmd, publishCmd]),
);
Command.run(cli, { version: "0.0.0" }).pipe(
+12
View File
@@ -68,3 +68,15 @@ export class ServerCliBuildAssetMissingError extends Schema.TaggedError<ServerCl
return `Missing build asset: ${this.assetPath}. Run the build subcommand first.`;
}
}
export class ServerCliExecutableImportError extends Schema.TaggedError<ServerCliExecutableImportError>()(
"ServerCliExecutableImportError",
{
bundlePath: Schema.String,
specifiers: Schema.Array(Schema.String),
},
) {
override get message(): string {
return `${this.bundlePath} imports file-backed packages that a single-executable cannot resolve: ${this.specifiers.join(", ")}. Load them through createRequire instead.`;
}
}