How to set up a dev server 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 styling to our application. The starting point is where we left in step 2. JS To start, let's replace our dummy JS with code that at least put something on the screen. I go with vanilla JS because frameworks tend...
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'll show how to add a development server to the simple application we started in part 1.
There are two alternative ways we can achieve our goal.
The first is to have the same setup we had in part 1, but watch files automatically rebuild them. This is provided by the --watch option - the documation. In our case, we can define a new npm script in package.json:
{
...
"scripts": {
...
"scripts": {
...
"watch": "esbuild --bundle src/index.js --outfile=www/main.js --watch"
...
With this in place, we can start the watch with:
$ npm run watch
> esbuild-tutorial@1.0.0 watch /home/marcin/workspace/github/esbuild-tutorial
> esbuild --bundle src/index.js --outfile=www/main.js --watch
[watch] build finished, watching for changes...
Then, after each change, the code is rebuilt:
[watch] build started (change: "src/index.js")
[watch] build finished
We access the page in the same as before - in my case, I have it at http://localhost/github/esbuild-tutorial/www/
Another approach is to use the development server provided by esbuild. From the approaches presented in the documentation, the server everything one is simple to set up in our case.
When we add to package.json:
{
...
"scripts": {
...
"scripts": {
...
"start": "esbuild --bundle src/index.js --outfile=www/main.js --servedir=www",
...
We can start a dev server with:
npm run start
> esbuild-tutorial@1.0.0 start /home/marcin/workspace/github/esbuild-tutorial
> esbuild --bundle src/index.js --outfile=www/main.js --servedir=www
> Local: http://127.0.0.1:8000/
> Network: http://192.168.2.107:8000/
> Network: http://172.18.0.1:8000/
> Network: http://172.17.0.1:8000/
Then, every reload of the page cause the build to be rerun:
127.0.0.1:44180 - "GET /" 200 [1ms]
127.0.0.1:44180 - "GET /main.js" 200 [0ms]
Not building ahead of time is a neat feature possible thanks to lighting fast builds in esbuild.
If you have an HTTP server set up to point out to your work space, the auto rebuild can be a quick & dirty way to improve your workflow. Otherwise, the dev server is the way to go.
With our current scripts, we have:
{
...
"scripts": {
...
"build": "esbuild --bundle src/index.js --outfile=www/main.js",
"start": "esbuild --bundle src/index.js --outfile=www/main.js --servedir=www",
"watch": "esbuild --bundle src/index.js --outfile=www/main.js --watch"
...
The part repeated in all 3 places - esbuild --bundle src/index.js --outfile=www/main.js will easily become a headache when needed to be changed. We can reuse the shortest command and add the additional flag to it in the others:
{
...
"scripts": {
...
"build": "esbuild --bundle src/index.js --outfile=www/main.js",
"start": "npm run build -- --servedir=www",
"watch": "npm run build -- --watch"
},
...
The code is in in example repository, in the step-2 branch.
You can check out my video course about esbuild.
In this article, we have seen how to set up a dev server. In the next article, we will make our application a bit more complicated & we will add styling. If you want to be updated when the next article is published, you can sign up here: