Bun

Bun v1.0.17


Jarred Sumner · 2023年12月12日

Bun v1.0.17 修復了 15 個錯誤(解決了 152 個 👍 反應)。bun install postinstall 腳本為前 500 個套件執行,bunx supabase 的啟動速度比 npx supabase 快 30 倍,bunx esbuild 的啟動速度比 npx esbuild 快 50 倍,以及針對 bun install 的錯誤修復

Bun 是一個速度極快的 JavaScript 執行時環境、打包器、轉譯器和套件管理器 — 功能All-in-One。如果您錯過了,以下是 Bun 最近的一些變更

  • v1.0.13 - 修復了 6 個錯誤(解決了 317 個 👍 反應)。 'http2' 模組和 gRPC.js 現在可以運作了。Vite 5 和 Rollup 4 可以運作了。實作了 process.report.getReport(),改進了對 ES5 'with' 語句的支援,修復了 bun install 中的回歸錯誤,修復了列印例外時的崩潰問題,修復了 Bun.spawn 錯誤,並修復了 peer dependencies 錯誤
  • v1.0.14 - Bun.Glob,一個使用 glob 模式匹配檔案和字串的快速 API。它還修復了在 bun install 期間提取依賴項時的競爭條件,改進了 node_modules 中的 TypeScript 模組解析,並使錯誤訊息更易於閱讀。
  • v1.0.15 - 修復了 23 個錯誤(解決了 117 個 👍 反應),tsc 的啟動速度快了 2 倍。穩定的 WebSocket 客戶端、語法突顯錯誤、更清晰的堆疊追蹤、使用 expect.extend() 新增自訂測試匹配器 + 其他 expect 匹配器。
  • v1.0.16 - 修復了 49 個錯誤(解決了 38 個 👍 反應)。Bun.file 和 Bun.write 的並行 IO 速度提高了 3 倍,現在支援 Google Cloud Run 和 Vercel,Bun.write 會在父目錄不存在時自動建立,preload 內部的 `expect.extend` 可以運作了,`napi_create_object` 的速度提高了 2.5 倍,修復了影響 Astro v4 和 p-limit 的模組解析錯誤,console.log 錯誤修復

安裝 Bun

curl
npm
brew
docker
curl
curl -fsSL https://bun.dev.org.tw/install | bash
npm
npm install -g bun
brew
brew tap oven-sh/bun
brew install bun
docker
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun

升級 Bun

bun upgrade

bun install 現在為前 500 個 npm 套件執行生命週期腳本

在此版本中,我們在前 500 個最常下載的 npm 套件的預設允許列表中新增了在 bun install 期間執行生命週期腳本的功能。這表示 bun install 現在將為已定義生命週期腳本的前 500 個 npm 套件執行 preinstallinstallpostinstall 腳本。

這解決了 bun install 的許多問題

感謝 @dylan-conway 和 @paperclover 實作此功能。

生命週期腳本現在並行執行

作為上述工作的一部分,我們也讓生命週期腳本並行執行,這減少了生命週期腳本對安裝時間的影響。

image
⚙️ 表情符號在生命週期腳本執行時出現

設定並行

您可以透過以下方式設定一次執行的最大並行生命週期腳本數:

bunfig.toml:

# Only run 5 in parallel at max
install.concurrentScripts = 5

CLI 參數

bun install --concurrent-scripts=5

環境變數

GOMAXPROCS=2 bun install

注意:它是 `$GOMAXPROCS`,因為我們在其他地方也使用它,而且我們可以少想一個環境變數的名稱(這是 Go 使用的,但 Bun 沒有使用 Go)。

bun install 中的錯誤訊息改進

當發生權限錯誤時,bun install 現在會顯示更有幫助的錯誤訊息。它現在會在可以提供更多資訊時,提供更多資訊,而不僅僅是「Unexpected」。

左:之前,右:之後

感謝 @paperclover 實作此功能。

已修正:Semver 預先發布版本排序錯誤

已修復同時排序特定輸入的多個預先發布版本時可能發生的崩潰問題。此錯誤由 @dylan-conway 修復。安裝 4 年前的 Gatsby 版本時可以重現此錯誤。

已修正:生命週期腳本依賴項安裝順序

已修復 `bun install` 會在未先確保安裝所有依賴的生命週期腳本的情況下安裝生命週期腳本的錯誤。此錯誤由 @dylan-conway 修復。

bunx supabase 的啟動速度比 npx supabase 快 30 倍

由於 `bun install` 現在預設為熱門套件啟用生命週期腳本,因此 `bunx supabase` 現在可以運作了。

在 Linux x64 上,bun x supabase 的啟動速度比 npx supabase 快 30 倍

Benchmark 1: bun x supabase
  Time (mean ± σ):      10.5 ms ±   1.0 ms    [User: 8.0 ms, System: 5.5 ms]
  Range (min … max):     8.5 ms …  13.1 ms    248 runs

Benchmark 2: npx supabase
  Time (mean ± σ):     325.2 ms ±  30.6 ms    [User: 272.6 ms, System: 117.9 ms]
  Range (min … max):   291.0 ms … 389.9 ms    10 runs

Summary
  bun x supabase ran
   30.90 ± 4.17 times faster than npx supabase

bunx esbuild 的啟動速度比 npx esbuild 快 50 倍

現在 `bun install` 預設為熱門套件啟用生命週期腳本,`bunx esbuild` 的啟動速度現在大約比 `npx esbuild` 快 50 倍

Benchmark 1: bun x esbuild --help
  Time (mean ± σ):       3.5 ms ±   0.3 ms    [User: 1.8 ms, System: 1.6 ms]
  Range (min … max):     2.8 ms …   4.3 ms    827 runs

Benchmark 2: npx esbuild --help
  Time (mean ± σ):     295.7 ms ±  23.8 ms    [User: 270.0 ms, System: 100.6 ms]
  Range (min … max):   259.6 ms … 339.6 ms    10 runs

Summary
  bun x esbuild --help ran
   83.84 ± 9.31 times faster than npx esbuild --help

-h 現在是 --help 的別名

-h 現在是所有命令的 --help 別名。感謝 @hustLer2k 實作此功能。

$ bun -h

Bun is a fast JavaScript runtime, package manager, bundler, and test runner. (1.0.16 (a2f595d3))

Usage: bun <command> [...flags] [...args]

Commands:
  run       ./my-script.ts       Execute a file with Bun
            lint                 Run a package.json script
  test                           Run unit tests with Bun
  x         vite                 Execute a package binary (CLI), installing if needed (bunx)
  repl                           Start a REPL session with Bun

  install                        Install dependencies for a package.json (bun i)
  add       svelte               Add a dependency to package.json (bun a)
  remove    babel-core           Remove a dependency from package.json (bun rm)
  update    lodash               Update outdated dependencies
  link      [<package>]          Register or link a local npm package
  unlink                         Unregister a local npm package
  pm <subcommand>                Additional package management utilities

  build     ./a.ts ./b.jsx       Bundle TypeScript & JavaScript into a single file

  init                           Start an empty Bun project from a blank template
  create                         Create a new project from a template (bun c)
  upgrade                        Upgrade to latest version of Bun.
  <command> --help               Print help text for command.

Flags:
      --watch                    Automatically restart the process on file change
      --hot                      Enable auto reload in the Bun runtime, test runner, or bundler
      --smol                     Use less memory, but run garbage collection more often
  -r, --preload                  Import a module before other modules are loaded
      --inspect                  Activate Bun's debugger
      --inspect-wait             Activate Bun's debugger, wait for a connection before executing
      --inspect-brk              Activate Bun's debugger, set breakpoint on first line of code and wait
      --if-present               Exit without an error if the entrypoint does not exist
      --no-install               Disable auto install in the Bun runtime
      --install                  Configure auto-install behavior. One of "auto" (default, auto-installs when no node_modules), "fallback" (missing packages only), "force" (always).
  -i                             Auto-install dependencies during execution. Equivalent to --install=fallback.
  -e, --eval                     Evaluate argument as a script
      --prefer-offline           Skip staleness checks for packages in the Bun runtime and resolve from disk
      --prefer-latest            Use the latest matching versions of packages in the Bun runtime, always checking npm
  -p, --port                     Set the default port for Bun.serve
  -b, --bun                      Force a script or package to use Bun's runtime instead of Node.js (via symlinking node)
      --silent                   Don't print the script command
  -v, --version                  Print version and exit
      --revision                 Print version with revision and exit
      --env-file                 Load environment variables from the specified file(s)
      --cwd                      Absolute path to resolve files & entry points from. This just changes the process' cwd.
  -c, --config                   Specify path to Bun config file. Default $cwd/bunfig.toml
  -h, --help                     Display this menu and exit

(more flags in bun install --help, bun test --help, and bun build --help)

Learn more about Bun:            https://bun.dev.org.tw/docs
Join our Discord community:      https://bun.dev.org.tw/discord

已修正:fs.createReadStream 中的 encoding 參數

已修復 fs.createReadStreamencoding 參數未正確處理的錯誤。此錯誤由 @hustLer2k 修復。

感謝 6 位貢獻者!