-
Notifications
You must be signed in to change notification settings - Fork 3
/
require.js
174 lines (161 loc) · 4.83 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
(function ( rootDir, modulePaths, hooks ) {
var File = java.io.File,
FileReader = java.io.FileReader,
BufferedReader = java.io.BufferedReader;
var readModuleFromDirectory = function( dir ) {
// look for a package.json file
var pkgJsonFile = new File( dir, './package.json' );
if ( pkgJsonFile.exists() ) {
var pkg = scload( pkgJsonFile );
var mainFile = new File( dir, pkg.main );
if ( mainFile.exists() ) {
return mainFile;
} else {
return null;
}
} else {
// look for an index.js file
var indexJsFile = new File( dir, './index.js' );
if ( indexJsFile.exists() ) {
return indexJsFile;
} else {
return null;
}
}
};
var fileExists = function( file ) {
if ( file.isDirectory() ) {
return readModuleFromDirectory( file );
} else {
return file;
}
};
var _canonize = function(file){
return "" + file.canonicalPath.replaceAll("\\\\","/");
};
var resolveModuleToFile = function ( moduleName, parentDir ) {
var file = new File(moduleName);
if ( file.exists() ) {
return fileExists(file);
}
if ( moduleName.match( /^[^\.\/]/ ) ) {
// it's a module named like so ... 'events' , 'net/http'
//
var resolvedFile;
for ( var i = 0; i < modulePaths.length; i++ ) {
resolvedFile = new File( modulePaths[i] + moduleName );
if ( resolvedFile.exists() ) {
return fileExists( resolvedFile );
} else {
// try appending a .js to the end
resolvedFile = new File( modulePaths[i] + moduleName + '.js' );
if ( resolvedFile.exists() ) {
return resolvedFile;
}
}
}
} else {
// it's of the form ./path
file = new File( parentDir, moduleName );
if ( file.exists() ) {
return fileExists(file);
} else {
// try appending a .js to the end
var pathWithJSExt = file.canonicalPath + '.js';
file = new File( parentDir, pathWithJSExt);
if ( file.exists() ) {
return file;
} else {
file = new File( pathWithJSExt );
if ( file.exists() ) {
return file;
}
}
}
}
return null;
};
var _loadedModules = {};
var fmt = java.lang.String.format;
/*
require() function implementation
*/
var _require = function( parentFile, path ) {
var file,
canonizedFilename,
moduleInfo,
buffered,
head = '(function(exports,module,require,__filename,__dirname){ ',
code = '',
tail = '})',
line = null;
file = resolveModuleToFile(path, parentFile);
if ( !file ) {
var errMsg = '' + fmt("require() failed to find matching file for module '%s' " +
"in working directory '%s' ", [path, parentFile.canonicalPath]);
if ( ! ( (''+path).match( /^\./ ) ) ) {
errMsg = errMsg + ' and not found in paths ' + JSON.stringify(modulePaths);
}
throw errMsg;
}
canonizedFilename = _canonize(file);
moduleInfo = _loadedModules[canonizedFilename];
if ( moduleInfo ) {
return moduleInfo;
}
if ( hooks ) {
hooks.loading( canonizedFilename );
}
buffered = new BufferedReader(new FileReader(file));
while ( ( line = buffered.readLine()) !== null ) {
code += line + '\n';
}
buffered.close(); // close the stream so there's no file locks
moduleInfo = {
loaded: false,
id: canonizedFilename,
exports: {},
require: _requireClosure(file.parentFile)
};
code = head + code + tail;
_loadedModules[canonizedFilename] = moduleInfo;
var compiledWrapper = null;
try {
compiledWrapper = eval(code);
} catch (e) {
throw new Error( "Error evaluating module " + path
+ " line #" + e.lineNumber
+ " : " + e.message, canonizedFilename, e.lineNumber );
}
var __dirname = '' + file.parentFile.canonicalPath;
var parameters = [
moduleInfo.exports, /* exports */
moduleInfo, /* module */
moduleInfo.require, /* require */
canonizedFilename, /* __filename */
__dirname /* __dirname */
];
try {
compiledWrapper
.apply(moduleInfo.exports, /* this */
parameters);
} catch (e) {
throw new Error( "Error executing module " + path
+ " line #" + e.lineNumber
+ " : " + e.message, canonizedFilename, e.lineNumber );
}
if ( hooks ) {
hooks.loaded( canonizedFilename );
}
moduleInfo.loaded = true;
return moduleInfo;
};
var _requireClosure = function( parent ) {
return function( path ) {
var module = _require( parent, path );
return module.exports;
};
};
return _requireClosure( new java.io.File(rootDir) );
// last line deliberately has no semicolon!
})