Skip to content
bun-bundler logo

Bun Bundler

bun-bundler

Use when bundling JavaScript/TypeScript code with Bun's fast bundler. Covers building for different targets, tree-shaking, code splitting, and optimization strategies.

TheBushidoCollective/han0installs199stars

SKILL.md

Full skill instructions

Bun Bundler

Use this skill when bundling JavaScript/​TypeScript applications with Bun's built-in bundler, which provides exceptional performance and modern features.

Key Concepts

Basic Bundling

Bun can bundle your code for different targets:

# Bundle for Bun runtime
bun build ./​src/​index.ts --outdir ./​dist

# Bundle for browsers
bun build ./​src/​index.ts --outdir ./​dist --target=browser

# Bundle for Node.js
bun build ./​src/​index.ts --outdir ./​dist --target=node

# Minify output
bun build ./​src/​index.ts --outdir ./​dist --minify

Programmatic API

Use Bun's build API in TypeScript:

import { build } from "bun";

await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  target: "bun",
  minify: true,
  sourcemap: "external",
});

Build Targets

Bun supports multiple build targets:

  • bun - Optimized for Bun runtime (default)
  • browser - Browser-compatible bundle
  • node - Node.js-compatible bundle

Output Formats

Control output format:

await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  format: "esm", // or "cjs", "iife"
});

Best Practices

Entry Points Configuration

Define multiple entry points for complex applications:

await build({
  entrypoints: [
    "./​src/​client/​index.ts",
    "./​src/​server/​index.ts",
    "./​src/​worker.ts",
  ],
  outdir: "./​dist",
  naming: {
    entry: "[dir]/[name].[ext]",
    chunk: "[name]-[hash].[ext]",
    asset: "assets/[name]-[hash].[ext]",
  },
});

Tree Shaking

Bun automatically tree-shakes unused code:

// utils.ts
export function used() {
  return "used";
}

export function unused() {
  return "unused";
}

// index.ts
import { used } from "./​utils";

console.log(used()); // Only 'used' function will be bundled

Code Splitting

Split code for better loading performance:

await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  splitting: true, // Enable code splitting
  target: "browser",
});

Environment Variables

Replace environment variables at build time:

await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  define: {
    "process.env.API_URL": JSON.stringify("https://api.example.com"),
    "process.env.NODE_ENV": JSON.stringify("production"),
  },
});

External Dependencies

Mark dependencies as external to exclude from bundle:

await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  external: ["react", "react-dom"], // Don't bundle React
  target: "browser",
});

Source Maps

Generate source maps for debugging:

await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  sourcemap: "external", // or "inline", "none"
  minify: true,
});

Common Patterns

Building a Library

// build.ts
import { build } from "bun";

// ESM build
await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist/​esm",
  format: "esm",
  target: "node",
  minify: true,
  sourcemap: "external",
});

// CJS build
await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist/​cjs",
  format: "cjs",
  target: "node",
  minify: true,
  sourcemap: "external",
});

console.log("Build complete!");

Building a Web Application

// build.ts
import { build } from "bun";

await build({
  entrypoints: ["./​src/​index.tsx"],
  outdir: "./​dist",
  target: "browser",
  format: "esm",
  minify: true,
  splitting: true,
  sourcemap: "external",
  publicPath: "/​assets/",
  naming: {
    entry: "[dir]/[name].[ext]",
    chunk: "[name]-[hash].[ext]",
    asset: "assets/[name]-[hash].[ext]",
  },
  define: {
    "process.env.NODE_ENV": JSON.stringify("production"),
  },
});

Building Multiple Outputs

// build.ts
import { build } from "bun";

const builds = [
  {
    entrypoints: ["./​src/​index.ts"],
    outdir: "./​dist/​esm",
    format: "esm" as const,
    target: "browser" as const,
  },
  {
    entrypoints: ["./​src/​index.ts"],
    outdir: "./​dist/​cjs",
    format: "cjs" as const,
    target: "node" as const,
  },
  {
    entrypoints: ["./​src/​index.ts"],
    outdir: "./​dist/​iife",
    format: "iife" as const,
    target: "browser" as const,
  },
];

for (const config of builds) {
  await build({
    ...config,
    minify: true,
    sourcemap: "external",
  });
}

console.log("All builds complete!");

Build Script with Watching

// watch-build.ts
import { watch } from "fs";
import { build } from "bun";

async function buildApp() {
  console.log("Building...");
  await build({
    entrypoints: ["./​src/​index.ts"],
    outdir: "./​dist",
    target: "bun",
  });
  console.log("Build complete!");
}

// Initial build
await buildApp();

// Watch for changes
watch("./​src", { recursive: true }, async (event, filename) => {
  console.log(`File changed: ${filename}`);
  await buildApp();
});

Plugin System

Create custom build plugins:

import type { BunPlugin } from "bun";

const myPlugin: BunPlugin = {
  name: "my-plugin",
  setup(build) {
    build.onLoad({ filter: /\.custom$/ }, async (args) => {
      const text = await Bun.file(args.path).text();
      return {
        contents: `export default ${JSON.stringify(text)}`,
        loader: "js",
      };
    });
  },
};

await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  plugins: [myPlugin],
});

Anti-Patterns

Don't Bundle Node Modules for Node Target

// Bad - Bundling all dependencies for Node
await build({
  entrypoints: ["./​src/​server.ts"],
  target: "node",
  // Missing external configuration
});

// Good - Mark dependencies as external
await build({
  entrypoints: ["./​src/​server.ts"],
  target: "node",
  external: ["express", "mongoose", "*"], // "*" excludes all node_modules
});

Don't Ignore Build Errors

// Bad - No error handling
await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
});

// Good - Handle build errors
try {
  const result = await build({
    entrypoints: ["./​src/​index.ts"],
    outdir: "./​dist",
  });

  if (!result.success) {
    console.error("Build failed");
    process.exit(1);
  }

  console.log("Build succeeded");
} catch (error) {
  console.error("Build error:", error);
  process.exit(1);
}

Don't Minify Development Builds

// Bad - Always minifying
await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  minify: true, // Hard to debug in development
});

// Good - Conditional minification
const isDev = Bun.env.NODE_ENV === "development";

await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  minify: !isDev,
  sourcemap: isDev ? "inline" : "external",
});

Don't Over-Split Code

// Bad - Excessive code splitting
await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  splitting: true,
  target: "bun", // Code splitting not needed for Bun target
});

// Good - Split only when beneficial
await build({
  entrypoints: ["./​src/​index.ts"],
  outdir: "./​dist",
  splitting: true,
  target: "browser", // Beneficial for browsers
});

Related Skills

  • bun-runtime: Understanding Bun's runtime for target optimization
  • bun-package-manager: Managing build dependencies
  • bun-testing: Testing bundled code

More skills from TheBushidoCollective

typescript-utility-types logo
TheBushidoCollective/han

typescript-utility-types

Use when typeScript utility types, mapped types, and advanced type manipulation. Use when creating flexible, type-safe TypeScript code.

199 0
View
shell-portability logo
TheBushidoCollective/han

shell-portability

Use when writing shell scripts that need to run across different systems, shells, or environments. Covers POSIX compatibility and platform differences.

199 0
View
tailwind-performance logo
TheBushidoCollective/han

tailwind-performance

Use when optimizing Tailwind CSS for production, reducing bundle size, and improving performance. Covers PurgeCSS, JIT mode, and build optimization.

199 0
View
figma-generate-component logo
TheBushidoCollective/han

figma-generate-component

Generate production-ready code from a Figma component or frame using the Figma MCP server

199 0
View
dialyzer-integration logo
TheBushidoCollective/han

dialyzer-integration

Use when integrating Dialyzer into development workflows and CI/CD pipelines for Erlang/Elixir projects.

199 0
View
rubocop-configuration logo
TheBushidoCollective/han

rubocop-configuration

Use when ruboCop configuration including YAML config, departments, inheritance, and customization.

199 0
View
elixir-otp-patterns logo
TheBushidoCollective/han

elixir-otp-patterns

Use when Elixir OTP patterns including GenServer, Supervisor, Agent, and Task. Use when building concurrent, fault-tolerant Elixir applications.

199 0
View
ameba-custom-rules logo
TheBushidoCollective/han

ameba-custom-rules

Use when creating custom Ameba rules for Crystal code analysis including rule development, AST traversal, issue reporting, and rule testing.

199 0
View
Swift Optionals Patterns logo
TheBushidoCollective/han

Swift Optionals Patterns

Use when swift's optional handling patterns including optional binding, chaining, nil coalescing, and modern approaches to safely working with optional values while avoiding common pitfalls and force unwrapping.

199 0
View
code-annotation-patterns logo
TheBushidoCollective/han

code-annotation-patterns

Use when annotating code with structured metadata, tags, and markers for AI-assisted development workflows. Covers annotation formats, semantic tags, and integration with development tools.

199 0
View
Scala Collections logo
TheBushidoCollective/han

Scala Collections

Use when scala collections including immutable/mutable variants, List, Vector, Set, Map operations, collection transformations, lazy evaluation with views, parallel collections, and custom collection builders for efficient data processing.

199 0
View
reactflow-custom-nodes logo
TheBushidoCollective/han

reactflow-custom-nodes

Use when creating custom React Flow nodes, edges, and handles. Covers custom node components, resizable nodes, toolbars, and advanced customization.

199 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View