Javascript - Universal Module Definition (UMD)

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript)

1 - About

UMD

The UMD module format try to be the glue between differents modules definition by offering template.

Advertising

3 - Example

Template commonjsStrict.js that offers:

  • a Global Variable module
  • a CommonJS module
  • an AMD module
commonjsStrict.js
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['exports', 'b'], factory);
    } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
        // CommonJS
        factory(exports, require('b'));
    } else {
        // Browser globals
        factory((root.commonJsStrict = {}), root.b);
    }
}(this, function (exports, b) {
    //use b in some fashion.
 
    // attach properties to the exports object to define
    // the exported module properties.
    exports.action = function () {};
}));

3.1 - D3

(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
	typeof define === 'function' && define.amd ? define(['exports'], factory) :
	(factory((global.d3 = global.d3 || {})));
}(this, (function (exports) { 'use strict';
 
var version = "4.10.2";
 
// .........
// ........
exports.version;
exports.myFunction = myFunction;
 
Object.defineProperty(exports, '__esModule', { value: true });
 
})));
web/javascript/umd.txt · Last modified: 2019/02/16 17:40 by gerardnico