RequireJs with moment.js doesn't call `init`

My requirejs config declares moment.js and I would like to set the default locale when the library is needed. So I tried this:

require.config({
  enforceDefine: true,
  noGlobal: true,
  baseUrl: '/js',
  shim: {
    'moment': {
      init: function (moment) {
        console.log("*** Moment loaded!");
        return moment;
      }
    }
  },
  paths: {
    'moment': 'lib/moment-with-locales.min'

  }
});

      

However, the call require(['moment'], function (moment) { ... });

does not call the function init

inside the configuration shim

.

What am I not getting here?

+3


source to share


1 answer


Moment.js identifies itself as an AMD module and RequireJS will not call init

.

From RequireJS documentation:

The function init

will be called for non -AMD modules. For example, you cannot use the shim initialization function to call jQuery noConflict

. See Mapping Modules for using noConflict for an alternate jQuery approach.



From the main source Moment.js:

https://github.com/moment/moment/blob/develop/moment.js#L2796

+2


source







All Articles