使用 Vite 和 Bun 构建前端
Vite 无需额外配置即可与 Bun 一起使用。使用 Vite 的模板之一开始。
terminal
bun create vite my-app
Text
◇ Select a framework:
│ React
│
◇ Select a variant:
│ TypeScript
│
◇ Which linter to use?
│ Oxlint
│
◇ Install with bun and start now?
│ No
│
◇ Scaffolding project in /path/to/my-app...
然后进入项目目录并安装依赖。
terminal
cd my-app
bun install
使用 bunx 和 vite CLI 启动开发服务器。
--bun 标志会告诉 Bun 使用 bun 而不是 node 来运行 Vite 的 CLI。默认情况下,Bun 会遵循 Vite 的 #!/usr/bin/env node shebang 行。
terminal
bunx --bun vite
为了简化此命令,将 package.json 中的 "dev" 脚本更新为以下内容。
package.json
"scripts": {
"dev": "vite", // [!code --]
"dev": "bunx --bun vite", // [!code ++]
"build": "tsc -b && vite build",
"lint": "oxlint",
"preview": "vite preview"
},
// ...
现在你可以用 bun run dev 启动开发服务器。
terminal
bun run dev
为生产环境构建应用。
terminal
bunx --bun vite build
如需了解更多信息,请参阅 Vite 文档。