Rollup Class Properties Transform Babel Plugin Issue

May 16, 2018
💫 Originally posted here. Broken? Let me know ~

I have an NPM library, calendar-dates built with RollUp.JS (“RollUp” hereafter).
As I was using an experimental JavaScript feature, Class properties transform (to declare a static property in an ES6 class) with RollUp, I ran into the following error 🙅.

[!] (commonjs plugin) SyntaxError: Unexpected token

I’ll show you what I did and how I fixed the error.
(I will assume that you are familiar with static class properties in ES6 and RollUp)

🤔 What I did before getting the error

My main class CalendarDates is declared with the static property, monthTypes, which caused the error in the introduction.

I installed the Class properties transform Babel plugin to the project as a devdependency.

And added transform-class-properties plugin in .babelrc file.

Below was my original rollup.config.js file.

The configuration takes src/index.js file as an input then

  1. Resolves package (resolve())
  2. Converts CommonJS to ES6 (commonjs())
  3. Minimize the package (uglify())
  4. Converts ES6 to ES5 (babel())

Lastly, outputs two files

  1. calendardates.cjs.js – Common JS module – Enables the library to be imported using require()
  2. calendardates.esm.js – ES6 module – Enables the library to be imported using import()

The logic above worked until I added Class transform properties plugin.

⛏️ How I Fixed It

It turns out that my logic was half-right.

What I had to do  was to transpile (with Babel) before piping the code into commonjs().

Basically Babel converts ES6 code into ES5 and then commonjs() converts that ES5 back to ES6 so that the library can be import‘ed in ES6 code.
The order of how plugins are added matters!

Here is the result after the change.

I hope you were making 😁 if you were having a similar issue.

🎐 Parting Words

Yeah, it was as simple as just moving plugin methods around.
Learned something new that the order of plugin declaration mattered.

I only guessed to change the order around as I was used to using middlewares in ASP.NET Core, and Express.
It seems like many programming concepts can be found in many places and can be used.

Here is the link to the updated rollup.config.js.