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.
What this skill does
# 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:
```bash
# 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:
```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:
```typescript
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:
```typescript
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:
```typescript
// 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:
```typescript
await build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
splitting: true, // Enable code splitting
target: "browser",
});
```
### Environment Variables
Replace environment variables at build time:
```typescript
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:
```typescript
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:
```typescript
await build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
sourcemap: "external", // or "inline", "none"
minify: true,
});
```
## Common Patterns
### Building a Library
```typescript
// 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
```typescript
// 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
```typescript
// 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
```typescript
// 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:
```typescript
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
```typescript
// 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
```typescript
// 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
```typescript
// 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
```typescript
// 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
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.