Skip to content

Latest commit

 

History

History
152 lines (95 loc) · 4.46 KB

bun.mdx

File metadata and controls

152 lines (95 loc) · 4.46 KB
title description type i18nReady
在 Astro 中使用 Bun
了解如何在 Astro 站点中使用 Bun。
recipe
true

Bun 是一个 all-in-one 的 JavaScript 运行时和工具套件。可请参阅 Bun 的文档 获取更多信息。

:::caution 在 Astro 中使用 Bun 可能会存在一些问题,例如某些集成可能无法正常工作。有关详细信息,请参阅 Bun 的官方文档中与 Astro 一同使用的内容

如果你在使用 Bun 时遇到任何问题,请直接在 Bun 的 GitHub 仓库提交一个 Issue。 :::

前期准备

  • 在你的机器上安装了 Bun。请参阅 Bun 官方文档的 安装说明

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

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

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

:::tip 你也可以从任意现有的 Astro GitHub 仓库中使用 --template 标志创建了一个新的 Astro 项目:

bunx create-astro@latest my-astro-project-using-bun --template eliancodes/brutal

:::

安装依赖

如果你正使用 bunx create-astro 创建一个新的项目,CLI 将会自动使用 Bun 安装依赖,所以你可以跳过这一步。

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

bun install

添加类型

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

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

bun add -d @types/bun

使用 Astro 集成

你也可以通过 astro add 命令来使用任意 Astro 官方集成:

bunx astro add react

通过 Bun 运行 Astro

:::note 在 astro 命令之前使用 --bun CLI 标志 来使用 Bun 的运行时替代 Node。 :::

运行开发服务器

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

bunx --bun astro dev

通过 Bun 构建你的站点

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

bunx --bun astro build

Astro 会将你的站点输出到 dist/ 目录。然后,你可以使用 preview 命令来运行你的站点:

bunx --bun astro preview

通过 @astrojs/node 来让 Bun 添加 SSR

Bun 提供了 Node.js API 兼容性 的功能。使用 @astrojs/node 适配器,你可以通过 Bun 的运行时来替代 Node 来为你的 Astro 项目添加 服务器端渲染

运行如下命令来为你的 Astro 项目添加 Node.js 适配器:

bunx astro add node

接着,使用和前面相同的构建命令来再次构建你的站点:

bunx --bun astro build

最后,使用如下命令来运行你构建后的站点。

bun ./dist/server/entry.mjs

在 Bun 中测试

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

当然,你也可以使用 Cypress 或 Playwright 来测试你的网页应用。

Cypress

Cypress 是一个前端测试工具,其使命是“让测试体验变得愉快并带来开发者的快乐”。这使你可以为你的 Astro 站点编写端到端测试。

用以下命令安装 Cypress:

bun add cypress --dev

对于其余的配置和开始你的第一个测试,请参阅 Astro 测试指南 中的 Cypress 测试部分。

Playwright

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

用以下命令安装 Playwright:

bun create playwright

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

官方资源

社区资源

已经在 Astro 项目中使用 Bun?请在此页面添加你的博客文章或视频!