How to start building a project with esbuild
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
Search for a command to run...
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
No comments yet. Be the first to comment.
In this series, I'll show simple but complete application set-up with esbuild.
In this article, I'll show how to add a development server to the simple application we started in part 1. Possible approaches There are two alternative ways we can achieve our goal. Autorebuild The first is to have the same setup we had in part 1, ...
I’ve been working in programming for the last 16 years. Let’s see what changes I see in our industry that are enabled by generative AI. Rapid prototyping The last few months, I’ve been working on a set of WordPress plugins, related to event organizat...
Writing unit tests takes time and effort. Nonetheless, many teams insist on writing them anyway—that’s because of the benefits they bring to a project. Those benefits are mainly the following: fast feedback—unit tests speed up each iteration of twea...

Pure functions are the perfect case for unit testing. For a given input, we always expect the same output—there is no internal state involved. Let’s take a look at a few examples and some simple tests that check if the methods work as expected. Jasmi...

Let’s say you have a job interview in a few days. How should you prepare for it so that you can make an informed decision about joining the company, as well as make sure that your interests are taken care of? Prepare your questions An interview is a ...

Creating example projects is a common way of showing your skills to potential employers. Let’s take a look at what’s important to keep in mind when building personal projects with an eye toward impressing prospective employers. Simplicity Building ap...

This article will show how to start a project with esbuild as a bundler. I assume you have:
In an empty folder named esbuild-tutorial, I run:
$ npm init -y
Wrote to /home/marcin/workspace/github/esbuild-tutorial/package.json:
{
"name": "esbuild-tutorial",
"version": "1.0.0",
...
This creates package.json for our project, and the -yattribute makes it use default values instead of asking questions interactively. Each value can be changed later on, directly in the package file.
For now, there will be 2 files in our project.
www/index.html:
<html>
<head>
<title>esbuild tutorial</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="#" />
<script type="module" src="./main.js"></script>
</head>
<body></body>
</html>
The code meaning is as follow:
<title>... - sets what appears in the browser tab, or title bar<meta>... - we set the encoding instead of forcing the browser to use default & complain in the console log.<link rel="shortcut icon" href="#" /> - stop the browser from loading default icon form /favicon.ico, which doesn't exist <script type="module" src="./main.js"></script> - loads js we will create in the next stepsrc/index.js:
console.log("Lorem ipsum");
A simple code that will allow us to see in the browser console if the code is built and included as expected.
Before we can build, we have to install esbuild:
$ npm install esbuild --save-dev
> esbuild@0.12.19 postinstall /home/marcin/workspace/github/esbuild-tutorial/node_modules/esbuild
> node install.js
npm notice created a lockfile as package-lock.json. You should commit this file.
+ esbuild@0.12.19
added 1 package and audited 1 package in 0.586s
found 0 vulnerabilities
By adding --save-dev, npm adds the package as a dev dependency. It means it will be installed automatically when we run npm install on the project in a new location.
If you use git, it's a good idea to add node_modules to .gitignore. It will stop git from including external packages in the repository - we have npm that takes care of it. You can do it quickly with:
$ echo node_modules >> .gitignore
You can already build the application by running:
$ ./node_modules/.bin/esbuild --bundle src/index.js --outfile=www/main.js
www/main.js 63b
⚡ Done in 8ms
As it's not handy to type such a long command, we can add a script command to package.json:
{
...
"scripts": {
...
"build": "esbuild --bundle src/index.js --outfile=www/main.js"
},
...
}
With this in place, we can run the build with:
$ npm run build
> esbuild-tutorial@1.0.0 build /home/marcin/workspace/github/esbuild-tutorial
> esbuild --bundle src/index.js --outfile=www/main.js
www/main.js 63b
⚡ Done in 1ms
You can find the code in the step-1 branch of the example repository.
You can check out my video course about esbuild.
We have seen how to start a simple, hello-world example with esbuild. Stay tuned for the following parts.