I'm using Moment.js on a few different pages in an international site. Each page uses require.js to require in Moment.
Moment allows you to set the locale to display dates in that locales format. However, this code has to be run on every page Moment is loaded (since I'm never sure which page the user loads first).
var Moment = require('moment');
// configure locale
What I want to do is move the code to configure the locale to something like a pre-require step so that I don't have to remember to configure the locale on every page (and it becomes more DRY). So all I would have to do is
var Moment = require('moment');
and the pre-require step would already have configured the locale.
Is this possible?
Yes, it is possible, like this:
Create a module that will be called
moment-configured
:Use a
map
configuration so that everybody who loads moment really loads yourmoment-configured
module whereasmoment-configured
loads the realmoment
:This method is adapted from the example in RequireJS' documentation about how to have jQuery be automatically loaded with
noConflict
. With this map configuration, all modules that requiremoment
will getmoment-configured
, except formoment-configured
which will get the realmoment
.The require config object can be given a callback to be executed after dependencies are loaded and before require() is defined: