-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire.js
239 lines (205 loc) · 6.54 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
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
require = (function($root) {
var BufferedReader = Java.type('java.io.BufferedReader');
var InputStreamReader = Java.type('java.io.InputStreamReader');
var FileInputStream = Java.type('java.io.FileInputStream');
var BufferedWriter = Java.type('java.io.BufferedWriter');
var OutputStreamWriter = Java.type('java.io.OutputStreamWriter');
var FileOutputStream = Java.type('java.io.FileOutputStream');
var File = Java.type('java.io.File');
var Path = Java.type('java.nio.file.Paths');
var isWin32 = Java.type('java.lang.System').getProperty('os.name').startsWith('Windows');
var sep = isWin32 ? '\\' : '/';
var wrapper = [
'(function (exports, module, require, __filename, __dirname) {',
'\n})'
];
function Exports() {
this._isDefault = true;
}
function ExtLoader(ext, handler) {
this.ext = ext[0] === '.' ? ext : '.' + ext;
this.handler = handler;
}
function Module(id, filename) {
this.id = id;
this.filename = filename;
this.fn = new Function();
this.children = {};
this.exports = new Exports();
this.isLoaded = false;
}
Module._packageCache = Object.create(null);
Module._exts = Object.create(null);
Module._exts['.js'] = new ExtLoader('.js', function(input) {
return input;
});
Module._exts['.json'] = new ExtLoader('.json', function(input) {
return JSON.parse(input);
});
function fs_read(location) {
var fIn = new BufferedReader(new InputStreamReader(new FileInputStream(location), "UTF8"));
var line;
var string = "";
while ((line = fIn.readLine()) != null) {
string += line + '\n';
}
fIn.close();
return string;
}
function fs_exists(path) {
return new File(path).exists();
}
function path_absolute(path) {
return Path.get(path).toAbsolutePath().toString();
}
function path_normalize(path) {
return Path.get(path).normalize().toString();
}
function path_dirname(path) {
var result = '';
var subs = path.split(sep);
for (var i = 0; i < subs.length - 1; i++) {
result += (subs[i] + sep);
}
return result;
}
function path_resolve() {
var paths = Array.prototype.slice.call(arguments);
if (paths.length === 0) return '';
var lastPath = Path.get(paths[0]);
for (var i = 1; i < paths.length; i++) {
lastPath = lastPath.resolve(Path.get(paths[i]));
}
return lastPath.toString();
}
function wrap(script) {
return wrapper[0] + script + wrapper[1];
}
function isRequestRelative(request) {
return request[0] == '.' && (request[1] == sep || request[1] == '.');
}
function assureExt(requestPath) {
if (fs_exists(requestPath)) return requestPath;
for (var field in Module._exts) {
var loader = Module._exts[field];
// at this point, we haven't found the file, so plug in the extensions and see if they exists
var realPath = requestPath + loader.ext;
if (fs_exists(realPath)) return realPath;
}
throw new Error('No loader for ' + requestPath + ' exists. To add one, reference require.exts');
}
function getLoader(filename) {
for (var field in Module._exts) {
if (filename.endsWith(field)) return Module._exts[field];
}
return Module._exts['.js'];
}
function resolveEntry(requestPath) {
try {
var jsonPath = path_resolve(requestPath, 'package.json');
var json = fs_read(jsonPath);
return JSON.parse(json).main;
} catch (ex) {
throw new Error('Failed to configure module ' + requestPath + ': ' + ex.message);
}
}
/**
* Resolves the relative path to execution and the module that called the require.
* @param {String} request
* @param {Module} caller
*/
function resolveFile(request, caller) {
if (!caller) caller = { filename: $root };
var dir = path_dirname(caller.filename);
return path_resolve(dir, request);
}
function tryFile(request, caller) {
if (isRequestRelative(request)) {
return resolveFile(request, caller);
} else {
return resolveEntry(path_resolve($root, request));
}
}
// 1: from the request, determine what file the request is pointing to and return a Module for it
function resolveModule(request, caller) {
var file = tryFile(request, caller);
var isRelative = isRequestRelative(request);
if (!file && !isRelative) {
// this will happen if the package.json doesn't include a 'main' property
file = path_resolve($root, request, 'index.js');
} else {
// if we have a 'main' property in the package
if (isRelative) {
file = assureExt(file);
} else {
file = path_resolve($root, request, file);
}
}
file = path_normalize(file);
if (Module._packageCache[file]) return Module._packageCache[file];
return new Module(request, file, isRelative ? caller : undefined);
}
// 2: from the returned Module, compile it and return it.
function compileModule(module) {
var loader = getLoader(module.filename);
var script = fs_read(module.filename);
var compiled = loader.handler(script); // if the file is JSON, then this will return a JSON object
if (typeof compiled == 'string') {
// if compiled is a string, that means we need to compile and eval
var wrapped = wrap(compiled);
module.fn = eval(wrapped);
} else {
// if compiled is an object, then we need to set the function as settings the module's exports
module.fn = function() {
module.exports = compiled;
}
}
}
// 3: from the compiled Module, ensure the safety of the object and cache it.
function cacheModule(module) {
Module._packageCache[module.filename] = module;
}
// 4: from the completed Module, run the body function and set the exports
function exportModule(module) {
var args = [
// exports
module.exports,
// module
module,
// require,
function (request) {
return require(request, module)
},
// __filename
module.filename,
// __dirname
path_dirname(module.filename)
];
try {
module.fn.apply(null, args);
} catch (ex) {
console.log('\xA7cAn error occured when reading ' + module.filename);
console.log(ex.getStackTrace());
throw ex;
}
module.isLoaded = true;
}
function require(request, caller) {
if (request[0] == '@') return eval(request.substr(1));
if (isWin32) request = request.replace('/', '\\');
else request = request.replace('\\', '/');
var module = resolveModule(request, caller);
if (Module._packageCache[module.filename]) {
return Module._packageCache[module.filename].exports;
}
compileModule(module);
exportModule(module);
cacheModule(module);
return module.exports;
}
require.unregisterModules = function() {
Module._packageCache = Object.create(null);
}
require.exts = Module._exts;
return require;
})('./plugins/Thiq/node_modules'); // the root location of the installed modules