How to manage complex configuration of 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 take a look on different aspects of esbuild - a fast js bundler.
If you are interested in trying out esbuild with your Vue CLI generated app, in this article I will walk you through setting it up for a freshly generated application. This should be a good starting point for migrating your Vue application to esbuild...
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...

When you get to the point in your esbuild journey when you want to start using plugins, you can be surprised that all those are not available from the CLI interface. The only way of using them is from the build script, and therefore you will have to move the whole build configuration there. The documentation mentions the build script can be either in JS or GO - in this article I'll cover only the JS side.
As an example, I'll move the CLI build command from create-react-app article in this series:
"scripts": {
....
"esbuild": "esbuild src/index.js --bundle --outfile=dist/main.js --loader:.html=text --loader:.js=jsx --loader:.svg=dataurl"
}
and replace it by a build script. The example provided by the documentation:
require('esbuild').build({
entryPoints: ['app.jsx'],
bundle: true,
outfile: 'out.js',
}).catch(() => process.exit(1))
let us start, but leaves few important things out.
The build script that builds our code is as follow
#! node
require("esbuild")
.build({
entryPoints: ["src/index.js"],
bundle: true,
outfile: "dist/main.js",
loader: {
".js": "jsx",
".svg": "dataurl",
".html": "text",
},
})
.catch(() => process.exit(1));
If you save it as build.js in the top-level directory, you can run:
$ chmod +x build.js
If you compare how both scripts work, you will see one difference:
% npm run esbuild
> esbuild-cra@0.1.0 esbuild
> esbuild src/index.js --bundle --outfile=dist/main.js --loader:.html=text --loader:.js=jsx --loader:.svg=dataurl
dist/main.js 903.0kb
dist/main.css 1019b
⚡ Done in 60ms
% ./build.js
% (no output)
If you like the default logging level that is set for CLI build, you need to add:
require("esbuild")
.build({
logLevel: "info",
...
// rest of the configuration
after this change, the build.js runs like:
% ./build.js
dist/main.js 903.0kb
dist/main.css 1019b
⚡ Done in 62ms
You can check out my course about esbuild.
In this article, we have seen how to move non-trivial esbuild command from CLI parameters to JS script. You can find the complete code - the app & the build script in this repo+branch