Is there any way to make Moment.js work with rollup without having to change this import statement:
import * as moment from "moment"
I tried to use rollup inject plugin but I didn't manage to make it work.
As explained here, since version 2.10.0, moment is written using ECMAScript 6 modules. The library should therefore be directly compatible with Rollup. Here's a solution using npm :
npm install moment --saveTo use npm modules with Rollup.js, you need to have these 2 plugins installed in your devDependencies: rollup-plugin-commonjs and rollup-plugin-node-resolve. The first one enables you to use Node's CommonJS modules as if they were ECMAScript modules (= use the import syntax to load them).
import moment from "moment";You can now use moment normally, for instance:
console.log(moment().format());
I'm new to Rollup and I've still trying to figure out how things work. I've made a simple test with the solution above and it seems to work, except that the whole library gets included in the bundle — the expected tree-shaking didn't happen and I don't know why.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With