Using Rollup for Angular 2's AoT compiler and importing Moment.js

后端 未结 9 1703
天涯浪人
天涯浪人 2020-12-24 13:34

I\'m trying to follow the official AoT guide for Angular 2, and I\'m using Moment.js in my application. Moment.js is on my packages.json file, and I\'m using versio

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 14:09

    Had same problem with momentJs (2.24) usage in my Angular 5 (5.2.9) project (upgraded from Ng2) with Gulp and Rollup (0.58.0) for prod build.

    As guys mentioned earlier here import * as moment from 'moment'; works only for deving (via SystemJS) with referring momentJs in packages list:

    {
      name: 'moment',
      path: 'node_modules/moment/min/moment.min.js'
    }
    

    Other case is Rollup usage (Production build) - momentJs have its code as ES6 module (in moment/src), but it exports in different way (then usual export). That's why import moment from 'moment'; works with Rollup's

    include: [
      'node_modules/rxjs/**',
    ]
    

    and ES6 modules import.

    Yes, to use ES6 wrapper (such as moment-es6 or so) is simple solution. But it always requires momentJs. Same time there is one more simple solution for this issue - replace your import row from Dev to Prod. For example, Gulp can use gulp-replace at some step:

    return gulp.src([
         ...
        ])
        .pipe(replace('import * as moment from \'moment\';', 'import moment from \'moment\';'))
         ...;
    

提交回复
热议问题