-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support bundling HTML files and their js, css, and assets in `Bun.bui…
…ld` and `bun build` (#15940)
- Loading branch information
1 parent
774e30d
commit da54e81
Showing
23 changed files
with
1,882 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
As of Bun v1.1.43, Bun's bundler now has first-class support for HTML. Build static sites, landing pages, and web applications with zero configuration. Just point Bun at your HTML file and it handles everything else. | ||
|
||
```html#index.html | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="./styles.css" /> | ||
<script src="./app.ts" type="module"></script> | ||
</head> | ||
<body> | ||
<img src="./logo.png" /> | ||
</body> | ||
</html> | ||
``` | ||
|
||
One command is all you need (won't be experimental after Bun v1.2): | ||
|
||
{% codetabs %} | ||
|
||
```bash#CLI | ||
$ bun build --experimental-html --experimental-css ./index.html --outdir=dist | ||
``` | ||
|
||
```ts#API | ||
Bun.build({ | ||
entrypoints: ["./index.html"], | ||
outdir: "./dist", | ||
// On by default in Bun v1.2+ | ||
html: true, | ||
experimentalCss: true, | ||
}); | ||
``` | ||
|
||
{% /codetabs %} | ||
|
||
Bun automatically: | ||
|
||
- Bundles, tree-shakes, and optimizes your JavaScript, JSX and TypeScript | ||
- Bundles and optimizes your CSS | ||
- Copies & hashes images and other assets | ||
- Updates all references to local files or packages in your HTML | ||
|
||
## Zero Config, Maximum Performance | ||
|
||
The HTML bundler is enabled by default after Bun v1.2+. Drop in your existing HTML files and Bun will handle: | ||
|
||
- **TypeScript & JSX** - Write modern JavaScript for browsers without the setup | ||
- **CSS** - Bundle CSS stylesheets directly from `<link rel="stylesheet">` or `@import` | ||
- **Images & Assets** - Automatic copying & hashing & rewriting of assets in JavaScript, CSS, and HTML | ||
|
||
## Watch mode | ||
|
||
You can run `bun build --watch` to watch for changes and rebuild automatically. | ||
|
||
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 builtin `HTMLRewriter` to preprocess HTML. | ||
|
||
```ts | ||
await Bun.build({ | ||
entrypoints: ["./index.html"], | ||
outdir: "./dist", | ||
html: true, | ||
experimentalCss: true, | ||
minify: true, | ||
|
||
plugins: [ | ||
{ | ||
// A plugin that makes every HTML tag lowercase | ||
name: "lowercase-html-plugin", | ||
setup({ onLoad }) { | ||
const rewriter = new HTMLRewriter().on("*", { | ||
element(element) { | ||
element.tagName = element.tagName.toLowerCase(); | ||
}, | ||
text(element) { | ||
element.replace(element.text.toLowerCase()); | ||
}, | ||
}); | ||
|
||
onLoad({ filter: /\.html$/ }, async args => { | ||
const html = await Bun.file(args.path).text(); | ||
|
||
return { | ||
// Bun's bundler will scan the HTML for <script> tags, <link rel="stylesheet"> tags, and other assets | ||
// and bundle them automatically | ||
contents: rewriter.transform(html), | ||
loader: "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 an `href` attribute pointing to a local file is rewritten to the new path, and hashed | ||
|
||
All paths are resolved relative to your HTML file, making it easy to organize your project however you want. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.