I encountered the same issues that were mentioned earlier.
The code
import * as moment from 'moment';
worked fine when I was developing and loading via systemjs, but it didn't work during rollup.
On the other hand, import moment from 'moment';
worked in a rollup build but not during development.
To solve this issue without changing the code depending on the build type, I decided to add moment as a global variable and created a helper function that I import wherever I need to use it instead of directly importing moment.
This approach allows me to use the same code in both scenarios, although it may not be the most elegant solution. If there is a better way, please share!
Here's the helper function, which I added to its own file called momentLoader.ts
:
import { default as mom } from 'moment';
export default function moment(args?: any): mom.Moment {
let m = window["moment"];
if (!m) {
console.error("moment does not exist globally.");
return undefined;
}
return m(args);
}
To use moment in other classes, I simply import the function and call it as if I had imported moment directly:
import moment from '../../momentLoader';
let d = moment().utc("1995-12-25");
let m = moment("1995-12-25");
In order to load moment as a global variable using systemjs, I followed these steps:
http://momentjs.com/docs/#/use-it/system-js/
In my case, the moment configuration for systemjs looks like this:
let meta = {
'lib:moment/moment.js': { format: 'global' }
};
System.config({
paths: paths,
map: map,
packages: packages,
meta: meta
});
System.import('lib:moment/moment.js');
For the rollup build, make sure to include moment.js on the page using a script tag, as it won't be included in the rollup build file by default.