agentleFS
addiplusaddiplus/claude-skill-pack/skills/bun-builder/SKILL.md

bun-builder

Skill0 starsChanged 7 months agoMITView on GitHub

Bundle and build JavaScript/TypeScript with Bun's native bundler. Use when user needs to bundle apps, create executables, or optimize builds. Keywords: bun build, bundler, bundle, compile, executable, minify, tree-shake, esbuild alternative.

  • Reads credentials

The file

We split this skill into the rules it gives an agent

10 rules. About 0 are common, 6 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈6

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Check the import path

    line 355
  2. Very specific:

    Use --external for packages not to bundle

    line 356
  3. Very specific:

    Add to external array

    line 357
  4. Distinctive:

    Check for side-effect imports

    line 362
  5. Very specific:

    Use --packages external for libraries

    line 363
  6. Very specific:

    Use --sourcemap=external for production

    line 366
  7. Distinctive:

    Ensure source maps enabled in browser devtools

    line 367
  8. Distinctive:

    Check filter regex pattern

    line 370
  9. Distinctive:

    Ensure namespace matches between onResolve and onLoad

    line 371
  10. Distinctive:

    Consider shipping as regular script for smaller size

    line 377

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Bun Builder

Bun's native bundler is faster than esbuild and webpack, with built-in TypeScript/JSX support and the ability to create standalone executables.

Quick Start

# Bundle for browser
bun build ./src/index.ts --outdir ./dist

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

# Create executable
bun build ./src/cli.ts --compile --outfile myapp

# Minified production build
bun build ./src/index.ts --outdir ./dist --minify

CLI Options

Basic Options

bun build <entrypoints...> [options]

# Entry points
bun build ./src/index.ts
bun build ./src/a.ts ./src/b.ts  # Multiple entries

# Output
--outdir <path>     # Output directory
--outfile <path>    # Single output file (for --compile)

# Target environment
--target browser    # Default, for web browsers
--target bun        # For Bun runtime
--target node       # For Node.js

# Format
--format esm        # ES modules (default)
--format cjs        # CommonJS
--format iife       # Immediately-invoked function

# Optimization
--minify            # Minify output
--minify-whitespace # Minify whitespace only
--minify-syntax     # Minify syntax only
--minify-identifiers # Minify identifiers only

# Source maps
--sourcemap         # Inline source map
--sourcemap=external # Separate .map file
--sourcemap=linked  # Link to external map

# Other
--splitting         # Enable code splitting
--watch             # Watch mode
--define KEY=VALUE  # Define compile-time constants
--external <pkg>    # Exclude package from bundle
--packages external # All node_modules external

Bun.build() API

const result = await Bun.build({
  entrypoints: ["./src/index.ts"],
  outdir: "./dist",
  target: "browser", // "browser" | "bun" | "node"
  format: "esm",     // "esm" | "cjs" | "iife"
  splitting: true,
  sourcemap: "external",
  minify: true,

  // Externals
  external: ["react", "react-dom"],
  packages: "external", // All node_modules

  // Naming
  naming: {
    entry: "[name].[hash].js",
    chunk: "[name]-[hash].js",
    asset: "[name]-[hash][ext]",
  },

  // Define constants
  define: {
    "process.env.NODE_ENV": JSON.stringify("production"),
  },

  // Plugins
  plugins: [myPlugin()],

  // Loaders
  loader: {
    ".svg": "file",
    ".txt": "text",
  },
});

// Check for errors
if (!result.success) {
  console.error("Build failed:");
  for (const log of result.logs) {
    console.error(log);
  }
  process.exit(1);
}

// Output info
for (const output of result.outputs) {
  console.log(`${output.path} (${output.size} bytes)`);
}

Creating Executables

Single-File Executable

# Basic executable
bun build --compile ./src/app.ts --outfile myapp

# With embedded assets
bun build --compile ./src/app.ts --outfile myapp \
  --define "import.meta.dir" "'./assets'"

# Cross-compile
bun build --compile --target=bun-linux-x64 ./app.ts
bun build --compile --target=bun-darwin-arm64 ./app.ts
bun build --compile --target=bun-windows-x64 ./app.ts

Available Targets

TargetPlatformArchitecture
bun-linux-x64Linuxx64
bun-linux-arm64LinuxARM64
bun-darwin-x64macOSx64
bun-darwin-arm64macOSARM64 (Apple Silicon)
bun-windows-x64Windowsx64

Embedding Files

// Files in same directory as script are embedded
const data = await Bun.file(import.meta.dir + "/data.json").text();

// Reference files relative to entry point
import config from "./config.json";
# Build with embedded assets
bun build --compile ./src/app.ts --asset-dir ./assets

Plugins

Plugin Structure

import { BunPlugin } from "bun";

const myPlugin: BunPlugin = {
  name: "my-plugin",
  setup(build) {
    // onResolve: Custom module resolution
    build.onResolve({ filter: /^virtual:.*/ }, (args) => {
      return {
        path: args.path,
        namespace: "virtual",
      };
    });

    // onLoad: Custom loading
    build.onLoad({ filter: /.*/, namespace: "virtual" }, (args) => {
      return {
        contents: `export default "Virtual module: ${args.path}"`,
        loader: "js",
      };
    });
  },
};

// Use in build
await Bun.build({
  entrypoints: ["./src/index.ts"],
  plugins: [myPlugin],
});

YAML Plugin Example

import { BunPlugin } from "bun";
import { parse } from "yaml";

const yamlPlugin: BunPlugin = {
  name: "yaml",
  setup(build) {
    build.onLoad({ filter: /\.ya?ml$/ }, async (args) => {
      const content = await Bun.file(args.path).text();
      const data = parse(content);
      return {
        contents: `export default ${JSON.stringify(data)}`,
        loader: "js",
      };
    });
  },
};

SVG Component Plugin

const svgPlugin: BunPlugin = {
  name: "svg-component",
  setup(build) {
    build.onLoad({ filter: /\.svg$/ }, async (args) => {
      const svg = await Bun.file(args.path).text();
      return {
        contents: `
          export default function SVG(props) {
            return (${svg.replace(/<svg/, "<svg {...props}")});
          }
        `,
        loader: "jsx",
      };
    });
  },
};

Loaders

Built-in loaders for different file types:

LoaderExtensionsOutput
js.js, .mjsJavaScript
jsx.jsxJSX → JavaScript
ts.ts, .mtsTypeScript → JavaScript
tsx.tsxTSX → JavaScript
json.jsonJSON → JS object
toml.tomlTOML → JS object
textAnyRaw text string
fileAnyURL to asset
napi.nodeNative addon
wasm.wasmWebAssembly module
await Bun.build({
  entrypoints: ["./app.ts"],
  loader: {
    ".svg": "file",      // Copy and return URL
    ".txt": "text",      // Import as string
    ".html": "text",     // Import as string
  },
});

Configuration (bunfig.toml)

# bunfig.toml

[build]
outdir = "./dist"
target = "browser"
format = "esm"
sourcemap = "external"
minify = true
splitting = true

[build.external]
packages = ["react", "react-dom"]

[build.define]
"process.env.NODE_ENV" = "'production'"

[build.loader]
".svg" = "file"
".txt" = "text"

Common Build Patterns

React SPA

bun build ./src/index.tsx --outdir ./dist \
  --minify \
  --splitting \
  --sourcemap=external \
  --define "process.env.NODE_ENV='production'"

Node.js Library

bun build ./src/index.ts --outdir ./dist \
  --target node \
  --format esm \
  --packages external \
  --sourcemap

CLI Tool

bun build --compile ./src/cli.ts --outfile mycli \
  --minify \
  --target bun-linux-x64

Multi-Entry Build

await Bun.build({
  entrypoints: [
    "./src/client.ts",
    "./src/worker.ts",
    "./src/admin.ts",
  ],
  outdir: "./dist",
  splitting: true,
  minify: true,
});

Comparison with Alternatives

FeatureBunesbuildVitewebpack
SpeedFastestFastFast (dev)Slow
TypeScriptNativeNativeVia pluginVia loader
ExecutablesYesNoNoNo
Dev serverYesNoYesYes
PluginsSimpleSimpleRollupComplex
Tree shakingYesYesYesYes
Code splittingYesYesYesYes

Troubleshooting

"Cannot resolve module"

  • Check the import path
  • Use --external for packages not to bundle
  • Add to external array

Large bundle size:

  • Enable tree shaking (default)
  • Use --minify
  • Check for side-effect imports
  • Use --packages external for libraries

Source maps not working:

  • Use --sourcemap=external for production
  • Ensure source maps enabled in browser devtools

Plugin not loading files:

  • Check filter regex pattern
  • Ensure namespace matches between onResolve and onLoad
  • Debug with console.log in plugin

Executable too large:

  • Bun runtime is embedded (~90MB base)
  • Use --minify
  • Consider shipping as regular script for smaller size

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.