兼容umd的写法(amd、jquery、Commonjs、Node、global等)

umd的各种兼容写法,其中包含:amd、jquery、Commonjs、Node、global等,

方案一:umd Jquery plugin

1
2
3
4
5
6
7
8
9
10
11
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function($) {
$.fn.jqueryPlugin = function() {};
}));

方案二:umd Node adapter

1
2
3
4
5
6
7
8
9
10
// Help Node out by setting up define.
if (typeof module === 'object' && typeof define !== 'function') {
var define = function(factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var b = require('b');
return function() {};
});

方案三:umd Commonjs adapter

1
2
3
4
5
6
7
8
9
10
11
12
// Help Node out by setting up define.
if (typeof exports === 'object' && typeof define !== 'function') {
var define = function(factory) {
factory(require, exports, module);
};
}
define(function(require, exports, module) {
var b = require('b');
// Only attach properties to the exports object to define
// the module's properties.
exports.action = function() {};
});

方案四:umd Commonjs strict

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports', 'b'], factory);
} else if (typeof exports === 'object') {
// CommonJS
factory(exports, require('b'));
} else {
// Browser globals
factory((root.commonJsStrict = {}), root.b);
}
}(this, function(exports, b) {
// attach properties to the exports object to define
// the exported module properties.
exports.action = function() {};
}));

方案五:umd AMD or browser global

1
2
3
4
5
6
7
8
9
10
11
12
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['b'], factory);
} else {
// Browser globals
root.test = factory(root.b);
}
}(this, function(b) {
function test() {}
return test;
}));

方案六:umd AMD with global export

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['b'], function(b) {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
return (root.amdWebGlobal = factory(b));
});
} else {
// Browser globals
root.amdWebGlobal = factory(root.b);
}
}(this, function(b) {
function amdWebGlobal() {}
return amdWebGlobal;
}));

方案七:umd Commonjs strict with global

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports', 'b'], function(exports, b) {
factory((root.commonJsStrictGlobal = exports), b);
});
} else if (typeof exports === 'object') {
// CommonJS
factory(exports, require('b'));
} else {
// Browser globals
factory((root.commonJsStrictGlobal = {}), root.b);
}
}(this, function(exports, b) {
// attach properties to the exports object to define
// the exported module properties.
exports.action = function() {};
}));

方案八:umd jquery plugin with Commonjs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function($) {
$.fn.jqueryPlugin = function() {};
}));

其他方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//压缩迷你
! function(a) {
"use strict";
function test() {
return "Hello World!"
}
"function" == typeof define && define.amd ? define(function() {
return test
}) : "object" == typeof module && module.exports ? module.exports = test : a.hello = test
}(this);
//实例:
//var ok = require('assets/js/***.js');
// console.log(ok.hello());
//输出:Hello World!
//简化
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory;
} else {
root.dchart = factory;
}
}(this, function() {
function dchart(a) {
}
return dchart;
}));
//UMD模式之 AMD,Node,Browser globals 常用
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.test = factory();
}
}(this, function() {
function test() {
console.log('hello World');
}
return test;
}));
//UMD模式之 AMD,Node,Browser globals 常用二 简化
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.test = factory());
}(this, (function () { 'use strict';
return test;
})));