How to manage CSS 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 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,...
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 styling to our application. The starting point is where we left in step 2.
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 to complicate the esbuild setup. Let's set src/index.js to:
import "./style.css";
const header = document.createElement("h1");
header.innerHTML = "Hello world";
document.body.appendChild(header);
import "./style.css";- by default, esbuild has CSS loader set up, but styles are not included in JS bundle. Before we get to it, we have to add the ./style.css because now it's failing to buildconst header = ... & the following lines - simple code to add element to the page. By doing it in JS, by one glimpse, we can tell if the JS is working or not.The styling goes to ./src/style.css:
body {
color: #66f;
}
If we build our application with npm run build or start the server with npm run start, we will see the header without the color. That's because styles are emitted to style file - with the same name as our bundle, but with .css extension.
To include the styling we have to add:
<link rel="stylesheet" type="text/css" href="./main.css"/>
With this in place, the application should look like this:

You can check out my video course about esbuild.
In this article, we have seen how to add styling to our esbuild application. If you are interested in the hearing when there is a new part ready, you can sign up: