Bun’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/oven-sh/bun/llms.txt
Use this file to discover all available pages before exploring further.
Bun.serve() integrates directly with the bundler, letting you serve HTML, TypeScript, JSX, and CSS from a single server process with hot module reloading in development and optimized production builds.
Quick start
Import HTML files and pass them to theroutes option in Bun.serve():
server.ts
HTML routes
HTML as an entrypoint
The web starts with HTML, and so does Bun’s fullstack dev server. Import an HTML file directly from your TypeScript or JavaScript server code:routes in Bun.serve():
/, Bun scans the HTML for <script> and <link> tags, runs the bundler on the referenced files, and serves the result.
What Bun does to your HTML
Anindex.html like this:
index.html
<script> tags are combined into a single bundle, and multiple CSS files are merged into one stylesheet. Asset URLs are content-hashed for cache busting.
Processing pipeline
Script processing
Transpiles TypeScript, JSX, and TSX from
<script> tags. Bundles imported dependencies. Generates sourcemaps for debugging. Minifies when development is false.CSS processing
Processes
<link rel="stylesheet"> tags. Concatenates CSS files and rewrites url() references to include content-addressable hashes.Asset processing
Rewrites image and font URLs to include content-addressable hashes. Small assets in CSS are inlined as
data: URLs to reduce HTTP requests.HTML rewriting
Combines all
<script> tags into one and all <link> tags into one, producing a new HTML file that references the bundled assets.React integration
Development mode
Enable development mode withdevelopment: true:
- Includes sourcemaps so devtools show original source
- Disables minification
- Re-bundles assets on each request to an
.htmlroute - Enables hot module reloading (HMR)
- Echoes
console.logcalls from the browser to the terminal
Hot module replacement
HMR is enabled by default in development mode. To configure it explicitly:console: true is set, console.log(), console.warn(), and console.error() calls from your frontend code are forwarded to the terminal over the same WebSocket connection used for HMR.
Development vs production comparison
| Feature | Development | Production |
|---|---|---|
| Source maps | Enabled | Disabled |
| Minification | Disabled | Enabled |
| Hot reloading | Enabled | Disabled |
| Asset bundling | On each request | Cached |
| Console forwarding | Supported | Disabled |
| Error details | Full | Minimal |
API routes
HTTP method handlers
Dynamic routes
Production builds
Ahead-of-time bundling (recommended)
Usebun build to bundle your full-stack application before deployment:
Bun.serve() uses to serve pre-bundled assets.
Runtime bundling
Setdevelopment: false to enable in-memory caching without a build step:
development: false, Bun:
- Bundles assets lazily on the first request
- Caches the result in memory until the server restarts
- Adds
Cache-ControlandETagheaders - Minifies JavaScript
Docker deployment
Dockerfile
Plugins
Bundler plugins work when bundling static routes. Configure them inbunfig.toml:
bunfig.toml
TailwindCSS
bunfig.toml
index.html
Custom plugins
bunfig.toml
my-plugin.ts
Inline environment variables
Configure howprocess.env.* references are handled in frontend code:
bunfig.toml
Project structure
A recommended structure for a Bun fullstack application:The fullstack dev server is still evolving. CLI integration with
bun build, file-based API routing, and built-in SSR are planned for future releases.