I have an NPM library, calendar-dates built with RollUp.JS (“RollUp” hereafter).
[!] (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.
transform-class-properties plugin in
Below was my original
The configuration takes
src/index.js file as an input then
- Resolves package (resolve())
- Converts CommonJS to ES6 (commonjs())
- Minimize the package (uglify())
- Converts ES6 to ES5 (babel())
Lastly, outputs two files
- calendardates.cjs.js – Common JS module – Enables the library to be imported using
- calendardates.esm.js – ES6 module – Enables the library to be imported using
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
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.