网站首页 > 开源技术 正文
Bun简介:
Bun 是一个全新的 JavaScript 和 TypeScript 运行环境,它是用 Zig 编程语言编写的。Bun 的目标是提供一个统一的、高性能的开发体验,以替代现有的 Node.js、npm 或 Yarn。
Bun的官网及开源地址:
https://bun.sh/
开源地址:https://github.com/oven-sh/bun
官方文档:https://bun.sh/docs
Bun的特性:
- "all in one",它提供了用于 JavaScript 和 TypeScript 应用程序的多合一工具包,省掉了前端用户安全nodejs,npm,yarn,npmp等要安装多种工具的麻烦,它内置了这些命令,方便用户“开箱即用”的需求。
- 单文件运行,无须安全。理论上,只需要下载一个单文件,即可取代整个nodejs运行环境。
- 提供打包、转译、安装和运行 JavaScript & TypeScript 项目的功能,内置原生打包器 (native bundler)、转译器、task runner、npm 客户端,以及fetch、WebSocket等 Web API
- 能将TypeScript 或 JavaScript 文件生成独立的二进制文件,既保障了你的代码安全,又方便部署。
- 提供了一系列先进的特性,包括子进程、FFI多语言接口、插件、DNS缓存等。
安装及升级
macOS/Linux (curl) :
curl -fsSL https://bun.sh/install | bash # for macOS, Linux, and WSL
# to install a specific version
curl -fsSL https://bun.sh/install | bash -s "bun-v1.0.0"
Windows:
powershell -c "irm bun.sh/install.ps1|iex"
#要求 Windows 10 1809及以上版本
也可以下载独立文件,下载地址:https://github.com/oven-sh/bun/releases,找到适合自己操作系统的版本,下载zip文件,解压之后,添加路径到系统变量中,即可使用。
升级:
如果你有旧版本的bun想升级到新版本,使用以下命令即可:
bun upgrade
也可使用测试版(尝鲜版,官方称之为“Canary builds”,金丝雀构建版)主要用于新功能测试及稳定版发布之前错误修复,此版本会自动上报错误信息。升级方法:
bun upgrade --canary
全局配置国内源:
为了加速包的下载,通常使用国内源。配置方法:
#Windows下,打开`C:\Users\xxx`文件夹(其中xxx为你的用户名),在里面创建`.bunfig.toml`文件,内容如下:
[install]
registry = "https://registry.npmmirror.com/"
#mac及linux则将此文件放在$HOME目录下,文件路径为:`$HOME/.bunfig.toml`
常见使用方法
- 创建新项目并添加相关依赖软件包
cd test
PS D:\tmp\test> bun init
bun init helps you get started with a minimal project and tries to guess sensible defaults. Press ^C anytime to quit
package name (test):
entry point (index.ts):
Done! A package.json file was saved in the current directory.
+ index.ts
+ .gitignore
+ tsconfig.json (for editor auto-complete)
+ README.md
To get started, run:
bun run index.ts
运行
D:\tmp\test> bun run index.ts
Hello via Bun!
- 使用现有的前端项目进行构建
以百度的amis-editor-demo项目为例,下载代码包并解压。进入目录,在命令行下运行:
PS D:\project\javascript\amis-editor-demo> bun install
bun install v1.1.20 (ae194892)
warn: incorrect peer dependency "react@16.14.0"
+ @types/lodash@4.17.7
+ @types/node@11.15.54 (v22.1.0 available)
+ @types/qs@6.9.15
+ @types/react@16.14.60 (v18.3.3 available)
+ @types/react-dom@16.9.24 (v18.3.0 available)
+ @types/react-router@5.0.1 (v5.1.20 available)
+ @types/react-router-dom@5.3.3
+ acorn@8.12.1
+ amis-widget-cli@3.2.9
+ monaco-editor-webpack-plugin@6.0.0 (v7.1.0 available)
+ prettier@2.8.8 (v3.3.3 available)
+ sass@1.77.8
+ typescript@4.9.5 (v5.5.4 available)
+ @fortawesome/fontawesome-free@5.15.4 (v6.6.0 available)
+ amis@6.0.0 (v6.6.0 available)
+ amis-core@6.0.0 (v6.6.0 available)
+ amis-editor@6.0.0 (v6.6.0 available)
+ amis-editor-core@6.0.0 (v6.6.0 available)
+ amis-formula@6.0.0 (v6.6.0 available)
+ amis-ui@6.0.0 (v6.6.0 available)
+ axios@0.21.1 (v1.7.3 available)
+ copy-to-clipboard@3.3.3
+ mobx@4.15.7 (v6.13.1 available)
+ mobx-react@6.3.1 (v9.1.1 available)
+ mobx-state-tree@3.17.3 (v6.0.1 available)
+ qs@6.5.3 (v6.13.0 available)
+ react@16.14.0 (v18.3.1 available)
+ react-dom@16.14.0 (v18.3.1 available)
+ react-hook-form@7.52.2
+ react-router@5.0.1 (v6.26.0 available)
+ react-router-dom@5.0.1 (v6.26.0 available)
1392 packages installed [32.27s]
Blocked 2 postinstalls. Run `bun pm untrusted` for details.
- 调试代码并运行
接着以开发者模式运行amis-editor-demo
bun dev
$ amis dev
_/
_/_/_/ _/_/_/ _/_/ _/_/_/
_/ _/ _/ _/ _/ _/ _/_/
_/ _/ _/ _/ _/ _/ _/_/
_/_/_/ _/ _/ _/ _/ _/_/_/
当前版本:v3.2.9.
- [amis]开启调试模式...[HPM] Proxy created: / -> http://api-test.com.cn
<i> [webpack-dev-middleware] wait until bundle finished
Build completed in 19.561s
。。。。。。
webpack 5.93.0 compiled with 1 warning in 19316 ms
√ [amis]调试模式已开启!
> Listening at http://localhost:80
当前运行脚本:
http://localhost:80/index.js
当前运行样式[可能不存在]:
http://localhost:80/index.css
浏览器会自动打开,可看到正常运行amis-editor-demo。
- 生成(构建)正式版代码
bun run build
bun run build
$ amis build
_/
_/_/_/ _/_/_/ _/_/ _/_/_/
_/ _/ _/ _/ _/ _/ _/_/
_/ _/ _/ _/ _/ _/ _/_/
_/_/_/ _/ _/ _/ _/ _/_/_/
当前版本:v3.2.9.
Build completed in 71.822s
Build completed in 0s
......
webpack 5.93.0 compiled with 3 warnings in 71823 ms
构建完成.
默认情况下,在demo-6.0.0下可看到生成的最终代码文件。
- 将前端代码编译成可执行代码
cd test
PS D:\tmp\test> bun init
bun init helps you get started with a minimal project and tries to guess sensible defaults. Press ^C anytime to quit
package name (test):
entry point (index.ts):
Done! A package.json file was saved in the current directory.
+ index.ts
+ .gitignore
+ tsconfig.json (for editor auto-complete)
+ README.md
To get started, run:
bun run index.ts
#编译
bun build ./index.ts --compile --outfile mycli
[11ms] bundle 1 modules
[57ms] compile mycli.exe
#运行
.\mycli.exe
Hello via Bun!
猜你喜欢
- 2024-11-08 程序员如何优雅地使用 macOS?(mac程序员常用软件)
- 2024-11-08 如何学习MySQL、一起来看看吧(如何学会mysql)
- 2024-11-08 TiDB 4.0 21天掌握——第4天(tidb优势)
- 2024-11-08 细数那些命令行堪称神器的软件之最
- 2024-11-08 搞一个Node Cli来提升工作效率(运行node项目)
- 2024-11-08 Linux 这些工具堪称神器(linux工具书)
- 2024-11-08 15个相见恨晚的 Linux 神器,你可能一个都没见过
- 2024-07-23 Python 中更优雅的日志记录方案(python3 日志模块)
- 2024-07-23 在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
- 2024-07-23 如何发布NPM包,搭建CLI工具(详细教程)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)