-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathplugin-base.js
122 lines (88 loc) · 2.34 KB
/
plugin-base.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
/**
* The base utilities for plugin development
*/
define('seajs/plugin-base', [], function(require, exports) {
var pluginSDK = seajs.pluginSDK
var util = pluginSDK.util
var Module = pluginSDK.Module
var pluginsInfo = {}
var uriCache = {}
exports.add = function(o) {
pluginsInfo[o.name] = o
}
exports.util = {
xhr: xhr,
globalEval: globalEval,
}
extendResolve()
extendFetch()
function extendResolve() {
var _resolve = Module._resolve
Module._resolve = function(id, refUri) {
var pluginName
var m
// id = text!path/to/some
if (m = id.match(/^(\w+)!(.+)$/)) {
pluginName = m[1]
id = m[2]
}
// Parse alias first
id = '#' + util.parseAlias(id)
// id = abc.xyz?t=123
if (!pluginName && (m = id.match(/[^?]*(\.\w+)/))) {
var ext = m[1]
for (var k in pluginsInfo) {
if (pluginsInfo.hasOwnProperty(k) &&
util.indexOf(pluginsInfo[k].ext, ext) > -1) {
pluginName = k
break
}
}
}
// Prevents adding the default `.js` extension
if (pluginName && !/\?|#$/.test(id)) {
id += '#'
}
var uri = _resolve(id, refUri)
if (pluginName && pluginsInfo[pluginName] && !uriCache[uri]) {
uriCache[uri] = pluginName
}
return uri
}
}
function extendFetch() {
var _fetch = Module._fetch
Module._fetch = function(url, callback, charset) {
var pluginName = uriCache[util.unParseMap(url)]
if (pluginName) {
pluginsInfo[pluginName].fetch(url, callback, charset)
return
}
_fetch(url, callback, charset)
}
}
function xhr(url, callback) {
var r = window.ActiveXObject ?
new window.ActiveXObject('Microsoft.XMLHTTP')
: new window.XMLHttpRequest()
r.open('GET', url, true)
r.onreadystatechange = function() {
if (r.readyState === 4) {
if (r.status === 200) {
callback(r.responseText)
}
else {
throw new Error('Could not load: ' + url + ', status = ' + r.status)
}
}
}
return r.send(null)
}
function globalEval(data) {
if (data && /\S/.test(data)) {
(window.execScript || function(data) {
window['eval'].call(window, data)
})(data)
}
}
});