bun create
從 React 元件、create-<template> npm 包、GitHub 儲存庫或本地模板建立新的 Bun 專案
使用 bun create 建立新的 Bun 專案。它可以從 React 元件、create-<template> npm 包、GitHub 儲存庫或本地模板建立專案。
要建立空專案,請使用 bun init。
從 React 元件建立
bun create ./MyComponent.tsx 會將已有的 React 元件轉變成一個包含熱過載和生產建置的一體化開發環境。
bun create ./MyComponent.jsx # 同時支援 .tsx
工作原理
當你執行 bun create <component> 時,Bun 會:
- 使用 Bun 的 JavaScript 打包器 分析你的模組圖。
- 收集執行該元件所需的所有相依。
- 掃描入口點匯出中是否存在 React 元件。
- 生成包含所需相依和指令碼的
package.json檔案,用來執行該元件。 - 使用
bun install --only-missing安裝缺失的相依。 - 生成以下檔案:
${component}.html${component}.client.tsx(前端入口點)${component}.css
- 啟動前端開發伺服器。
在 Bun 中使用 TailwindCSS
TailwindCSS 是一個用於為 Web 應用程式設定樣式的實用優先 CSS 框架。
當你執行 bun create <component> 時,Bun 會掃描你的 JSX/TSX 檔案(以及它匯入的任何檔案)中的 TailwindCSS 類名。如果 Bun 檢測到 TailwindCSS 類名,它會將以下相依新增到你的 package.json 中:
{
"dependencies": {
"tailwindcss": "^4",
"bun-plugin-tailwind": "latest"
}
}
Bun 還會設定 bunfig.toml,以便通過 Bun.serve() 使用其 TailwindCSS 外掛:
[serve.static]
plugins = ["bun-plugin-tailwind"]
Bun 還會寫入一個 ${component}.css 檔案,並在檔案頂部新增 @import "tailwindcss";:
@import "tailwindcss";
在 Bun 中使用 shadcn/ui
shadcn/ui 是一個用於建置 Web 應用程式的元件庫工具。
bun create <component> 會掃描是否有任何從 @/components/ui 匯入的 shadcn/ui 元件。
如果找到,它會執行:
# 假設 bun 檢測到匯入了 @/components/ui/accordion 和 @/components/ui/button
bunx shadcn@canary add accordion button # 以及其他元件
由於 shadcn/ui 本身使用 TailwindCSS,bun create 還會將 TailwindCSS 相依新增到你的 package.json 中,並設定 bunfig.toml,以便通過 Bun.serve() 使用 Bun 的 TailwindCSS 外掛,如前文所述。
bun create 還會設定:
tsconfig.json,將"@/*"別名指向"src/*"或.(具體取決於是否存在src/目錄)components.json,以便 shadcn/ui 知道這是一個 shadcn/ui 專案styles/globals.css檔案,按照 shadcn/ui 的預期設定 Tailwind v4${component}.build.ts檔案,該檔案使用設定了bun-plugin-tailwind的方式為生產環境建置元件
使用 bun create ./MyComponent.jsx 可以在本地執行由 Claude 或 ChatGPT 等 LLM 生成的程式碼。
從 npm 建立
bun create <template> [<destination>]
如果你沒有名稱相同的本地模板,此命令會從 npm 下載並執行 create-<template> 包。以下兩個命令是等效的:
bun create remix
bunx create-remix
有關使用說明,請參閱 create-<template> 包的文件。對於 react 和 next,Bun 不會執行任何包;它只會列印一條訊息,指向 react-app/vite 和 next-app。
從 GitHub 建立
bun create <user>/<repo> 會將 GitHub 儲存庫的內容下載到磁碟。
bun create <user>/<repo>
bun create github.com/<user>/<repo>
可以選擇為目標資料夾指定名稱。如果不指定,Bun 將使用儲存庫名稱。
bun create <user>/<repo> mydir
bun create github.com/<user>/<repo> mydir
然後,Bun 會:
- 下載模板
- 將所有模板檔案複製到目標資料夾
- 使用
bun install安裝相依 - 初始化一個全新的 Git 儲存庫。使用
--no-git標誌可選擇退出。
從本地模板建立
你可以在本地檔案系統上定義自定義模板。將它們放在以下目錄之一:
$HOME/.bun-create/<name>:全域性模板目錄<專案根目錄>/.bun-create/<name>:專案專屬模板目錄
要建立本地模板,請在 $HOME/.bun-create 中建立一個以模板命名的目錄。
cd $HOME/.bun-create
mkdir foo
cd foo
然後,在該目錄下建立 package.json 檔案,內容如下:
{
"name": "foo"
}
在檔案系統的其他位置執行 bun create foo,以驗證 Bun 是否能找到你的本地模板。
設定邏輯
你可以在本地模板的 package.json 的 "bun-create" 欄位中指定安裝前後執行的指令碼。
{
"name": "@bun-examples/simplereact",
"version": "0.0.1",
"main": "index.js",
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"bun-create": {
"preinstall": "echo 'Installing...'", // 單條命令
"postinstall": ["echo 'Done!'"], // 命令陣列,會依次執行
"start": "bun run echo 'Hello world!'"
}
}
preinstall 和 postinstall 接受字串或字串陣列。陣列中的命令會按順序執行。
| Field | Description |
|---|---|
postinstall | 在安裝相依後執行 |
preinstall | 在安裝相依前執行 |
start | Bun 完成後在“To get started, run:”下方列印的命令;預設為 bun dev |
克隆模板後,bun create 會從 package.json 中移除 "bun-create" 部分,然後再將其寫入目標資料夾。
參考
CLI 引數
| 引數 | 描述 |
|---|---|
--force | 覆蓋已存在的檔案 |
--no-install | 跳過安裝 node_modules 和相關任務 |
--no-git | 不初始化 git 儲存庫 |
--open | 執行完成後啟動並在瀏覽器中開啟 |
環境變數
| 名稱 | 描述 |
|---|---|
GITHUB_API_DOMAIN | Bun 下載內容所用的 GitHub 域名。如果你使用 GitHub Enterprise 或代理,請設定此項 |
GITHUB_TOKEN (or GITHUB_ACCESS_TOKEN) | 允許 bun create 訪問私有儲存庫並避免速率限制。如果兩者都存在,Bun 會優先使用 GITHUB_TOKEN。 |
關於 bun create 的工作流程
當你執行 bun create ${template} ${destination} 時,流程如下:
如果是遠端模板(elysia、elysia-buchta 或 stric;對於任何其他 npm 模板名稱,Bun 會將其交給 bunx create-${template} 處理;react 和 next 除外,它們只會列印一條訊息,指向 react-app/vite 和 next-app)
- GET
registry.npmjs.org/@bun-examples/${template}/latest並解析它 - GET
registry.npmjs.org/@bun-examples/${template}/-/${template}-${latestVersion}.tgz - 將
${template}-${latestVersion}.tgz解壓並提取到${destination}- 如果檔案將被覆蓋,Bun 會警告並退出,除非你傳入
--force
- 如果檔案將被覆蓋,Bun 會警告並退出,除非你傳入
如果是 GitHub 儲存庫
- 從 GitHub 的 API 下載 tarball
- 解壓並提取到
${destination}- 如果檔案將被覆蓋,Bun 會警告並退出,除非你傳入
--force
- 如果檔案將被覆蓋,Bun 會警告並退出,除非你傳入
如果是本地模板
- 開啟本地模板資料夾
- 遞迴刪除目標目錄
- 使用可用的最快系統呼叫遞迴複製檔案(在 macOS 上使用
fcopyfile;在 Linux 上使用copy_file_range)。如果node_modules資料夾存在,請勿複製或遍歷該資料夾(僅此一項就使其速度快於cp)
然後,對於遠端模板、GitHub 儲存庫和本地模板,一律執行以下操作:
- 再次解析
package.json,將name更新為${basename(destination)},從package.json中移除bun-create部分,並將更新後的package.json儲存到磁碟。 - 執行
"bun-create": { "preinstall" }中定義的任何任務 - 除非你傳入
--no-install或package.json中沒有相依,否則執行bun install - 執行
"bun-create": { "postinstall" }中定義的任何任務 - 執行
git init; git add -A .; git commit -am "Initial Commit";- 將
gitignore重新命名為.gitignore。npm 會從已釋出的包中移除.gitignore檔案。 - 如果存在相依,此步驟會在單獨的執行緒中執行,同時 Bun 安裝
node_modules - 我們測試過在可用時使用 libgit2,它在微基準測試中的速度慢了 3 倍
- 將