Basic usage
Given an HTML file:index.html
dist/index.html
Supported elements
Bun’s HTML processor recognizes and bundles the following elements:Scripts
Stylesheets
Images
Other assets
External URLs
External URLs (those starting withhttp://, https://, or //) are preserved as-is:
Public path
Use--public-path to prefix all asset URLs:
Output
Minification
HTML is minified when--minify is enabled:
- Whitespace between elements
- HTML comments (except conditional comments)
- Unnecessary quotes around attribute values
- Optional closing tags
Multiple HTML files
You can bundle multiple HTML files:Code splitting
When multiple HTML files share JavaScript or CSS, those assets can be code-split:JavaScript API
Dynamic HTML generation
For server-side rendering or dynamic HTML generation, import HTML files in your JavaScript:server.ts
--target=bun, the HTML import resolves to a manifest object for serving pre-bundled assets efficiently.
Full-stack applications
Combine HTML processing with server-side code:server.ts
Templates and components
For more complex HTML generation, use template literals or JSX:server.tsx
Asset hashing
All bundled assets are content-hashed by default:- Long-term caching (files can be cached forever)
- Cache invalidation (changing content changes the hash)
- Parallel loading (no version conflicts)
Source maps
Generate source maps for JavaScript bundles:Examples
Static site
index.html
Single-page application
index.html
src/main.tsx
Progressive Web App
index.html
Troubleshooting
Paths not resolving
Make sure all asset paths are relative to the HTML file:Assets not found
Check that the referenced files exist relative to the HTML file:External URLs bundled
Ensure external URLs start withhttp://, https://, or //:
Inline scripts interfering
Inline scripts run immediately and might interfere with bundled modules. Usetype="module" for modern code: