Build A Library With esbuild
How to bundle ESM, IIFE or CommonJS libraries with esbuild.
I recently developed plugins and, migrated all the utilities of DeckDeckGo to build these with esbuild.
If you are looking to do the same, hope this tutorial helps you get started!
Introduction
esbuild is “an extremely fast JavaScript bundler” made by Evan Wallace. It is its tagline and, according my tests, the least we can say is that it is true. It is blazing fast ⚡️.
Sometimes while migrating my libraries, I even found myself waiting for the end of a build because I did not notice that it was already finished. I assumed it would still need some more time, old habits die hard I guess 😅.
In addition, other things which make me really like this new bundler are its clean, flexible API and, its documentation. It is easy to follow and, clear.
Setup
To get started, let’s create a new empty project.
mkdir mylib && cd mylib && npm init --yes
You can use esbuild to bundle libraries from vanilla JavaScript source files but, at least in this tutorial, we are going to use TypeScript too. That’s why, in addition to the bundler, we also install it and rimraf, to remove…