-
Notifications
You must be signed in to change notification settings - Fork 0
/
define.js
117 lines (105 loc) · 3.11 KB
/
define.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(function (global, factory) {
if (typeof module == 'object' && typeof exports == 'object') {
module.exports = factory();
} else {
global.define = factory();
}
}(this, function () {
'use strict';
var loadedModules = {},
queuedModules = [],
dependenciesAreLoaded = function (dependencies) {
for (var i = 0; i < dependencies.length; i++) {
if (!loadedModules.hasOwnProperty(dependencies[i])) {
return false;
}
}
return true;
},
queueModule = function (id, dependencies, factory) {
queuedModules.push([id, dependencies, factory]);
},
evaluateQueuedModules = function (id) {
var module,
i = 0;
while (i < queuedModules.length) {
module = queuedModules[i];
if (dependenciesAreLoaded(module[1])) {
queuedModules.splice(i, 1);
loadModule.apply(undefined, module);
} else {
i++;
}
}
},
loadModule = function (id, dependencies, factory) {
var module = {},
exports;
dependencies = dependencies.map(function (dependency) {
if (dependency === 'module') {
return module;
}
if (dependency === 'exports') {
module.exports = {};
return module.exports;
}
return loadedModules[dependency];
});
exports = factory.apply(global, dependencies);
if (exports) {
module.exports = exports;
}
if (id) {
module.id = id;
loadedModules[id] = module.exports;
evaluateQueuedModules(id);
}
},
define = function (id, dependencies, factory) {
// `define(id, dependencies, factory)`
// `define(dependencies, factory)`
// `define(id, factory)`
// `define(factory)`
var args = Array.prototype.slice.call(arguments),
toString = function (object) {
return Object.prototype.toString.call(object);
},
object;
if (toString(args[0]) === '[object String]') {
id = args.shift();
} else {
id = undefined;
}
if (toString(args[0]) === '[object Array]') {
dependencies = args.shift();
} else {
dependencies = ['require', 'exports', 'module'];
}
if (toString(args[0]) === '[object Object]') {
object = args.shift();
factory = function () {
return object;
};
} else {
factory = args.shift();
}
if (dependenciesAreLoaded(dependencies)) {
loadModule(id, dependencies, factory);
} else {
queueModule(id, dependencies, factory);
}
};
define.amd = {};
define('require', [], function () {
return function (id) {
if (typeof id === 'string') {
return loadedModules[id];
} else {
define.apply(global, arguments);
}
};
});
define('exports', [], function () {});
define('module', [], function () {});
return define;
}));