开发、测试、运行和捆绑 JavaScript 和 TypeScript 项目 - 全部使用 Bun。
1. 什么是 Bun
Bun 是一个从头开始构建的新 JavaScript 运行时,旨在服务现代 JavaScript 生态系统。它有三个主要设计目标:
- 速度:Bun 启动快,运行也快。它扩展了 JavaScriptCore,即为 Safari 构建的注重性能的 JS 引擎。随着计算向边缘发展,这一点至关重要。
- 优雅的 API:Bun 提供了一组最小的高度优化的 API,用于执行常见任务,例如启动 HTTP 服务器和写入文件。
- 凝聚力 DX:Bun 是一个用于构建 JavaScript 应用程序的完整工具包,包括包管理器、测试运行器和捆绑器。
Bun 被设计为 Node.js 的直接替代品。它原生实现了数百个 Node.js 和 Web API,包括 fs 、 path 、 Buffer 等。
Bun 的目标是运行世界上大部分的服务器端 JavaScript,并提供提高性能、降低复杂性并提高开发人员生产力的工具。
2. Bun 官网地址
3. Bun 安装
运行下面命令进行安装,
# 支持在 macOS、Linux 和 WSL 上安装
curl -fsSL https://bun.sh/install | bash
source /root/.bashrc
运行下面命令进行确认,
bun --help
输出日志,
Bun is a fast JavaScript runtime, package manager, bundler, and test runner. (1.0.20 (09d51486))
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 prettier 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 next-app Add a dependency to package.json (bun a)
remove react Remove a dependency from package.json (bun rm)
update webpack 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 lyra 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.sh/docs
Join our Discord community: https://bun.sh/discord
完结!