-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrequire.js
154 lines (112 loc) · 3.63 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
;(function(root) {
function require(p) {
var path = require.resolve(p)
, mod = require.modules[path];
// weepy: following line added
if (!mod) mod = require.load(path);
if (!mod) throw new Error('failed to require "' + p + '"');
if (!mod.exports) {
mod.exports = {};
// weepy: added __filename and __dirname
var bits = path.split('/')
, __filename = bits.pop() + '.js'
, __dirname = bits.join('/')
mod.call(null, mod, mod.exports, require.relative(path), __filename, __dirname);
}
return mod.exports;
}
require.modules = {};
require.prefix = ""
require.resolve = function (path){
path = require.prefix + path
var orig = path
, reg = path + '.js'
, index = path + '/index.js';
return require.modules[reg] && reg
|| require.modules[index] && index
|| orig;
};
require.register = function (path, fn){
require.modules[path] = fn;
};
require.relative = function (parent) {
return function(p){
if ('.' != p.charAt(0)) return require(p);
var path = parent.split('/')
, segs = p.split('/');
path.pop();
var relative_path = path.concat(segs)
var normalized = []
for (var i = 0; i < relative_path.length; i++) {
var seg = relative_path[i];
if ('..' == seg && normalized.length && normalized[normalized.length-1].charAt(0) != ".") {
normalized.pop();
}
else if('.' == seg && normalized.length) {
//
}
else
normalized.push(seg)
}
return require(normalized.join('/'));
};
};
var compilers = require.compilers = {
js: function(text) {
return text
},
json: function(text) {
try {
JSON.parse(text)
} catch(e) {
console.error("BAD JSON @ " + text, e)
return
}
return "module.exports = " + text
},
txt: function(text) {
return "module.exports = " + JSON.stringify(text)
}
}
// weepy: following added
require.load = function( path ) {
var orig_path = path
var request = new XMLHttpRequest();
if(path.match(/\.[^./]+$/)) {
// has extension
} else {
path += ".js"
}
var ext = path.split(".").pop()
request.open('GET', path, false);
request.send();
var data = request.responseText;
if(!request.responseText || !request.responseText.length)
console.log("FAILED to load ", path)
console.log("xhr: ", path, " loaded ", request.responseText.length, " bytes")
var compiler = compilers[ext] || compilers.txt
var output = compiler(data) || ""
var text = "require.register('" + orig_path + "', \
function(module, exports, require, __filename, __dirname) {\n\n"
+ output+ "\n\n\
}); //@ sourceURL=" + path;
try {
(window.execScript || function(text) {
return window["eval"].call(window, text)
})(text)
}
catch(e) {
if(root.PARSEJS) {
try {
root.PARSEJS.parse(text)
} catch(e) {
console.error('Syntax error in file ' + path + ' at line:' + (e.line-2) + ', col:' + e.col + '. ' + e.message) //, e.stack) //, e)
}
} else {
console.error( "Syntax Error in file " + path + ": " + e.toString() )
}
}
return require.modules[ orig_path ];
}
root.require = require;
})(window);