UMD

Universal Module Definition 通用模块定义规范
可以通过运行时,或者编译时,
让同一个代码模块在使用 CommonJs、CMD 甚至是 AMD 的项目中运行

UMD 没有自己专有的规范,是集结了 CommonJs、CMD、AMD 的规范于一身

  • 在定义模块的时候,会检测当前使用环境和模块的定义方式
  • 将各种模块化定义方式转化为同样一种写法
  • UMD 规范将浏览器端、服务器端甚至是 APP 端都大统一
    1. ((root, factory) => {
    2. if (typeof define === 'function' && define.amd) {
    3. //AMD
    4. define(['jquery'], factory);
    5. } else if (typeof exports === 'object') {
    6. //CommonJS
    7. var $ = requie('jquery');
    8. module.exports = factory($);
    9. } else {
    10. root.testModule = factory(root.jQuery);
    11. }
    12. })(this, ($) => {
    13. //todo
    14. });

https://www.jianshu.com/p/6e61bf5c4d23

AMD

Asynchronous Module Definition 异步模块定义规范
CommonJs 模块化规范的超集

  1. define(id?, dependencies?, factory);
  1. // moudle-a.js
  2. define('moudleA', function() {
  3. return {
  4. a: 1
  5. }
  6. });
  7. // moudle-b.js
  8. define(['moudleA'], function(ma) {
  9. var b = ma.a + 2;
  10. return {
  11. b: b
  12. }
  13. });

CMD

Common Module Definiton 通用模块定义规范
运行于浏览器之上, Sea.js
**

  1. define(factory);

**

  1. // moudle-a.js
  2. define(function(require, exports, module) {
  3. module.exports = {
  4. a: 1
  5. };
  6. });
  7. // moudle-b.js
  8. define(function(require, exports, module) {
  9. var ma = require('./moudle-a');
  10. var b = ma.a + 2;
  11. module.exports = {
  12. b: b
  13. };
  14. });

commonJS

CommonJs 是一种 JavaScript 语言的模块化规范,通常会在服务端的 Nodejs 上使用

  1. //moudle-a.js
  2. moudle.exports = {
  3. a: 1
  4. };
  5. //moudle-b.js
  6. var ma = require('./moudle-a');
  7. var b = ma.a + 2;
  8. module.exports ={
  9. b: b
  10. };