Hello from Bun!

otterlord

Reuben Tier

Posted on September 8, 2023

Hello from Bun!

Bun 1.0 less than an hour ago (at the time of publishing), and to celebrate this, I've migrated my blog's build process to Bun! This details how I now publish my static Astro blog to Cloudflare Pages using Bun.

First, a warning...

Before attempting this yourself, be warned that I used some hacky workarounds to bypass some existing issues with Cloudflare Pages, and Bun. I don't recommend this for important deployments yet.

Build an Astro site with Bun

Bun 1.0 supports Astro out the box. To start using it, make sure you have Bun installed and updated. Delete your existing package lockfile (package-lock.json, pnpm-lock.yml, yarn.lock) and use Bun to install dependencies.

bun install
Enter fullscreen mode Exit fullscreen mode

Workaround for the sharp package

For those using sharp, it currently has an issue when installed with Bun, but there are two workarounds you can try.

  1. Set sharp as a trusted dependency in your package.json.
"trustedDependencies": [
  "sharp"
],
Enter fullscreen mode Exit fullscreen mode
  1. If the first workaround failed, prefix your build command with the following to install sharp manually.
cd node_modules/sharp && bun install && cd ../../ && YOUR_BUILD_COMMAND
Enter fullscreen mode Exit fullscreen mode

Run bun run build to test your site builds successfully.

Deploy to Cloudflare Pages

Cloudflare Pages has experimental support for Bun. I found a gist from a user in the Cloudflare Developers Discord which documents what you need to do to enable it.

In your Cloudflare dashboard, navigate to your project and add these two environment variables.

SKIP_DEPENDENCY_INSTALL=true
UNSTABLE_PRE_BUILD=asdf plugin add bun && asdf install bun latest && asdf global bun latest && bun i
Enter fullscreen mode Exit fullscreen mode

Commit and deploy!

If everything works locally, and your Cloudflare environment variables are set, you're ready to deploy your site! My blog is still relatively small, and takes under a minute to build. However, now than Bun 1.0 is out, I'll be looking at other repos to use as benchmarks, and test how far I can push Bun. If you want to stay up to date with all my planned Bun & Astro content, and all my other shenanigans, be sure to subscribe to my email list (or the RSS feed) and follow me on Twitter.

💖 💪 🙅 🚩
otterlord
Reuben Tier

Posted on September 8, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Hello from Bun!
bunjs Hello from Bun!

September 8, 2023