Develop a JS lib these days

Posted on April 12, 2019 in
2 min read

I made several attempts in the past trying to develop javascript libraries. Recently, I wanted to include in my current side-project a lib I built a while ago.

I've realized, quite immediately, that my little piece of code was not compatible anymore with the modern way of building web-app (read: import in a ES6 or Vue-cli projects). I succeed to include it with some hacks but, most importantly, I've realized that I should update my knowledge in the field.

The time has come and here my first attempt at grasping the whole stuff.

The goal

Here my goal: I want to write a js library the modern way, that means:

- write it in ES6 (or other additional flavors)

- use the module loader (import/export) for peace of mind

- build a package that is compatible both in the browser and node.js

- have a developer-friendly setup (watcher and reloader during development session)

The Proof-of-concept

Here the repo with my P-O-C which I'm quite satisfied with.

Rollup is the bundler that does almost all of the hard work, Babel to make it compatible with ES5.

So far, so good since the same bundle can be used in the browser, such as:

<script src="mylib.js"></script>
<script>
  mylib.mod1.method()
</script>

and in Node.js with:

var { mod1 } = require('mylib')
mod1.method()

All that with a nice and modular development experience.