How to configure esbuild with a build script
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 set up a complete development server for esbuild. It's a replacement for the half-successful approach I had in the previous post. Dependency This approach is based on esbuild-serve - a nice package that allows us to ...
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...

In this article, I will show how to use build scripts in esbuild. So far in this series, we've been using command-line (CLI) arguments to configure our builds. As our configuration is growing, it can become cumbersome. And what's even more important, some esbuild features - such as plugins - are not available from CLI.
I want to keep the npm scripts set up in the dev sever part. Plus, I still want to avoid code duplication.
First, let's start by adding an executable file. The easiest way will be:
$ touch build.js
(no output)
$ chmod +x build.js
(no output)
To make the file executable with node, we have to start the file with:
#!/usr/bin/env node
We can add a simple console.log to see if it works as expected:
console.log('test');
With all this in place, we can call it directly from the terminal:
$ ./build.js
test
(thanks to BenVida for pointing it our in a comment to other article).
Now, we can make the build script work for our basic case npm run build. Let's set the build.js set:
#!/usr/bin/env node
require("esbuild")
.build({
logLevel: "info",
entryPoints: ["src/index.js"],
bundle: true,
outfile: "www/main.js",
})
.catch(() => process.exit(1));
A longer discussion of the basic build is my older post.
We can already use this script via npm run build, and we can update package.json:
{
...
"scripts": {
...
"build": "./build.js",
...
With this, our build works:
$ npm run build
> esbuild-tutorial@1.0.0 build /home/marcin/workspace/github/esbuild-tutorial
> ./build.js
www/main.js 151b
www/main.css 44b
⚡ Done in 2ms
tuxy [npm run build] ~/workspace/github/esbuild-tutorial 8:09
tuxy% npm run watch
> esbuild-tutorial@1.0.0 watch /home/marcin/workspace/github/esbuild-tutorial
But unfortunately, the parameters we add for watch or start are ignored:
$ npm run build -- --watch
> esbuild-tutorial@1.0.0 build /home/marcin/workspace/github/esbuild-tutorial
> ./build.js "--watch"
www/main.js 151b
www/main.css 44b
⚡ Done in 2ms
You can check out my video course about esbuild.
In this article, we have started refactoring towards build script with esbuild. The current script works fine for our build script, and I will address the other two in next article in this series.