index.html
bun
.
terminal
- Automatic Bundling - Bundles and serves your HTML, JavaScript, and CSS
- Multi-Entry Support - Handles multiple HTML entry points and glob entry points
- Modern JavaScript - TypeScript & JSX support out of the box
- Smart Configuration - Reads
tsconfig.json
for paths, JSX options, experimental decorators, and more - Plugins - Plugins for TailwindCSS and more
- ESM & CommonJS - Use ESM and CommonJS in your JavaScript, TypeScript, and JSX files
- CSS Bundling & Minification - Bundles CSS from
<link>
tags and@import
statements - Asset Management - Automatic copying & hashing of images and assets; Rewrites asset paths in JavaScript, CSS, and HTML
Single Page Apps (SPA)
When you pass a single.html
file to Bun, Bun will use it as a fallback route for all paths. This makes it perfect for single page apps that use client-side routing:
terminal
/about
, /users/123
, etc. will serve the same HTML file, letting your client-side router handle the navigation.
index.html
Multi-page apps (MPA)
Some projects have several separate routes or HTML files as entry points. To support multiple entry points, pass them all tobun
:
terminal
index.html
at/
about.html
at/about
Glob patterns
To specify multiple files, you can use glob patterns that end in.html
:
terminal
Path normalization
The base path is chosen from the longest common prefix among all the files.terminal
JavaScript, TypeScript, and JSX
Bun’s transpiler natively implements JavaScript, TypeScript, and JSX support. Learn more about loaders in Bun.Bun’s transpiler is also used at runtime.
ES Modules & CommonJS
You can use ESM and CJS in your JavaScript, TypeScript, and JSX files. Bun will handle the transpilation and bundling automatically. There is no pre-build or separate optimization step. It’s all done at the same time. Learn more about module resolution in Bun.CSS
Bun’s CSS parser is also natively implemented (clocking in around 58,000 lines of Zig). It’s also a CSS bundler. You can use@import
in your CSS files to import other CSS files.
For example:
Referencing local assets in CSS
You can reference local assets in your CSS files.styles.css
./logo.png
to the output directory and rewrite the path in the CSS file to include a content hash.
styles.css
Importing CSS in JavaScript
To associate a CSS file with a JavaScript file, you can import it in your JavaScript file../app.css
and ./app.js
in the output directory. All CSS files imported from JavaScript will be bundled into a single CSS file per entry point. If you import the same CSS file from multiple JavaScript files, it will only be included once in the output CSS file.
Plugins
The dev server supports plugins.Tailwind CSS
To use TailwindCSS, install thebun-plugin-tailwind
plugin:
terminal
bunfig.toml
:
bunfig.toml
<link>
tag, @import
in CSS, or import in JavaScript.
- index.html
- styles.css
- app.ts
index.html
Only one of those are necessary, not all three.
Echo console logs from browser to terminal
Bun’s dev server supports streaming console logs from the browser to the terminal. To enable, pass the--console
CLI flag.
terminal
console.log
or console.error
will be broadcast to the terminal that started the server. This is useful to see errors from the browser in the same place you run your server. This is also useful for AI agents that watch terminal output.
Internally, this reuses the existing WebSocket connection from hot module reloading to send the logs.
Edit files in the browser
Bun’s frontend dev server has support for Automatic Workspace Folders in Chrome DevTools, which lets you save edits to files in the browser.Keyboard Shortcuts
While the server is running:o + Enter
- Open in browserc + Enter
- Clear consoleq + Enter
(orCtrl+C
) - Quit server
Build for Production
When you’re ready to deploy, usebun build
to create optimized production bundles:
- CLI
- API
terminal
Currently, plugins are only supported through
Bun.build
’s API or through bunfig.toml
with the
frontend dev server - not yet supported in bun build
’s CLI.Watch Mode
You can runbun build --watch
to watch for changes and rebuild automatically. This works nicely for library development.
You’ve never seen a watch mode this fast.
Plugin API
Need more control? Configure the bundler through the JavaScript API and use Bun’s builtinHTMLRewriter
to preprocess HTML.
What Gets Processed?
Bun automatically handles all common web assets:- Scripts (
<script src>
) are run through Bun’s JavaScript/TypeScript/JSX bundler - Stylesheets (
<link rel="stylesheet">
) are run through Bun’s CSS parser & bundler - Images (
<img>
,<picture>
) are copied and hashed - Media (
<video>
,<audio>
,<source>
) are copied and hashed - Any
<link>
tag with anhref
attribute pointing to a local file is rewritten to the new path, and hashed
This is a work in progress
- Need more plugins
- Need more configuration options for things like asset handling
- Need a way to configure CORS, headers, etc.
How this works
This is a small wrapper around Bun’s support for HTML imports in JavaScript.Adding a backend to your frontend
To add a backend to your frontend, you can use the “routes” option inBun.serve
.
Learn more in the full-stack docs.