-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmod.js
128 lines (106 loc) · 2.96 KB
/
mod.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
118
119
120
121
122
123
124
125
126
127
128
var require, define;
(function(self) {
var head = document.getElementsByTagName('head')[0],
resourceMap = {},
loadingMap = {},
factoryMap = {},
modulesMap = {};
function loadScript(id, callback) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = id;
head.appendChild(script);
var queue = loadingMap[id] || (loadingMap[id] = []);
queue.push(callback);
}
define = function(id, factory) {
factoryMap[id] = factory;
var queue = loadingMap[id];
if (queue) {
for(var i = queue.length - 1; i >= 0; --i) {
queue[i]();
}
delete loadingMap[id];
}
};
require = function(id) {
var mod = modulesMap[id];
if (mod) {
return mod['exports'];
}
//
// init module
//
var factory = factoryMap[id];
if (!factory) {
throw new Error('Cannot find module `' + id + '`');
}
mod = modulesMap[id] = {
'exports': {}
};
//
// factory: function OR value
//
var ret = (typeof factory === 'function')
? factory.apply(mod, [require, mod['exports'], mod])
: factory;
if (ret) {
mod['exports'] = ret;
}
return mod['exports'];
};
require.async = function(names, callback) {
if (typeof names === 'string') {
names = [names];
}
var needMap = {};
var needNum = 0;
function findNeed(depArr) {
for(var i = depArr.length - 1; i >= 0; --i) {
//
// skip loading or loaded
//
var dep = depArr[i];
if (dep in factoryMap || dep in needMap) {
continue;
}
needMap[dep] = true;
needNum++;
loadScript(dep, updateNeed);
var child = resourceMap[dep];
if (child) {
findNeed(child.deps);
}
}
}
function updateNeed() {
if (0 === needNum--) {
var i, args = [];
for(i = names.length - 1; i >= 0; --i) {
args[i] = require(names[i]);
}
callback.apply(self, args);
}
}
findNeed(names);
updateNeed();
};
require.resourceMap = function(obj) {
var res = obj['res'];
if (res) {
for(var k in res) {
if (res.hasOwnProperty(k)) {
resourceMap[k] = res[k];
}
}
}
var pkg = obj['pkg'];
if (pkg) {
//...
}
};
define.amd = {
'jQuery': true,
'version': '1.0.0'
};
})(this);