I often have a LibraryPlugins
module which I load in my initial require
call. In that module, I load up all of the plugins I want available everywhere - like knockout bindings, underscore mixins, jquery plugins, etc. Since it's one of my first require calls, I know the extensions it adds are generally available throughout my application
Sometimes I will take a more modular approach and have one extra module for each library which returns the library it's operating on (i.e. one for knockout with custom bindings that returns ko and one for underscore with custom mixins that returns a modified underscore). That approach ends up looking much like Louis's answer, but without the extra map:*
config. Some modules expose themselves as a named module which makes it a bit more difficult to work with - that's when you'd use the map:* alias like in Louis's solution. For projects where I'm not using a ton of different libraries, I tend to just have one LibraryPlugins
module as it is easier to manage when there's a small number of dependencies.
Here's a jsfiddle where I show both approaches.
For convenience I'll also paste the code in here:
require.config({
paths: {
'knockout':'//cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min',
'knockout.validation': '//cdnjs.cloudflare.com/ajax/libs/knockout-validation/1.0.2/knockout.validation.min',
'underscore.core': 'http://underscorejs.org/underscore-min',
'underscore.string': 'http://epeli.github.com/underscore.string/lib/underscore.string'
},
shim: {
'underscore.core': {exports:'_'}
}
});
// Approach 1: One module to load everything in advance
define('LibraryPlugins', ['knockout', 'knockout.validation'], function(ko){ });
// Approach 2: One module for each library
define('underscore', ['underscore.core', 'underscore.string'], function (_, _string) {
_.mixin(_string.exports());
return _;
});
require(['knockout', 'underscore', 'LibraryPlugins'], function(ko, _) {
console.log(ko.validation);
console.log(_.humanize);
});