-
Notifications
You must be signed in to change notification settings - Fork 51
/
lodjs.js
466 lines (423 loc) · 13.4 KB
/
lodjs.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
;(function (root) {
'use strict';
var modMap = [];
var moduleMap = [];
var toString = {}.toString;
var slice = [].slice;
var doc = document;
var head = doc.head || doc.getElementsByTagName("head")[0] || doc.documentElement;
var docCharset = doc.charset;
var docUrl = location.href.split('?')[0];//去除问号之后部分
var baseUrl = getCurSrc() || docUrl;
var gid = 0;
var commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;
var cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g;
var interactiveScript = null;
var currentlyAddingScript = null;
var curExecModName = null;
var t = (new Date).getTime();
var o = {};
function getGid() {
return gid++;
}
function getType(x) {
if(x === null){
return 'null';
}
var t= typeof x;
if(t !== 'object'){
return t;
}
var c = toString.call(x).slice(8, -1).toLowerCase();
if(c !== 'object'){
return c;
}
if(x.constructor==Object){
return c;
}
return 'unkonw';
}
function isArr(arr) {
return Array.isArray ? Array.isArray(arr) : getType(arr) === 'array';
}
function isObj(obj) {
return getType(obj) === 'object';
}
function isFn(fn) {
return getType(fn) === 'function';
}
function extendDeep() {
var target = arguments[0] || {};
var arrs = slice.call(arguments, 1);
var len = arrs.length;
var copyIsArr;
var clone;
for (var i = 0; i < len; i++) {
var arr = arrs[i];
for (var name in arr) {
var src = target[name];
var copy = arr[name];
//避免无限循环
if (target === copy) {
continue;
}
if (copy && (isObj(copy) || (copyIsArr = isArr(copy)))) {
if (copyIsArr) {
copyIsArr = false;
clone = src && isArr(src) ? src : [];
} else {
clone = src && isObj(src) ? src : {};
}
target[ name ] = extendDeep(clone, copy);
} else if (typeof copy !== 'undefined'){
target[name] = copy;
}
}
}
return target;
}
function loadjs(src, success, error, option) {
var d = extendDeep({
charset: docCharset,
cache: o.cache
}, option);
if (d.cache) {
src += '?t=' + t;
}
var node = doc.createElement('script');
node.src = src;
node.id = 'lodjs-js-' + getGid();
node.charset = d.charset;
if ('onload' in node) {
node.onload = success;
node.onerror = error;
} else {
node.onreadystatechange = function() {
if (/loaded|complete/.test(node.readyState)) {
success();
}
}
}
currentlyAddingScript = node;
head.appendChild(node);
currentlyAddingScript = null;
}
function getCurSrc() {
if(doc.currentScript){
return doc.currentScript.src;
}
if (currentlyAddingScript) {
return currentlyAddingScript.src;
}
// For IE6-9 browsers, the script onload event may not fire right
// after the script is evaluated. Kris Zyp found that it
// could query the script nodes and the one that is in "interactive"
// mode indicates the current script
// ref: http://goo.gl/JHfFW
if (interactiveScript && interactiveScript.readyState === "interactive") {
return interactiveScript.src;
}
var scripts = head.getElementsByTagName("script");
for (var i = scripts.length - 1; i >= 0; i--) {
var script = scripts[i];
if (script.readyState === "interactive") {
interactiveScript = script;
return interactiveScript.src;
}
}
return null;
}
function isUrl(url) {
return url.search(/^(http:\/\/|https:\/\/|\/\/)/) !== -1;
}
function fixUrl(url) {
return url.replace(/([^:])\/+/g, '$1/');
}
function getUrl(path, url) {
//绝对网址
if (isUrl(path)) {
return fixUrl(path);
}
var rootUrl;
//修复url
if (rootUrl = url.match(/[^\/]*\/\/[^\/]*\//)) {
//http://yanhaijing.com/abc
url = url.slice(0, url.lastIndexOf('/') + 1);
rootUrl = rootUrl[0];
} else {
//http://yanhaijing.com
rootUrl = url = url + '/';
}
// /开头
if (path.search(/^\//) !== -1) {
return fixUrl(rootUrl + path);
}
// ../开头
if (path.search(/^\.\.\//) !== -1) {
while(path.search(/^\.\.\//) !== -1) {
if (url.lastIndexOf('/', url.length - 2) !== -1) {
path = path.slice(3);
url = url.slice(0, url.lastIndexOf('/', url.length - 2) + 1);
} else {
throw new Error('lodjs geturl error, cannot find path in url');
}
}
return fixUrl(url + path);
}
// ./
path = path.search(/^\.\//) !== -1 ? path.slice(2) : path;
return fixUrl(url + path);
}
function fixSuffix(url, suffix) {
var reg = new RegExp('\\.' + suffix + '$', 'i');
return url.search(reg) !== -1 ? url : url + '.' + suffix;
}
function replacePath(id) {
var ids = id.split('/');
// id中不包含路径 或 查找路径失败
if (ids.length < 2 || !(ids[0] in o.path)) {
return id;
}
ids[0] = o.path[ids[0]];
return ids.join('/');
}
function getDepUrl(id, url) {
var pathId = replacePath(id);
//找到path 基于baseUrl
if (pathId !== id) {
url = o.baseUrl;
}
return fixSuffix(getUrl(pathId, url || o.baseUrl), 'js');
}
function getIdUrl(id){
//没有id的情况
if (!id) {
return getCurSrc();
}
//id不能为相对路径,amd规定此处也不能带后缀,此处放宽限制。
if (id.search(/^\./) !== -1) {
throw new Error('lodjs define id' + id + 'must absolute');
}
return fixSuffix(getUrl(id, o.baseUrl), 'js');
}
function require(id, url) {
var url = getDepUrl(id, url || curExecModName);
return moduleMap[url] && moduleMap[url].exports;
}
function fixPath(path) {
//path是网址
if (isUrl(path)) {
return getUrl('./', path).slice(0, -1);
}
return path;
}
function config(option) {
if (!isObj(option)) {
return extendDeep({}, o);
}
//处理baseUrl
if (option.baseUrl) {
option.baseUrl = getUrl(option.baseUrl, docUrl);
}
//处理path
if (isObj(option.path)) {
for(var key in option.path) {
option.path[key] = fixPath(option.path[key]);
}
}
o = extendDeep(o, option);
//fix keywords
o.path.BASEURL = fixPath(option.baseUrl || o.baseUrl);
o.path.DOCURL = fixPath(docUrl);
return extendDeep({}, o);
}
function execMod(modName, callback, params) {
//判断定义的是函数还是非函数
if (!params) {
moduleMap[modName].exports = modMap[modName].callback;
} else {
curExecModName = modName;
//commonjs
var exp = modMap[modName].callback.apply(null, params);
curExecModName = null;
//amd和返回值的commonjs
if (exp) {
moduleMap[modName].exports = exp;
}
}
//执行回调函数
callback(moduleMap[modName].exports);
//执行complete队列
execComplete(modName);
}
function execComplete(modName) {
//模块定义完毕 执行load函数,当加载失败时,会不存在module
for (var i = 0; i < modMap[modName].oncomplete.length; i++) {
modMap[modName].oncomplete[i](moduleMap[modName] && moduleMap[modName].exports);
}
//释放内存
modMap[modName].oncomplete = [];
}
function loadMod(id, callback, option) {
//commonjs
if(id === 'require') {
callback(require);
return -1;
}
if (id === 'exports') {
var exports = moduleMap[option.baseUrl].exports = {};
callback(exports);
return -2;
}
if (id === 'module') {
callback(moduleMap[option.baseUrl]);
return -3;
}
var modName = getDepUrl(id, option.baseUrl);
//未加载
if (!modMap[modName]) {
modMap[modName] = {
status: 'loading',
oncomplete: []
};
loadjs(modName, function () {
//如果define的不是函数
if (!isFn(modMap[modName].callback)) {
execMod(modName, callback);
return 0;
}
//define的是函数
use(modMap[modName].deps, function () {
execMod(modName, callback, slice.call(arguments, 0));
}, {baseUrl: modName});
return 1;
}, function () {
modMap[modName].status === 'error';
callback();
execComplete(modName);//加载失败执行队列
});
return 0;
}
//加载失败
if (modMap[modName].status === 'error') {
callback();
return 1;
}
//正在加载
if (modMap[modName].status === 'loading') {
modMap[modName].oncomplete.push(callback);
return 1;
}
//加载完成
//尚未执行完成
if (!moduleMap[modName].exports) {
//如果define的不是函数
if (!isFn(modMap[modName].callback)) {
execMod(modName, callback);
return 2;
}
//define的是函数
use(modMap[modName].deps, function () {
execMod(modName, callback, slice.call(arguments, 0));
}, {baseUrl: modName});
return 3;
}
//已经执行过
callback(moduleMap[modName].exports);
return 4;
}
function use(deps, callback, option) {
if (arguments.length < 2) {
throw new Error('lodjs.use arguments miss');
return 0;
}
if (typeof deps === 'string') {
deps = [deps];
}
if (!isArr(deps) || !isFn(callback)) {
throw new Error('lodjs.use arguments type error');
return 1;
}
//默认为当前脚本的路径或baseurl
if (!isObj(option)) {
option = {};
}
option.baseUrl = option.baseUrl || o.baseUrl;
if (deps.length === 0) {
callback();
return 2;
}
var depsCount = deps.length;
var params = [];
for(var i = 0; i < deps.length; i++) {
(function (j) {
loadMod(deps[j], function (param) {
depsCount--;
params[j] = param;
if (depsCount === 0) {
callback.apply(null, params);
}
}, option);
}(i));
}
return 3;
}
function define(name, deps, callback) {
//省略模块名
if (typeof name !== 'string') {
callback = deps;
deps = name;
name = null;
}
//无依赖
if (!isArr(deps)) {
callback = deps;
deps = [];
}
//支持commonjs
if (deps.length === 0 && isFn(callback) && callback.length) {
callback
.toString()
.replace(commentRegExp, '')
.replace(cjsRequireRegExp, function (match, dep) {
deps.push(dep);
});
var arr = ['require'];
if (callback.length > 1) {
arr.push('exports');
}
if (callback.length > 2) {
arr.push('module');
}
deps = arr.concat(deps);
}
var modName = getIdUrl(name).split('?')[0];//fix 后缀
modMap[modName] = modMap[modName] || {};
modMap[modName].deps = deps;
modMap[modName].callback = callback;
modMap[modName].status = 'loaded';
modMap[modName].oncomplete = modMap[modName].oncomplete || [];
moduleMap[modName] = {};
return 0;
}
define.amd = {from: 'lodjs'};
function debug() {
console.log(modMap, moduleMap);
}
var lodjs = {
version: '0.1.0',
use: use,
loadjs: loadjs,
config: config,
define: define,
require: require,
debug: debug
};
lodjs.config({
baseUrl: baseUrl,
path: {},
cache: false
});
root.define = define;
root.lodjs = lodjs;
}(window));