Skip to content

将 Bun 与 Astro 一起使用

Bun 是一款一体化 JavaScript 运行时和工具包。请参阅 Bun 的文档 了解更多信息。

¥Bun is an all-in-one JavaScript runtime & toolkit. See Bun’s documentation for more information.

¥Prerequisites

  • Bun 安装在本地计算机上。参见 Bun 官方文档中的 安装说明

使用 Bun 创建一个新的 Astro 项目

Section titled 使用 Bun 创建一个新的 Astro 项目

¥Create a new Astro project with Bun

使用以下 create-astro 命令使用 Bun 创建一个新的 Astro 项目:

¥Create a new Astro project with Bun using the following create-astro command:

Terminal window
bunx create-astro@latest my-astro-project-using-bun

¥Install dependencies

如果你使用 bunx create-astro 启动新项目,CLI 将自动使用 Bun 来安装依赖,你可以跳过此步骤。

¥If you’re starting a new project using bunx create-astro, the CLI will automatically use Bun to install dependencies and you can skip this step.

否则,你需要使用 Bun 安装依赖:

¥Otherwise, you’ll need to install your dependencies with Bun:

Terminal window
bun install

¥Add Types

Bun 发布了 @types/bun 包,其中包含 Bun 的运行时类型。

¥Bun publishes the @types/bun package, containing the runtime types for Bun.

使用以下命令安装 @types/bun

¥Install @types/bun using the following command:

Terminal window
bun add -d @types/bun

¥Using Astro integrations

你还可以使用任何官方 Astro 集成与 astro add 命令:

¥You can also use any of the official Astro integrations with the astro add command:

Terminal window
bunx astro add react

¥Run Astro with Bun

¥Run the development server

要使用 Bun 作为运行时来运行开发服务器,请使用以下命令:

¥To run the development server using Bun as the runtime, use the following command:

Terminal window
bunx --bun astro dev

¥Building your site with Bun

要使用 Bun 作为运行时构建站点,请使用以下命令:

¥To build your site using Bun as the runtime, use the following command:

Terminal window
bunx --bun astro build

Astro 会将你的站点输出到 dist/ 目录。然后,你可以使用 preview 命令为你的站点提供服务:

¥Astro will output your site to the dist/ directory. Then, you can serve your site using the preview command:

Terminal window
bunx --bun astro preview

¥Add SSR with Bun

由于 Bun 具有 Node.js API 兼容性,你可以将任何 Astro 适配器用于 服务器端渲染 到你的 Astro 项目:

¥Since Bun features Node.js API compatibility, you can use any Astro adapter for server-side rendering to your Astro project:

Terminal window
bunx astro add vercel

¥Testing in Bun

Bun 通过 bun test 命令附带了一个快速、内置、兼容 Jest 的测试运行器。如果你喜欢使用它,请阅读 bun test 文档

¥Bun ships with a fast, built-in, Jest-compatible test runner through the bun test command. If you like to use that read bun test documentation.

然而,也可以使用 Cypress 或 Playwright 作为测试 Web 应用的现代方法。

¥However, it is also possible to use Cypress or Playwright for a modern approach to testing web apps.

Cypress 是一款前端测试工具,肩负着 “让测试体验变得愉快并给开发者带来快乐” 的使命。这使你能够为 Astro 站点编写端到端测试。

¥Cypress is a front-end testing tool and is on a mission to “make the testing experience enjoyable and generate developer happiness”. This enables you to write end-to-end tests for your Astro site.

使用以下命令安装 Cypress:

¥Install Cypress with the following command:

Terminal window
bun add cypress --dev

对于其余配置并开始首次测试,请遵循 Astro 测试指南 中赛普拉斯流程的其余部分。

¥For the rest of the configuration and to start your first test, follow the rest of Cypress process in the Astro Testing Guide.

Playwright 是一个端到端测试框架,允许你在所有现代渲染引擎(包括 Chromium、WebKit 和 Firefox)上测试 Astro 代码。

¥Playwright is an end-to-end testing framework that allows you to test your Astro code on all modern rendering engines including Chromium, WebKit, and Firefox.

使用以下命令安装 Playwright:

¥Install Playwright using the following command:

Terminal window
bun create playwright

要创建你的第一个 Playwright 测试,请按照 Astro 测试指南 中其余 Playwright 流程的说明进行操作。

¥To create your first Playwright test, follow the instructions for the rest of the Playwright process in the Astro Testing Guide.

¥Official Resources

¥Community Resources

将 Bun 与 Astro 一起使用?将你的博客文章或视频添加到此页面!

¥Using Bun with Astro? Add your blog post or video to this page!

Astro 中文网 - 粤ICP备13048890号