feat: compatible node_modules load logic

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-07 12:20:11 +08:00
parent bbf08816ba
commit de18b0f628
3 changed files with 14 additions and 16 deletions

View File

@ -10,6 +10,7 @@
return { return {
log: log, log: log,
info: log, info: log,
ex: log,
debug: global.debug ? _proxy('DEBUG') : global.noop, debug: global.debug ? _proxy('DEBUG') : global.noop,
warn: _proxy('WARN'), warn: _proxy('WARN'),
error: _proxy('ERROR') error: _proxy('ERROR')

View File

@ -1,5 +1,6 @@
(function(root, logger) { (function(root, logger) {
global.root = root; global.root = root;
global.logger = logger;
global.noop = global.engineDisable = function() { }; global.noop = global.engineDisable = function() { };
// disable // disable
global.engineLoad = load; global.engineLoad = load;

View File

@ -29,9 +29,9 @@
(function(parent) { (function(parent) {
'use strict'; 'use strict';
var File = Java.type("java.io.File"); var File = Java.type("java.io.File");
var FileNotFoundException = Java.type("java.io.FileNotFoundException");
var separatorChar = File.separatorChar; var separatorChar = File.separatorChar;
var cacheDir = parent + separatorChar + "runtime"; var cacheDir = parent + separatorChar + "runtime";
var paths = [parent + separatorChar + 'node_modules', parent];
function __assign(t) { function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) { for (var s, i = 1, n = arguments.length; i < n; i++) {
@ -104,25 +104,21 @@
name = _canonical(name) || name; name = _canonical(name) || name;
// 解析本地目录 // 解析本地目录
if (name.startsWith('./') || name.startsWith('../')) { if (name.startsWith('./') || name.startsWith('../')) {
return resolveAsFile(parent, name) || resolveAsDirectory(parent, name) || undefined; return resolveAsFile(name, parent) || resolveAsDirectory(name, parent) || undefined;
} else { } else {
// 查找可能存在的路径 // 解析Node目录
for (var i in paths) { var dir = [parent, 'node_modules'].join(separatorChar);
var path = paths[i]; return resolveAsFile(name, dir) ||
var result = resolveAsFile(path, name) || resolveAsDirectory(path, name); resolveAsDirectory(name, dir) ||
if (result) { (parent && parent.toString().startsWith(root) ? resolve(name, new File(parent).getParent()) : undefined);
return result;
}
}
} }
return undefined;
} }
/** /**
* 解析文件 * 解析文件
* @returns {*} * @returns {*}
*/ */
function resolveAsFile(dir, file) { function resolveAsFile(file, dir) {
file = dir != undefined ? new File(dir, file) : new File(file); file = dir != undefined ? new File(dir, file) : new File(file);
// 直接文件 // 直接文件
if (file.isFile()) { if (file.isFile()) {
@ -144,17 +140,17 @@
* 解析目录 * 解析目录
* @returns {*} * @returns {*}
*/ */
function resolveAsDirectory(dir, file) { function resolveAsDirectory(file, dir) {
dir = dir != undefined ? new File(dir, file) : new File(file); dir = dir != undefined ? new File(dir, file) : new File(file);
var _package = new File(dir, 'package.json'); var _package = new File(dir, 'package.json');
if (_package.exists()) { if (_package.exists()) {
var json = JSON.parse(base.read(_package)); var json = JSON.parse(base.read(_package));
if (json.main) { if (json.main) {
return resolveAsFile(dir, json.main); return resolveAsFile(json.main, dir);
} }
} }
// if no package or package.main exists, look for index.js // if no package or package.main exists, look for index.js
return resolveAsFile(dir, 'index.js'); return resolveAsFile('index.js', dir);
} }
/** /**
@ -259,7 +255,7 @@
file = _isFile(file) ? file : resolve(name, path); file = _isFile(file) ? file : resolve(name, path);
optional = __assign({ cache: true }, optional); optional = __assign({ cache: true }, optional);
if (file === undefined) { if (file === undefined) {
throw Error("Can't found module " + name + " in directory " + path) throw new FileNotFoundException("Can't found module " + name + " in directory " + path)
} }
// 重定向文件名称和类型 // 重定向文件名称和类型
return getCacheModule(_canonical(file), file.name.split(".")[0], file, optional); return getCacheModule(_canonical(file), file.name.split(".")[0], file, optional);