注意 — 我們正在招聘 C/C++ 和 Zig 工程師,以打造 JavaScript 的未來!
佳節愉快!我們很高興發布 Bun v0.4.0,它支援更多 Node.js API、提升了穩定性、修正了許多錯誤,以及一個新指令:bunx
。
隆重介紹 bunx
此版本推出了 bunx
,它是 Bun 相當於 npx
的工具,啟動速度快了 100 倍。它可以從本機或遠端 npm 套件執行可執行檔。
bunx esbuild --version
與 npx
相同,它會先檢查本機是否已安裝套件,然後退而求其次從 npm 自動安裝到共享的全域快取中。憑藉 Bun 快速的啟動時間,對於本機安裝的套件,它大約比 npx
快 100 倍。
隆重介紹 bunx
— Jarred Sumner (@jarredsumner) 2022年12月23日
從 npm 自動安裝並執行可執行檔
比 npx 快 100 倍
左: 「bunx esbuild --version」(執行 1 千次)
右: 「npx esbuild --version」(執行 1 千次) pic.twitter.com/pbgzfXyhnP
--bun
旗標
預設情況下,Bun 會遵循使用 bun run <script>
或 bunx <command>
執行的腳本或可執行檔頂部的 #!/usr/bin/env node
shebang。
// foo.js
#! /usr/bin/env node
console.log(process.argv[0]);
bun run foo.js
/path/to/node/19.2.0/bin/node
若要覆寫此行為,請傳遞 --bun
旗標。這會在執行期間將 node
暫時別名為 bun
。
bun --bun run foo.js
/path/to/.bun/bin/bun
未來,一旦 Bun 的 Node.js 相容性更加完善,我們可能會將此行為設為預設。

Node.js 相容性
對 Node.js API 的支援仍然是 Bun 的首要任務。從 Bun v0.4.0 開始,您現在可以使用以下 API
crypto.timingSafeEqual()
crypto.scryptSync()
process.abort()
process.argv0
和process.execPath
node:util/types
bun:test
中的設定與拆解
Bun 有一個內建的測試執行器,您可以使用指令 bun wiptest
執行。您現在可以為測試的設定與拆解定義 Jest 樣式生命週期鉤子。
import { beforeAll, test } from "bun:test";
let tests;
beforeAll(async () => {
const response = await fetch("https://example.com/path/to/resource");
tests = await response.json();
});
test("that integration tests are defined", () => {
expect(tests).toHaveLength(100);
});
Bun.deepEquals()
嚴格模式
您現在可以將 strict
引數傳遞給 Bun.deepEquals()
,其行為將與 expect().toStrictEqual()
相同。
const a = { entries: [1, 2] };
const b = { entries: [1, 2], extra: undefined };
Bun.deepEquals(a, b); // => true
Bun.deepEquals(a, b, true); // => false
bun pm
如果您錯過了,Bun 有一個指令可讓您查看專案中套件和鎖定檔的相關資訊:bun pm
。

bun pm ls
在 Bun v0.4.0 中,有一個新的子指令 bun pm ls
,它會列出專案中的所有套件和版本,類似於 npm ls
。

重大修正
- 修正安裝相依性時的各種問題 -
#1643
,#1647
,#1649
- 修正
node:stream
的各種錯誤 -#1606
,#1613
- 修正
process.stdin
在tty
中無法運作的問題 -#1611
,#1626
- 修正舊版 Linux 核心不支援
io_uring
時Bun.write()
的問題 -e7a14f8
- 修正匯入二進位檔案時的問題 -
5bbaa7
- 修正轉譯器錯誤,導致
const {resolve} = require
無法運作 -3c2029
- 實作了對將引數傳遞給
setTimeout()
、setInterval()
和setImmediate()
的支援 -a98e0ad
- 移除了 RegExp 回溯支援的變通方案,因為 WebKit 已經實作了 -
#1625
- 修正了轉譯器錯誤(在 Bun 中),導致在 Bun 的執行階段載入時
@babel/parser
損壞
PR 紀錄
- 修正連結造成的
__require
衝突,由 @dylan-conway 貢獻於 https://github.com/oven-sh/bun/pull/1585 - 雜項:新增 eslint 快取,由 @Simon-He95 貢獻於 https://github.com/oven-sh/bun/pull/1586
- 在
bun
指令中為 Zsh 新增檔案名稱自動完成功能,由 @colinhacks 貢獻於 https://github.com/oven-sh/bun/pull/1593 - 從自動完成功能中排除額外的 TS 宣告檔案副檔名,由 @colinhacks 貢獻於 https://github.com/oven-sh/bun/pull/1596
- 修正路徑字串,由 @YUxiangLuo 貢獻於 https://github.com/oven-sh/bun/pull/1597
- 正確覆寫
process.stdin.on()
,由 @alexlamsl 貢獻於 https://github.com/oven-sh/bun/pull/1603 - 修正 (stream):修正 Readable.pipe(),由 @ThatOneBro 貢獻於 https://github.com/oven-sh/bun/pull/1606
- 使
process.stdin
在 TTY 下運作,由 @alexlamsl 貢獻於 https://github.com/oven-sh/bun/pull/1611 - 新增
bun pm ls
以印出鎖定檔,由 @dylan-conway 貢獻於 https://github.com/oven-sh/bun/pull/1612 - 修正 (stream):使 Readable.read 在未實作 _construct 的情況下運作,由 @ThatOneBro 貢獻於 https://github.com/oven-sh/bun/pull/1613
- 修正 bun.d.ts 中的錯字,由 @eltociear 貢獻於 https://github.com/oven-sh/bun/pull/1619
- 為
process.stdin
新增測試,由 @alexlamsl 貢獻於 https://github.com/oven-sh/bun/pull/1621 - 文件 (README.md):更新 bun-types 新路徑定義,由 @hoseinprd 貢獻於 https://github.com/oven-sh/bun/pull/1622
- 刪除 Oniguruma,由 @Jarred-Sumner 貢獻於 https://github.com/oven-sh/bun/pull/1625
- 錯誤修正,與
stdin.on("readable")
相容,由 @alexlamsl 貢獻於 https://github.com/oven-sh/bun/pull/1626 - 實作
bunx
,由 @Jarred-Sumner 貢獻於 https://github.com/oven-sh/bun/pull/1634 - 為 #1633 新增測試,由 @alexlamsl 貢獻於 https://github.com/oven-sh/bun/pull/1635
- 修正 bun-test 中的 jest 鉤子,由 @ethanburrell 貢獻於 https://github.com/oven-sh/bun/pull/1639
- 修正
bun install
相依性解析,由 @alexlamsl 貢獻於 https://github.com/oven-sh/bun/pull/1643 - [install] 避免同層級之間的相依性衝突,由 @alexlamsl 貢獻於 https://github.com/oven-sh/bun/pull/1647
- 更新基準測試,由 @colinhacks 貢獻於 https://github.com/oven-sh/bun/pull/1648
- [install] 修正相依性解析中剩餘的邊角案例,由 @alexlamsl 貢獻於 https://github.com/oven-sh/bun/pull/1649
貢獻者
我們也要感謝所有協助 Bun 做出貢獻的人。
- alexlamsl 修正了
process
、node:stream
和bun install
的問題 - ThatOneBro 修正了
node:stream
的問題 - ethanburrell 修正了
bun:test
中生命週期鉤子的支援 - hoseinprd、eltociear、YUxiangLuo 和 Simon-He95 為 Bun 做出貢獻