<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Beau</title>
    <description>The latest articles on DEV Community by Beau (@beau-to).</description>
    <link>https://dev.to/beau-to</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F8419%2Feab3ee1f-76db-45dc-b2ed-c317728bc959.png</url>
      <title>DEV Community: Beau</title>
      <link>https://dev.to/beau-to</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/beau-to"/>
    <language>en</language>
    <item>
      <title>Bun-ify Your Project</title>
      <dc:creator>Anna</dc:creator>
      <pubDate>Wed, 06 Mar 2024 12:30:36 +0000</pubDate>
      <link>https://dev.to/beau-to/bun-ify-your-project-544i</link>
      <guid>https://dev.to/beau-to/bun-ify-your-project-544i</guid>
      <description>&lt;p&gt;In the realm of front-end development, maximizing efficiency and performance is paramount. Bun is a JavaScript runtime that offers faster development, testing, running, and bundling for your project. Moreover, it is also known because of its memory efficiency. Bun claims to offer significant improvements in their benchmarks.&lt;/p&gt;

&lt;p&gt;Bundle benchmark:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljogy5duhepi5vmsj1i4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljogy5duhepi5vmsj1i4.png" alt="Bundle benchmark"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Package installation:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdhyz93tw60bqr77e6kq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdhyz93tw60bqr77e6kq.png" alt="Package installation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s also worth paying attention to Bun’s bundler abilities: support for tree shaking, source maps, and minification. Bun was known for some time even though the v1.0 was released only on September 8, 2023, and caused much fuss among the front-end community.&lt;/p&gt;

&lt;p&gt;So, after some preparation and research, we decided to use it in our project, and that’s what happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Initial data
&lt;/h2&gt;

&lt;p&gt;At Beau, we have several front-end projects with the same architecture. Those projects were built using Nuxt.js, Vue2, and JavaScript. The Yarn was used as a package manager.&lt;/p&gt;

&lt;p&gt;Also, our projects have a bunch of tests: unit, e2e, and regression. Those tests use Puppeteer, jest, playwright, and other handy vue libraries for testing.&lt;/p&gt;

&lt;p&gt;Why did we decide to use Bun?&lt;br&gt;
Who, as a developer, has never faced the issue of waiting for a project to build or tests to run? This obstacle is relatable to any developer. Moreover, for business, it means money. Our project isn’t an exception. The slower parts of our processes are package installation and running tests. We think that Bun might help us to speed up such processes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Replacing yarn
&lt;/h2&gt;

&lt;p&gt;First of all, we need to deal with package management. The first step was installing the Bun itself. We can find all the necessary commands in Bun documentation.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

curl -fsSL https://bun.sh/install|bash #for macOS, Linux and WSL


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Bun also provides a Docker image if it’s necessary.&lt;br&gt;
Let’s check our local speed of package installation with yarn. To be precise about this, we need to clean the yarn cache by using the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

yarn cache clean


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After this, the pure installation speed will be almost a minute.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Done in 57.33s


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So, let’s try installing the packages with bun.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

bun install


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3z0ouq904aeqxtgekvnu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3z0ouq904aeqxtgekvnu.png" alt="Bun package installation"&gt;&lt;/a&gt;&lt;br&gt;
We can already see a significant difference.&lt;/p&gt;

&lt;p&gt;With yarn using a cache, this difference isn’t that impressive.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Done in 23.71s


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After installing the packages,  Bun will also add the bun.lockb file as an analog of yarn.lock or package.lock. Bun will install dependencies, devDependencies,  optionalDependencies, and peerDependecies by default, but because of security reasons, it will skip lifecycle scripts of installed dependencies.&lt;/p&gt;

&lt;p&gt;And this might be a problem. These scripts, such as postinstall-scripts or node-gyp, might be an essential part of the package you use. Using Bun, you can see some errors like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

error: could not determine executable to run for package


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Bun has a solution for it. First of all, it already has a list of trusted dependencies. For them, Bun will execute all necessary scripts by default. Otherwise, you can add it to trustedDependecies in your package.json file. In Bun community usage of trustedDependencies is a hot &lt;a href="https://github.com/oven-sh/bun/issues/4959" rel="noopener noreferrer"&gt;topic&lt;/a&gt;. There are &lt;a href="https://github.com/oven-sh/bun/issues/7594" rel="noopener noreferrer"&gt;several suggestions&lt;/a&gt; on how to improve it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tests with Bun
&lt;/h2&gt;

&lt;p&gt;After dealing with packages, we need to look at our tests. The first obstacle we faced here was the puppeteer. Puppeteer is a Node.js library that provides a high-level API to control Chrome/Chromium over the DevTools Protocol. It’s usually used for e2e testing or screenshot testing. This library has a script that installs Chromium by default, but we must find a solution as Bun skips it.&lt;/p&gt;

&lt;p&gt;As was discussed before, it’s about trustedDependencies. Although Bun already added Puppeteer to the list of trusted dependencies, it still skips the part with chromium installation. Adding this library to trustedDependecies in package.json file doesn’t help. During the test run,  no browser was installed. We found an &lt;a href="https://github.com/oven-sh/bun/issues/4894" rel="noopener noreferrer"&gt;open bug&lt;/a&gt; in the Bun repo with similar problems. It ended up with a separate script that installed the necessary browser for Puppeteer. Not a big deal.&lt;/p&gt;

&lt;p&gt;The other thing was the collision of webpack package versions, so we had to fix one specific version via the overrides option in the package.json file.&lt;/p&gt;

&lt;p&gt;So far, we used Bun for e2e end regression testing. But what about unit tests? Bun has the opportunity to get rid of any test runner because it has all the necessary instruments for it, similar to jest abilities. Unfortunately, it seems not to work with vue-test-utils, which is essential for our project, so we decided to keep it as it was, at least for now.&lt;/p&gt;

&lt;p&gt;After running this test, we found that something was wrong with the installed testing libraries and bumped into the following messages:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

- Test suite failed to run
  Cannot find module '../build/Release/canvas.node'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This time, the solution with trusted dependencies helps us.&lt;/p&gt;

&lt;h2&gt;
  
  
  GitHub actions
&lt;/h2&gt;

&lt;p&gt;Besides library installation and test running, running projects in dev and production mode didn’t cause any problems, so it is time to look at GitHub actions.&lt;/p&gt;

&lt;p&gt;First, we needed to use the official setup-bun GitHub action to use Bun. Second, we needed to rewrite all commands with bun usage instead of npm or npx.&lt;/p&gt;

&lt;p&gt;After that, there was a last question. Usually, we need to use some cache mechanism in GitHub actions for package installation. We dived into the documentation and GitHub issues, and according to this fantastic comment, there is no need for that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxabnmhlj9q1uuy6ha6j4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxabnmhlj9q1uuy6ha6j4.png" alt="Github comment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using Bun helped us reach our main goal with minimum effort from our team. We successfully implemented Bun in 2 of our projects, speeding up our pipeline time by 30%.&lt;/p&gt;

&lt;p&gt;For me, as a developer, it means the ability to deliver some changes faster without much time for waiting. It allows business to see actual changes more quickly, ultimately saving time and cost. Imagine how Bun might speed up processes on larger projects!&lt;/p&gt;

&lt;p&gt;We faced some problems, but all of them had solutions. It’s also worth mentioning that Bun creators and the community are aware of those problems and continue working on solutions.&lt;/p&gt;

&lt;p&gt;The next steps for our project will be improvements in tests and their adaptations for our team to use Bun entirely.&lt;/p&gt;

&lt;p&gt;It is also worth mentioning a few problems with Bun:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows support is still &lt;a href="https://github.com/oven-sh/bun/issues/43" rel="noopener noreferrer"&gt;experimental&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;It came out AFTER ChatGPT:) So, no help from AI.&lt;/li&gt;
&lt;li&gt;Bun still cannot completely &lt;a href="https://bun.sh/docs/runtime/nodejs-apis" rel="noopener noreferrer"&gt;replace node.js&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>bunjs</category>
      <category>vue</category>
      <category>nuxt</category>
    </item>
  </channel>
</rss>
