How to set up ES module library for the frontend JavaScript
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.
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...

This article will show how to set up an npm library package as an ES module that can be easily used in other packages, with a minimum size impact.
I generated the package.json with npm run -y. The only difference was adding "type": "module". library/package.json:
{
"name": "library",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
The library itself is very simple, library/index.js:
const valueA = "function A",
valueB = "function B";
export function functionA() {
return valueA;
}
export function functionB() {
return valueB;
}
By moving values to const, I was hoping to trick bundler leave some redundant code, but they managed pretty well.
As simple the library is the test application webpack/src/index.js & esbuild/src/index.js:
import { functionA } from "../../library";
console.log("Hello!", functionA());
Alternatively, I could import the library as a whole:
import * as library from "../../library";
console.log("Hello!", library.functionA());
But in my simplified example, both bundlers manage just fine to leave unnecessary code behind.
I generated my code with my webpack-starter. The only change to webpack configuration was switching to production mode - otherwise, the build was full of comments. The resulting build command:
"build": "webpack --mode=production"
I used similar generate as above (esbuild-starter), and switched to minified output - so both bundlers are used in similar circumstances. The build command:
"build": "esbuild --bundle src/index.js --outfile=dist/main.js --minify"
Both bundlers build the application code as they should - importing the one method we used and ignoring the other one. Interestingly, we didn't need to set "sideEffects": false to make it happen - just using ES modules seems to be enough.
(()=>{"use strict";console.log("Hello!","function A")})();
Webpack (or Babel) is pretty smart about minification - it reduces all my code into a static value that is always the output.
(()=>{var o="function A";function n(){return o}console.log("Hello!",n());})();
esbuild is less efficient with simplification, but it correctly removes the other exported function.
I cover basics of bundler in those video courses:
In this article, we have seen one of the approaches you can take to build a JS library as an ES module.