patterntypescriptTip
Bun bundler: fast JavaScript/TypeScript bundling
Viewed 0 times
bun buildbundlertree shakingTypeScript compilebun bundleesbuild alternative
bun
Problem
Bundling TypeScript/JavaScript projects for production without configuring webpack, esbuild, or Vite separately.
Solution
Use bun build ./src/index.ts --outdir ./dist --target browser (or node/bun). Bun's bundler supports TypeScript, JSX, tree-shaking, and code splitting natively. No config file needed for basic cases.
Why
Bun's bundler is implemented in Zig and is one of the fastest JS bundlers available, often faster than esbuild. It is built-in so no additional dependencies are needed.
Gotchas
- Bun bundler is newer than esbuild/webpack — some edge cases may not be handled
- --target bun produces bun-specific output; use --target node for Node.js compatibility
- CSS bundling and asset handling are supported but less mature than Vite
- Source maps require --sourcemap flag; not enabled by default
Code Snippets
Basic Bun build command
bun build ./src/index.ts --outdir ./dist --target browser --minify --sourcemapProgrammatic bun build API
const result = await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist',
minify: true,
target: 'browser',
});
if (!result.success) console.error(result.logs);Revisions (0)
No revisions yet.