compilenamespacenamespace compileA program built into one binary with the pinned Bun (commons 0001-system FR-013): bun build --compile, for this computer or for a target given (linux-x64, linux-arm64, darwin-x64, darwin-arm64, windows-x64), with the files it embeds, giving the binary's path and SHA-256. Bun is found as bun on PATH, or given by its path; where an expected version is given, a Bun of another version is refused, so a build is made only by the pinned one. Everything that can go wrong is a CompileError in a Result.
The argument vector is made by compileArgv, a pure function, so what is asked of Bun can be checked without running it; a program built this way runs itself again with proc.selfCommand() (FR-053).
import { compile } from "./compile.ts";
const r = await compile({ entry: "src/main.ts", outfile: "dist/tool", target: "windows-x64" });
if (r.kind === "ok") console.log(r.value.path, r.value.sha256); // dist/tool.exe, its SHA-256compilenamespacenamespace compilecompile.Targettypetype Target = "linux-x64" | "linux-arm64" | "darwin-x64" | "darwin-arm64" | "windows-x64";A system Bun builds a binary for.
compile.TARGETSconstconst TARGETS: readonly Target[]Every target, in a fixed order.
compile.targetOffunctionfunction targetOf(os: Os, arch: Arch): Target | undefinedThe target of a system, by proc.build's names; undefined where Bun builds for no such system.
compile.hostTargetfunctionfunction hostTarget(): Target | undefinedThe target of this computer; undefined where Bun builds for no such system.
compile.isWindowsfunctionfunction isWindows(target: Target): booleanWhether a target's binaries are Windows programs, named with .exe.
type CompileOptions = {
/** The program's entry point (a `.ts` or `.js` file). */
readonly entry: string;
/** Wher… (1011 characters)type CompileOptions = {
/** The program's entry point (a `.ts` or `.js` file). */
readonly entry: string;
/** Where the binary goes; `.exe` is added for Windows where it is not there, as Bun adds it. */
readonly outfile: string;
/** The system to build for; this computer where it is not given. */
readonly target?: Target;
/** Files embedded in the binary, beside the code its imports bring in. */
readonly files?: readonly string[];
/** Bun's path; `bun` on `PATH` where it is not given. */
readonly bun?: string;
/** The version Bun must be (the pin, such as `1.4.4`); any version where it is not given. */
readonly bunVersion?: string;
/** The folder the build runs in, against which relative paths are read; the process's working folder by default. */
readonly cwd?: string;
/** More of `bun build`'s options, put before the entry point (`--minify`, `--define=...`). */
readonly extraArgs?: readonly string[];
/** Stopping the build. */
readonly signal?: AbortSignal;
};What to build.
compile.Compiledtypetype Compiled = {
/** Its path, absolute. */
readonly path: string;
/** Its SHA-256. */
readonly sha256: Sha256;
/** The system it is for. */
readonly target: Target;
/** The version of Bun that built it. */
readonly bunVersion: string;
};A binary built.
type CompileError =
| { readonly kind: "no-bun"; readonly message: string }
| { readonly kind: "wrong-bun"; readonly expected: string; readonly found: string }
| { readonly kind: "no-target"; readonly message: string }
| { readonly kind: "failed"; readonly code: number; readonly stderr: string }
| { readonly kind: "no-binary"; readonly path: string };Why a binary was not built.
compile.describeCompileErrorfunctionfunction describeCompileError(e: CompileError): stringA sentence for a CompileError.
compile.binaryPathfunctionfunction binaryPath(outfile: string, target: Target): stringThe binary's path for a target: outfile, with .exe added for Windows where it is not there.
compile.compileArgvfunctionfunction compileArgv(bun: string, options: CompileOptions, outfile: string): readonly string[]The argument vector that builds the binary (pure): bun build --compile [--target=bun-<target>] --outfile=<path> [extra...] <entry> [files...]. A target is named only where one is given; without it Bun builds for the computer it runs on. The files after the entry point are embedded in the binary.
compile.compilefunctionfunction compile(options: CompileOptions): Promise<Result<Compiled, CompileError>>Builds a program into one binary with Bun (FR-062) and gives its path and SHA-256; a CompileError where it cannot.
Targettypetype Target = "linux-x64" | "linux-arm64" | "darwin-x64" | "darwin-arm64" | "windows-x64";A system Bun builds a binary for.
TARGETSconstconst TARGETS: readonly Target[]Every target, in a fixed order.
targetOffunctionfunction targetOf(os: Os, arch: Arch): Target | undefinedThe target of a system, by proc.build's names; undefined where Bun builds for no such system.
hostTargetfunctionfunction hostTarget(): Target | undefinedThe target of this computer; undefined where Bun builds for no such system.
isWindowsfunctionfunction isWindows(target: Target): booleanWhether a target's binaries are Windows programs, named with .exe.
CompileOptionstypeno test imports ittype CompileOptions = {
/** The program's entry point (a `.ts` or `.js` file). */
readonly entry: string;
/** Wher… (1011 characters)type CompileOptions = {
/** The program's entry point (a `.ts` or `.js` file). */
readonly entry: string;
/** Where the binary goes; `.exe` is added for Windows where it is not there, as Bun adds it. */
readonly outfile: string;
/** The system to build for; this computer where it is not given. */
readonly target?: Target;
/** Files embedded in the binary, beside the code its imports bring in. */
readonly files?: readonly string[];
/** Bun's path; `bun` on `PATH` where it is not given. */
readonly bun?: string;
/** The version Bun must be (the pin, such as `1.4.4`); any version where it is not given. */
readonly bunVersion?: string;
/** The folder the build runs in, against which relative paths are read; the process's working folder by default. */
readonly cwd?: string;
/** More of `bun build`'s options, put before the entry point (`--minify`, `--define=...`). */
readonly extraArgs?: readonly string[];
/** Stopping the build. */
readonly signal?: AbortSignal;
};What to build.
Compiledtypeno test imports ittype Compiled = {
/** Its path, absolute. */
readonly path: string;
/** Its SHA-256. */
readonly sha256: Sha256;
/** The system it is for. */
readonly target: Target;
/** The version of Bun that built it. */
readonly bunVersion: string;
};A binary built.
CompileErrortypetype CompileError =
| { readonly kind: "no-bun"; readonly message: string }
| { readonly kind: "wrong-bun"; readonly expected: string; readonly found: string }
| { readonly kind: "no-target"; readonly message: string }
| { readonly kind: "failed"; readonly code: number; readonly stderr: string }
| { readonly kind: "no-binary"; readonly path: string };Why a binary was not built.
describeCompileErrorfunctionfunction describeCompileError(e: CompileError): stringA sentence for a CompileError.
binaryPathfunctionfunction binaryPath(outfile: string, target: Target): stringThe binary's path for a target: outfile, with .exe added for Windows where it is not there.
compileArgvfunctionfunction compileArgv(bun: string, options: CompileOptions, outfile: string): readonly string[]The argument vector that builds the binary (pure): bun build --compile [--target=bun-<target>] --outfile=<path> [extra...] <entry> [files...]. A target is named only where one is given; without it Bun builds for the computer it runs on. The files after the entry point are embedded in the binary.