A Better Rollup Build

I’ve been experimenting with using rollup.js in a new project I’ve been working on lately. I love Rollup as a build tool because it allows me to author and publish my modules as ES2015 today. Until ES2015 module support is officially in Node, this is the best solution so far.

I’m not going to give a tutorial on how to use Rollup, since that’s not really the thing I want to focus on here, but I wanted to illustrate a better way to generate your builds with it, based on my own experience so far.

Since the main draw for me with using Rollup is publishing ES2015 modules (that, and tree-shaking), I can’t just generate one distributable in CommonJS format, for instance. Though, I do still need to provide the CJS build, for those who wish to consume my module the traditional way.

That being the case, I need two different builds, but the command line and the configuration-driven functionality of Rollup don’t provide me a simple one-shot method of accomplishing this. I would have to invoke the command twice or utilize two separate config files for each build target—even worse in the latter case because I still have to invoke the command twice to use the configuration files I created!

This ham-fisted approach felt very clunky and off-putting (though still great for simpler use-cases), but then I looked into Rollup’s third option: the JavaScript API—i.e. a proper script that generates the build targets through Rollup’s provided methods.

While this did give me a singular way of having multiple build targets, it still wasn’t quite optimal because it wasn’t immediately obvious that I could generate multiple builds using one invocation of Rollup; the documentation doesn’t demonstrate a more advanced build like this. After a moment the solution dawned on me: Rollup uses Promises, duh! Just lump the write out calls into one invocation!

And here’s the amazingly simple solution:

I highly recommend taking a look at rollup.js today if you have a similar desire to publish libraries as ES2015 modules.
Show your support

Clapping shows how much you appreciated Kevin Lanni’s story.