fix issue #113
This commit is contained in:
parent
7457cd58b8
commit
7ab34980e4
1 changed files with 85 additions and 75 deletions
|
@ -56,7 +56,9 @@ module specification, the '.js' suffix is optional.
|
||||||
***/
|
***/
|
||||||
(function ( rootDir, modulePaths, hooks ) {
|
(function ( rootDir, modulePaths, hooks ) {
|
||||||
|
|
||||||
var File = java.io.File;
|
var File = java.io.File,
|
||||||
|
FileReader = java.io.FileReader,
|
||||||
|
BufferedReader = java.io.BufferedReader;
|
||||||
|
|
||||||
var readModuleFromDirectory = function( dir ) {
|
var readModuleFromDirectory = function( dir ) {
|
||||||
|
|
||||||
|
@ -170,82 +172,90 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
/*
|
/*
|
||||||
wph 20131215 Experimental
|
wph 20131215 Experimental
|
||||||
*/
|
*/
|
||||||
var _loadedModules = {};
|
var _loadedModules = {};
|
||||||
var _format = java.lang.String.format;
|
var _format = java.lang.String.format;
|
||||||
var _require = function(parentFile, path)
|
/*
|
||||||
{
|
require() function implementation
|
||||||
var file = resolveModuleToFile(path, parentFile);
|
*/
|
||||||
if (!file){
|
var _require = function( parentFile, path ) {
|
||||||
var errMsg = '' + _format("require() failed to find matching file for module '%s' " +
|
var file,
|
||||||
"in working directory '%s' ", [path, parentFile.canonicalPath]);
|
canonizedFilename,
|
||||||
if (! ( (''+path).match(/^\./) )){
|
moduleInfo,
|
||||||
errMsg = errMsg + ' and not found in paths ' + JSON.stringify(modulePaths);
|
buffered,
|
||||||
}
|
head = '(function(exports,module,require,__filename,__dirname){ ',
|
||||||
throw errMsg;
|
code = '',
|
||||||
}
|
line = null;
|
||||||
var canonizedFilename = _canonize(file);
|
|
||||||
|
|
||||||
var moduleInfo = _loadedModules[canonizedFilename];
|
file = resolveModuleToFile(path, parentFile);
|
||||||
if (moduleInfo){
|
if ( !file ) {
|
||||||
return moduleInfo;
|
var errMsg = '' + _format("require() failed to find matching file for module '%s' " +
|
||||||
}
|
"in working directory '%s' ", [path, parentFile.canonicalPath]);
|
||||||
if (hooks)
|
if (! ( (''+path).match( /^\./ ) ) ) {
|
||||||
hooks.loading(canonizedFilename);
|
errMsg = errMsg + ' and not found in paths ' + JSON.stringify(modulePaths);
|
||||||
var reader = new java.io.FileReader(file);
|
}
|
||||||
var br = new java.io.BufferedReader(reader);
|
throw errMsg;
|
||||||
var code = "";
|
}
|
||||||
var r = null;
|
canonizedFilename = _canonize(file);
|
||||||
while ((r = br.readLine()) !== null)
|
|
||||||
code += r + "\n";
|
|
||||||
|
|
||||||
var head = "(function(exports,module,require,__filename,__dirname){ ";
|
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 = {
|
moduleInfo = {
|
||||||
loaded: false,
|
loaded: false,
|
||||||
id: canonizedFilename,
|
id: canonizedFilename,
|
||||||
exports: {},
|
exports: {},
|
||||||
require: _requireClosure(file.parentFile)
|
require: _requireClosure(file.parentFile)
|
||||||
};
|
|
||||||
var tail = "})";
|
|
||||||
code = head + code + tail;
|
|
||||||
|
|
||||||
_loadedModules[canonizedFilename] = moduleInfo;
|
|
||||||
var compiledWrapper = null;
|
|
||||||
try {
|
|
||||||
compiledWrapper = eval(code);
|
|
||||||
}catch (e){
|
|
||||||
throw "Error:" + e + " while evaluating module " + canonizedFilename;
|
|
||||||
}
|
|
||||||
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 'Error:' + e + ' while executing module ' + canonizedFilename;
|
|
||||||
}
|
|
||||||
if (hooks)
|
|
||||||
hooks.loaded(canonizedFilename);
|
|
||||||
moduleInfo.loaded = true;
|
|
||||||
return moduleInfo;
|
|
||||||
};
|
};
|
||||||
|
var tail = '})';
|
||||||
|
code = head + code + tail;
|
||||||
|
|
||||||
var _requireClosure = function(parent){
|
_loadedModules[canonizedFilename] = moduleInfo;
|
||||||
return function(path){
|
var compiledWrapper = null;
|
||||||
var module = _require(parent, path);
|
try {
|
||||||
return module.exports;
|
compiledWrapper = eval(code);
|
||||||
};
|
} catch (e) {
|
||||||
|
throw 'Error:' + e + ' while evaluating module ' + canonizedFilename;
|
||||||
|
}
|
||||||
|
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 'Error:' + e + ' while executing module ' + canonizedFilename;
|
||||||
|
}
|
||||||
|
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));
|
};
|
||||||
|
return _requireClosure( new java.io.File(rootDir) );
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Reference in a new issue