ember-cli-moment-shim
ember-cli ES6 module shim for momentjs and moment timezone within your Ember applications. It will also conditionally bundle in specific locale/timezone data for those concerned about payload size.
Usage
ember install ember-cli-moment-shim
import moment from 'moment';
Features
- ES6 accessible module for moment
- Trim your build sizes by bundling locale & timezone data through simple configuration
- FastBoot support
Enabling moment-timezone
module.exports = function(environment) {
return {
moment: {
includeTimezone: 'all'
}
};
}
i18n support
Cherry pick locales (optimal)
module.exports = function(environment) {
return {
moment: {
includeLocales: ['es', 'fr-ca']
}
};
NOTE: English is bundled automatically – no need to add en
in includeLocales
Include all locales
module.exports = function(environment) {
return {
moment: {
includeLocales: true
}
};
Configure default runtime locale
import moment from 'moment';
export default Ember.Route.extend({
beforeModel() {
moment.locale('es');
}
});
Single module use
The default behavior for loading moment locales is an IIFE which is added into
the vendor.js
file. The consequence of that is that you have no control over
when that code is run. That code also triggers setting of the moment locale,
which means that at arbitrary times in your application you don't know what the
locale is.
Included in ember-cli-moment-shim
is a module that allows you to specify which
localization you would like to load, and when you would like it invoked. This
enables you to configure timing based upon application constraints. (With the
initial state being en
.)
module.exports = function(environment) {
return {
moment: {
includeLocales: ['es', 'fr-ca'],
singleModule: true
}
};
import Route from '@ember/routing/route';
import defineLocale from 'ember-cli-moment-shim/define-locale';
import moment from 'moment';
import { inject as service } from '@ember/service';
export default Route.extend({
moment: service(),
beforeModel() {
const desiredLocale = 'en-us';
defineLocale(desiredLocale);
moment.locale(desiredLocale);
this.get('moment').setLocale(desiredLocale);
}
});
Write all locales to a folder that is relative to dist
module.exports = function(environment) {
return {
moment: {
localeOutputPath: 'assets/moment-locales'
}
};
The feature set for i18n support within moment can be found here: http://momentjs.com/docs/#/i18n/
License
ember-cli-moment-shim shims is MIT Licensed.