Javascript - Universal Module Definition (UMD)

1 - About

UMD

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

A UMD module is one that can either be used:


import moment = require("moment");
console.log(moment.format());

  • without import (without a module loader) as a global (when run in an environment without a module loader - ie browser environment).

console.log(moment.format());

3 - Identify

Template commonjsStrict.js that offers:

At the top of the file, it’s almost always a UMD library when you see tests as:

  • typeof define,
  • typeof window,
  • or typeof module

UMD modules check for the existence of a module loader environment with the following code:


(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 });

})));

4 - Documentation


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap