Programmatic Bundler API in Bun

From the Bun cheat sheet ยท Bundler ยท verified Jul 2026

Programmatic Bundler API

Use Bun.build() in code with plugins and advanced options

typescript
const result = await Bun.build({
  entrypoints: ["./src/index.ts"],
  outdir: "./dist",
  target: "browser",
  minify: true,
});

if (!result.success) {
  console.error(result.logs);
}
๐Ÿ’ก Bun.build() returns artifacts with path, size, and hash โ€” useful for build pipelines
โšก Plugins use the same API as esbuild plugins โ€” most esbuild plugins work in Bun
๐Ÿ“Œ Use env: "inline" to replace process.env references with actual values at build time
๐ŸŸข Check result.success and result.logs to catch build errors programmatically
buildpluginsapi

More Bun tasks

Back to the full Bun cheat sheet