Skip to main content
Bun’s bundler can process HTML files as entrypoints, automatically bundling all referenced assets (scripts, stylesheets, images, etc.) and updating the HTML to reference the bundled files.

Basic usage

Given an HTML file:
index.html
Bundle it with:
The output will be:
And the HTML will be updated to reference the hashed files:
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 with http://, https://, or //) are preserved as-is:

Public path

Use --public-path to prefix all asset URLs:
Output
This is useful for CDN deployments:

Minification

HTML is minified when --minify is enabled:
This removes:
  • 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:
Each HTML file becomes an independent entrypoint with its own set of bundled assets.

Code splitting

When multiple HTML files share JavaScript or CSS, those assets can be code-split:
Shared code is extracted into separate chunk files, and each HTML file references the appropriate chunks.

JavaScript API

Dynamic HTML generation

For server-side rendering or dynamic HTML generation, import HTML files in your JavaScript:
server.ts
When bundled with --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
Build for production:
This creates a standalone executable with all assets embedded.

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:
This enables:
  • 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:
Source map files are placed alongside their corresponding 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 with http://, https://, or //:

Inline scripts interfering

Inline scripts run immediately and might interfere with bundled modules. Use type="module" for modern code: