They all define the rules of modules, such as module identification, module dependency, module implementation, module external interface, module loading;
There are asynchronous, synchronous, pre-dependent, near-dependent, pre-execution, lazy execution, soft dependency, hard dependency, and support types of module objects.
CommonJS
Common JS Modules/1.0 specification, Summary, wiki
On the server or the local desktop application, the module format is defined.
Only object type modules are supported and loaded synchronously.
For example, module qux:
// filename: qux.js
// dependencies
var $ = require('jquery');
// methods
function foo(){};
// exposed public method (single)
module.exports = foo;
// filename: qux.js
var $ = require('jquery');
var _ = require('underscore');
// methods
function foo(){}; // private because it's omitted from module.exports (see below)
function bar(){}; // public because it's defined in module.exports
function baz(){}; // public because it's defined in module.exports
// exposed public methods
module.exports = {
bar: bar,
baz: baz
};
AMD
Asynchronous Module Definition, Summary, Why AMD?
Asynchronous/Synchronous Loading
Support object, function, constructor, string, JSON and other types of modules.
Early implementation of AMD relies on pre-implementation and early execution
define(id?, dependencies?, factory);
For example, module qux:
// filename: qux.js
define(['jquery'], function ($) {
// methods
function foo(){};
// exposed public methods
return foo;
});
// filename: qux.js
define(['jquery', 'underscore'], function ($, _) {
// methods
function foo(){}; // private because it's not returned (see below)
function bar(){}; // public because it's returned
function baz(){}; // public because it's returned
// exposed public methods
return {
bar: bar,
baz: baz
}
});
Compatible with CommonJS specification:
// filename: qux.js
define(function (require, exports, module) {
// dependencies
var $ = require('jquery');
_ = require('underscore');
// exposed public methods
exports.foo= function () {}
});
CMD
Common Module Definition, Summary, Chinese
define(function(require,exports,module){...});
CMD implements dependency proximity and delays execution
UMD
Universal Module Definition
Identify the style of modules supported by the current environment and compatible with different loading specifications
// code from https://github.com/tangshuang/omd
(function(factory){
"use strict";
// change to your module name
var NAME = 'omd';
if(typeof define == 'function' && (define.cmd || define.amd)) { // amd & cmd
define(function(require){
var requires = {
/**
* dependences:
* 1. need to fill requires
* 2. use require, moudule name should be defined before
* 3. relative path is not allowed in cmd & amd mode
*/
// 'jquery' : require('jquery'),
// 'bootstrap' : require('bootstrap')
};
var _require_ = function(key) {
return requires[key];
}
return factory(_require_);
});
}
else if(typeof module !== 'undefined' && typeof exports === 'object') { // nodejs
module.exports = factory(require); // in node, require can be used everywhere generally
}
else { // none module
this[NAME] = factory(function(key){
return this[key];
}.bind(this));
}
}).call(this || (typeof window !== 'undefined' ? window : global),function(require){
"use strict";
// require('jquery');
/**
* use return object to export. i.e.
* return {
name : 'module name',
init : function(opotions) {},
destory : function() {}
};
*/
});
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery', 'underscore'], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory(require('jquery'), require('underscore'));
} else {
// Browser globals (root is window)
root.returnExports = factory(root.jQuery, root._);
}
}(this, function ($, _) {
// methods
function foo(){}; // private because it's not returned (see below)
function bar(){}; // public because it's returned
function baz(){}; // public because it's returned
// exposed public methods
return {
bar: bar,
baz: baz
}
}));
ES6
Quote from and expand reading:
- Modules - CommonJS Spec Wiki
- Modules | Node.js Documentation
- Why Web Modules?
- David Calhoun: What is AMD, CommonJS, and UMD?
- Huang Xuan: JavaScript Module Loader
- Huang Xuan: A Seven-Day Talk on JavaScript Modularization
- Ruan Yifeng: Module Grammar - Introduction to ECMAScript 6
- Ruan Yifeng: Javascript Modular Programming
- Nuggets: A Brief History of JavaScript Modules
- Yubo: The History of Front-end Modular Development. seajs/seajs