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 by default
- Smart Configuration - Reads
tsconfig.jsonfor paths, JSX options, and experimental decorators - Plugins - Plugin support, including TailwindCSS
- ESM & CommonJS - Use ESM and CommonJS in your JavaScript, TypeScript, and JSX files
- CSS Bundling & Minification - Bundles CSS from
<link>tags and@importstatements - Asset Management - Copies and hashes images and assets, and rewrites asset paths in JavaScript, CSS, and HTML
Single Page Apps (SPA)
When you pass a single.html file to Bun, Bun uses it as a fallback route for all paths. This suits single page apps that use client-side routing:
terminal
/about and /users/123 serve the same HTML file, so your client-side router handles 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.htmlat/about.htmlat/about
Glob patterns
To specify multiple files, use a glob pattern that ends in.html:
terminal
Path normalization
Bun chooses the base path from the longest common prefix among all the files.terminal
JavaScript, TypeScript, and JSX
Bun’s transpiler natively implements JavaScript, TypeScript, and JSX support. See loaders.Bun’s transpiler is also used at runtime.
ES Modules & CommonJS
You can use ESM and CommonJS in your JavaScript, TypeScript, and JSX files. Bun transpiles and bundles them automatically. There is no pre-build or separate optimization step. It’s all done at the same time. See module resolution.CSS
Bun’s CSS parser is also natively implemented (about 70,000 lines of Rust). It’s also a CSS bundler. You can use@import in your CSS files to import other CSS files.
For example:
styles.css
Referencing local assets in CSS
styles.css
./logo.png to the output directory and rewrites 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, import it from the JavaScript file../app.css and ./app.js in the output directory. All CSS files imported from JavaScript are bundled into a single CSS file per entry point. If you import the same CSS file from multiple JavaScript files, it is only 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, an @import in CSS, or an import in JavaScript.
- index.html
- styles.css
- app.ts
index.html
Only one of these is necessary, not all three.
Inline environment variables
Bun can replaceprocess.env.* references in your JavaScript and TypeScript with their actual values at build time. Use this to inject configuration like API URLs or feature flags into your frontend code.
Dev server (runtime)
To inline environment variables when usingbun ./index.html, configure the env option in your bunfig.toml:
bunfig.toml
This only works with literal
process.env.FOO references, not import.meta.env or indirect access like const env = process.env; env.FOO.If an environment variable is not set, you may see runtime errors like ReferenceError: process is not defined in the browser.terminal
Build for production
When building static HTML for production, use theenv option to inline environment variables:
- CLI
- API
terminal
Example
Given this source file:PUBLIC_API_URL=https://api.example.com:
terminal
Echo console logs from browser to terminal
Bun’s dev server can stream console logs from the browser to the terminal. To enable this, pass the--console CLI flag.
terminal
console.log or console.error is broadcast to the terminal that started the server, so browser errors show up in the same place you run your server. This also helps AI agents that watch terminal output.
Internally, this reuses the existing WebSocket connection from hot module replacement (HMR) to send the logs.
Edit files in the browser
Bun’s frontend dev server supports Automatic Workspace Folders in Chrome DevTools, so you can save edits to files from 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
Watch Mode
Runbun build --watch to watch for changes and rebuild automatically. This works well for library development.
You’ve never seen a watch mode this fast.
Plugin API
For more control, configure the bundler through the JavaScript API and use Bun’s built-inHTMLRewriter 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 anhrefattribute pointing to a local file is rewritten to the new path, and hashed
How this works
This is a small wrapper around Bun’s support for HTML imports in JavaScript.Standalone HTML
You can bundle your entire frontend into a single self-contained.html file with no external dependencies using --compile --target=browser. All JavaScript, CSS, and images are inlined directly into the HTML.
terminal
Adding a backend to your frontend
To add a backend to your frontend, use theroutes option in Bun.serve. See the full-stack docs.