-
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.
Merge branch 'oven-sh:main' into main
- Loading branch information
Showing
208 changed files
with
15,083 additions
and
5,568 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,7 @@ | ||
# Add directories or file patterns to ignore during indexing (e.g. foo/ or *.csv) | ||
bench | ||
vendor | ||
*-fixture.{js,ts} | ||
zig-cache | ||
packages/bun-uws/fuzzing | ||
build |
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,62 @@ | ||
import { bench, run } from "../runner.mjs"; | ||
import zlib from "node:zlib"; | ||
import { promisify } from "node:util"; | ||
|
||
const deflate = promisify(zlib.deflate); | ||
const inflate = promisify(zlib.inflate); | ||
|
||
const short = "Hello World!"; | ||
const long = "Hello World!".repeat(1024); | ||
const veryLong = "Hello World!".repeat(10240); | ||
|
||
// Pre-compress some data for decompression tests | ||
const shortBuf = Buffer.from(short); | ||
const longBuf = Buffer.from(long); | ||
const veryLongBuf = Buffer.from(veryLong); | ||
|
||
let [shortCompressed, longCompressed, veryLongCompressed] = await Promise.all([ | ||
deflate(shortBuf, { level: 6 }), | ||
deflate(longBuf, { level: 6 }), | ||
deflate(veryLongBuf, { level: 6 }), | ||
]); | ||
|
||
const format = new Intl.NumberFormat("en-US", { notation: "compact", unit: "byte" }); | ||
// Compression tests at different levels | ||
bench(`deflate ${format.format(short.length)}B (level 1)`, async () => { | ||
await deflate(shortBuf, { level: 1 }); | ||
}); | ||
|
||
bench(`deflate ${format.format(short.length)} (level 6)`, async () => { | ||
await deflate(shortBuf, { level: 6 }); | ||
}); | ||
|
||
bench(`deflate ${format.format(long.length)} (level 1)`, async () => { | ||
await deflate(longBuf, { level: 1 }); | ||
}); | ||
|
||
bench(`deflate ${format.format(long.length)} (level 6)`, async () => { | ||
await deflate(longBuf, { level: 6 }); | ||
}); | ||
|
||
bench(`deflate ${format.format(veryLong.length)} (level 1)`, async () => { | ||
await deflate(veryLongBuf, { level: 1 }); | ||
}); | ||
|
||
bench(`deflate ${format.format(veryLong.length)} (level 6)`, async () => { | ||
await deflate(veryLongBuf, { level: 6 }); | ||
}); | ||
|
||
// Decompression tests | ||
bench(`inflate ${format.format(short.length)}`, async () => { | ||
await inflate(shortCompressed); | ||
}); | ||
|
||
bench(`inflate ${format.format(long.length)}`, async () => { | ||
await inflate(longCompressed); | ||
}); | ||
|
||
bench(`inflate ${format.format(veryLong.length)}`, async () => { | ||
await inflate(veryLongCompressed); | ||
}); | ||
|
||
await run(); |
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
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.