I'm creating my first AngularJS module intended for open source distribution. I'd like to package it in a way that's easy for others to consume.
The UMD project provides a pattern for exporting JavaScript modules that are compatible with AMD, CommonJS (or at least Node) and browser globals:
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['b'], factory); // AMD
} else if (typeof exports === 'object') {
module.exports = factory(require('b')); // Node
} else {
root.returnExports = factory(root.b); // browser global (root is window)
}
}(this, function (b) {
// use b in some fashion
return {}; // return a value to define the module export
}));
However, since AngularJS has its own internal module system, registering a module is done by simply calling a method on the angular
object, i.e. angular.module()
. Thus, a UMD module wouldn't need to export anything; it would just need to require and act on angular
. In terms of the previous example, I think that would look something like this:
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
factory(require(['b'])); // AMD
} else if (typeof exports === 'object') {
factory(require('b')); // Node
} else {
factory(root.b); // browser global (root is window)
}
}(this, function (b) {
// use b in some fashion
}));
Or, specific to my case:
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
factory(require(['angular'])); // AMD
} else if (typeof exports === 'object') {
factory(require('angular')); // Node
} else {
factory(root.angular); // browser global (root is window)
}
}(this, function (angular) {
angular.module( ... );
}));
Is this no big deal, or does it go against the spirit of UMD? I ask because I couldn't find any UMD patterns that don't export anything.