How to set up CSS Modules 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 take a look on different aspects of esbuild - a fast js bundler.
This article will present a simple code generator for kicking off a browser project that uses esbuild as a bundler. Code generator For generating code, I'm using degit. It allows you to start a new project directly from a GitHub repository without dr...
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 configure CSS Modules with esbuild.
It is a technic of making our styles scoped locally. I first encountered it while working on this SolidJS + esbuild. My understanding is that it's popular among developers using react.
My code is pretty simples. src/style.module.css:
.test {
color: #66f;
}
src/index.js:
import styles from "./style.module.css";
const header = document.createElement("h1");
header.innerHTML = "Hello world";
header.className = styles.test;
document.body.appendChild(header);
This is combined in a way that ensures no css class collision:
<style>
._test_1f4jo_1 {
color: #66f;
}
</style>
...
<h1 class="_test_1f4jo_1">Hello world</h1>
Esbuild needs a plugin to support CSS modules. On the plugin list, there are 2 listed. I used esbuild-css-modules-plugin here. The dependencies needed to run it all successfully:
$ npm install --save-dev esbuild-css-modules-plugin esbuild
An additional dependency is needed to address a tiny dependency issue:
$ npm install --save-dev css-tree
For using a plugin with esbuild, we need a build script. build.js:
#!/usr/bin/env node
const cssModulesPlugin = require("esbuild-css-modules-plugin");
require("esbuild")
.build({
logLevel: "info",
entryPoints: ["src/index.js"],
bundle: true,
outfile: "dist/main.js",
plugins: [cssModulesPlugin()],
})
.catch(() => process.exit(1));
If you run chmod +x build.js, you will be able to run it:
$ ./build.js
dist/main.js 722b
⚡ Done in 16ms
You can check out my course about esbuild.
In this article, we have seen how to configure CSS Modules with esbuild.