-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire.js
139 lines (122 loc) · 2.96 KB
/
require.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
129
130
131
132
133
134
135
136
137
138
139
(function(root) {
var context = {
topModule: '',
modules: {},
loaded: [],
waiting: []
};
function genId() {
var time = new Date().getTime();
return time.toString(36);
}
function checkLoaded(dep) {
return context.loaded.indexOf(dep) > -1;
}
function checkAllLoaded() {
return context.waiting.length === 0;
}
/**
* define
* @param {String} name 模块名
* @param {Array} deps 依赖模块数组
* @param {Function} callback 回调函数
* @return
*/
root.define = function(name, deps, callback) {
// 无name require(['a', 'b'], function() {})
if (typeof name !== 'string') {
callback = deps;
deps = name;
name = null;
}
// 无deps require('a', function() {}) || require(function() {})
if (!Array.isArray(deps)) {
callback = deps;
deps = null;
}
name ? name : name = genId();
var module = {
name: name,
deps: deps,
callback: callback,
callbackReturn: null,
args: []
};
context.modules[name] = module;
if (deps !== null) {
deps.map(function(item) {
if (!checkLoaded(item)) {
context.waiting.push(item);
}
return item;
});
}
};
/**
* require
* @param {Array} deps 依赖模块数组
* @param {Function} callback 回调
* @return
*/
root.require = function(deps, callback) {
if (!Array.isArray(deps)) {
callback = deps;
deps = null;
}
var topId = genId();
context.topModule = topId;
var obj = {};
obj = {
name: topId,
deps: deps,
callback: callback,
callbackReturn: null,
args: []
};
context.modules[topId] = obj;
deps.map(function(item) {
if (!checkLoaded(item)) {
context.waiting.push(item);
}
return item;
});
context.waiting.map(function(dep) {
loadScript(dep);
});
};
/**
* 写入script
* @param {String} dep 模块名
*/
function loadScript(dep) {
var script = document.createElement('script');
script.src = './js/' + dep + '.js';
script.setAttribute('data-module', dep);
script.async = true;
document.querySelector('head').appendChild(script);
script.onload = loadAfter;
}
function loadAfter(e) {
console.log(e.target.dataset.module);
var loadModule = e.target.dataset.module;
context.loaded.push(loadModule);
var index = context.waiting.indexOf(loadModule);
context.waiting.splice(index, 1);
if (!checkAllLoaded()) {
return;
} else {
var topModule = context.modules[context.topModule];
execCallback(topModule);
}
}
// 执行回调
function execCallback(module) {
var args = module.args;
if (module.deps && module.deps.length !== 0) {
module.deps.map(function(item, i) {
args[i] = execCallback(context.modules[item]);
});
}
return module.callback.apply(module, args);
}
})(this);