I think all JavaScript module writers should start using UMD when defining their modules. Yes, it’s a bit messy and seemingly unnecessary boilerplate. But if you really cared about your users, then you’ll do it. :)

Even if you don’t like using an AMD like RequireJS, you will do your users a big favour by using one of the UMD variants.

For example, a jQuery plugin can be defined as follows:

// Uses AMD or browser globals to create a jQuery plugin.

// It does not try to register in a CommonJS environment since
// jQuery is not likely to run in those environments.
// See jqueryPluginCommonJs.js for that version.

(function (factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['jquery'], factory);
    } else {
        // Browser globals
}(function ($) {
    $.fn.jqueryPlugin = function () {};

This is taken straight from the umdjs repo.

blog comments powered by Disqus