feat: add ts defiend
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
46925f9b52
commit
2a891c1117
@ -24,7 +24,13 @@
|
||||
* 3. 如果 xx/index.json 存在 则使用 `xx/index.json` 解析为对象加载 并停止执行
|
||||
* 暂不支持 4. 如果 xx/index.msm 是一个文件 则使用MScript解析器解析 并停止执行
|
||||
*/
|
||||
(function(parent) {
|
||||
// @ts-check
|
||||
/// <reference types="@ms/nashorn" />
|
||||
(
|
||||
/**
|
||||
* @param {any} parent
|
||||
*/
|
||||
function(parent) {
|
||||
'use strict';
|
||||
var File = Java.type('java.io.File');
|
||||
var Paths = Java.type('java.nio.file.Paths');
|
||||
@ -37,10 +43,14 @@
|
||||
var BufferedInputStream = Java.type('java.io.BufferedInputStream');
|
||||
|
||||
var URL = Java.type('java.net.URL')
|
||||
var JavaString = Java.type('java.lang.String')
|
||||
var separatorChar = File.separatorChar;
|
||||
|
||||
var CoreModules = ['assert', 'async_hooks', 'child_process', 'cluster', 'crypto', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'inspector', 'net', 'os', 'path', 'vm', 'url', 'util', 'zlib', 'worker_threads']
|
||||
|
||||
/**
|
||||
* @param {...object} t
|
||||
*/
|
||||
function __assign(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
@ -53,7 +63,7 @@
|
||||
|
||||
/**
|
||||
* 判断是否为一个文件
|
||||
* @param file
|
||||
* @param {any} file
|
||||
* @returns {*}
|
||||
*/
|
||||
function _isFile(file) {
|
||||
@ -62,7 +72,7 @@
|
||||
|
||||
/**
|
||||
* 获得文件规范路径
|
||||
* @param file
|
||||
* @param {any} file
|
||||
* @returns {*}
|
||||
*/
|
||||
function _canonical(file) {
|
||||
@ -71,7 +81,7 @@
|
||||
|
||||
/**
|
||||
* 获得文件绝对路径
|
||||
* @param file
|
||||
* @param {any} file
|
||||
* @returns {*}
|
||||
*/
|
||||
function _absolute(file) {
|
||||
@ -84,8 +94,8 @@
|
||||
* 当前目录 ./
|
||||
* 父目录 ../
|
||||
* 模块目录 /node_modules
|
||||
* @param name 模块名称
|
||||
* @param parent 父目录
|
||||
* @param {string} name 模块名称
|
||||
* @param {string} parent 父目录
|
||||
*/
|
||||
function resolve(name, parent) {
|
||||
name = _canonical(name) || name;
|
||||
@ -97,6 +107,7 @@
|
||||
var dir = [parent, 'node_modules'].join(separatorChar);
|
||||
if (cacheModuleIds[name]) return cacheModuleIds[name]
|
||||
cacheModuleIds[name] = resolveAsFile(name, dir) || resolveAsDirectory(name, dir) ||
|
||||
// @ts-ignore
|
||||
(parent && parent.toString().startsWith(root) ? resolve(name, new File(parent).getParent()) : undefined);
|
||||
return cacheModuleIds[name];
|
||||
}
|
||||
@ -104,13 +115,14 @@
|
||||
|
||||
/**
|
||||
* 解析文件
|
||||
* @param file 文件
|
||||
* @param dir 目录
|
||||
* @param {string} file 文件
|
||||
* @param {string | undefined} dir 目录
|
||||
* @returns {*}
|
||||
*/
|
||||
function resolveAsFile(file, dir) {
|
||||
file = dir != undefined ? new File(dir, file) : new File(file);
|
||||
// 直接文件
|
||||
// @ts-ignore
|
||||
if (file.isFile()) {
|
||||
return file;
|
||||
}
|
||||
@ -128,14 +140,15 @@
|
||||
|
||||
/**
|
||||
* 解析目录
|
||||
* @param file 文件
|
||||
* @param dir 目录
|
||||
* @param {string} file 文件
|
||||
* @param {string | undefined} dir 目录
|
||||
* @returns {*}
|
||||
*/
|
||||
function resolveAsDirectory(file, dir) {
|
||||
dir = dir != undefined ? new File(dir, file) : new File(file);
|
||||
var _package = new File(dir, 'package.json');
|
||||
if (_package.exists()) {
|
||||
// @ts-ignore
|
||||
var json = JSON.parse(base.read(_package));
|
||||
if (json.main) {
|
||||
return resolveAsFile(json.main, dir);
|
||||
@ -147,8 +160,8 @@
|
||||
|
||||
/**
|
||||
* 后缀检测和添加
|
||||
* @param fileName 文件名称
|
||||
* @param ext 后缀
|
||||
* @param {string} fileName 文件名称
|
||||
* @param {string} ext 后缀
|
||||
* @returns {*}
|
||||
*/
|
||||
function normalizeName(fileName, ext) {
|
||||
@ -160,11 +173,11 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译模块
|
||||
* @param id 模块ID
|
||||
* @param name 模块名称
|
||||
* @param file 模块文件
|
||||
* @param optional 附加选项
|
||||
* 检查模块缓存
|
||||
* @param {string} id 模块ID
|
||||
* @param {string} name 模块名称
|
||||
* @param {any} file 模块文件
|
||||
* @param {any} optional 附加选项
|
||||
* @returns {Object}
|
||||
*/
|
||||
function getCacheModule(id, name, file, optional) {
|
||||
@ -172,12 +185,24 @@
|
||||
if (optional.cache && module) {
|
||||
return module;
|
||||
}
|
||||
return createModule(id, name, file, optional)
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译模块
|
||||
* @param {string} id 模块ID
|
||||
* @param {string} name 模块名称
|
||||
* @param {any} file 模块文件
|
||||
* @param {any} optional 附加选项
|
||||
* @returns {Object}
|
||||
*/
|
||||
function createModule(id, name, file, optional) {
|
||||
console.trace('Loading module', name + '(' + id + ')', 'Optional', JSON.stringify(optional));
|
||||
module = {
|
||||
var module = {
|
||||
id: id,
|
||||
exports: {},
|
||||
loaded: false,
|
||||
require: getRequire(file.parentFile)
|
||||
require: getRequire(file.parentFile, id)
|
||||
};
|
||||
cacheModules[id] = module;
|
||||
var cfile = _canonical(file);
|
||||
@ -195,17 +220,19 @@
|
||||
|
||||
/**
|
||||
* 预编译JS
|
||||
* @param module JS模块
|
||||
* @param file JS文件
|
||||
* @param optional 附加选项
|
||||
* @returns {Object}
|
||||
* @param {any} module JS模块
|
||||
* @param {any} file JS文件
|
||||
* @param {any} optional 附加选项
|
||||
* @returns {void}
|
||||
*/
|
||||
function compileJs(module, file, optional) {
|
||||
// @ts-ignore
|
||||
var origin = base.read(file);
|
||||
if (optional.hook) {
|
||||
origin = optional.hook(origin);
|
||||
}
|
||||
// 2019-09-19 使用 扩展函数直接 load 无需保存/删除文件
|
||||
// @ts-ignore
|
||||
var compiledWrapper = engineLoad({ script: '(function $(module, exports, require, __dirname, __filename) {' + origin + '});', name: file });
|
||||
compiledWrapper.apply(module.exports, [
|
||||
module, module.exports, module.require, file.parentFile, file
|
||||
@ -215,18 +242,19 @@
|
||||
|
||||
/**
|
||||
* 预编译Json
|
||||
* @param module Json模块
|
||||
* @param file Json 文件
|
||||
* @returns {Object}
|
||||
* @param {{ id?: string | null; exports?: {}; loaded: any; require?: any; }} module Json模块
|
||||
* @param {any} file Json 文件
|
||||
* @returns {void}
|
||||
*/
|
||||
function compileJson(module, file) {
|
||||
// @ts-ignore
|
||||
module.exports = JSON.parse(base.read(file));
|
||||
module.loaded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试从网络下载依赖包
|
||||
* @param name 包名称
|
||||
* @param {string} name 包名称
|
||||
*/
|
||||
function download(name) {
|
||||
// handle name es6-map/implement => es6-map @ms/common/dist/reflect => @ms/common
|
||||
@ -235,10 +263,11 @@
|
||||
// at windows need replace file name java.lang.IllegalArgumentException: Invalid prefix or suffix
|
||||
var tempFile = Files.createTempFile(module_name.replace('/', '_'), '.json');
|
||||
Files.copy(new URL('https://repo.yumc.pw/repository/npm/' + module_name).openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);
|
||||
var info = JSON.parse(new java.lang.String(Files.readAllBytes(tempFile), 'UTF-8')); tempFile.toFile().deleteOnExit();
|
||||
var info = JSON.parse(new JavaString(Files.readAllBytes(tempFile), 'UTF-8')); tempFile.toFile().deleteOnExit();
|
||||
var url = info.versions[info['dist-tags']['latest']].dist.tarball;
|
||||
console.log('node_module ' + module_name + ' not found at local but exist at internet ' + url + ' downloading...')
|
||||
var tis = new TarInputStream(new BufferedInputStream(new GZIPInputStream(new URL(url).openStream())));
|
||||
// @ts-ignore
|
||||
var entry; var target = root + separatorChar + 'node_modules' + separatorChar + module_name;
|
||||
while ((entry = tis.getNextEntry()) != null) {
|
||||
var targetPath = Paths.get(target + separatorChar + entry.getName().substring('package/'.length));
|
||||
@ -248,6 +277,10 @@
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查核心模块
|
||||
* @param {string} name
|
||||
*/
|
||||
function checkCoreModule(name) {
|
||||
if (CoreModules.indexOf(name) != -1) {
|
||||
throw new Error("Can't load nodejs core module " + name + " . maybe later will auto replace to @ms/" + name + ' to compatible...')
|
||||
@ -256,9 +289,9 @@
|
||||
|
||||
/**
|
||||
* 加载模块
|
||||
* @param name 模块名称
|
||||
* @param path 路径
|
||||
* @param optional 附加选项
|
||||
* @param {string} name 模块名称
|
||||
* @param {string} path 路径
|
||||
* @param {any} optional 附加选项
|
||||
* @returns {*}
|
||||
*/
|
||||
function _require(name, path, optional) {
|
||||
@ -271,7 +304,7 @@
|
||||
// excloud local dir, prevent too many recursive call and cache not found module
|
||||
if (name.startsWith('.') || name.startsWith('/') || optional.recursive || notFoundModules[name]) {
|
||||
console.log(name, path, optional, notFoundModules[name])
|
||||
throw new Error("Can't found module " + name + ' at local or network!')
|
||||
throw new Error("Can't found module " + name + '(' + JSON.stringify(optional) + ') at local ' + path + ' or network!')
|
||||
}
|
||||
optional.recursive = true;
|
||||
return _require(download(name), path, optional);
|
||||
@ -286,29 +319,65 @@
|
||||
|
||||
/**
|
||||
* 闭包方法
|
||||
* @param parent 父目录
|
||||
* @param {string} parent 父目录
|
||||
* @param {string} parentId
|
||||
* @returns {Function}
|
||||
*/
|
||||
function exports(parent) {
|
||||
return function __DynamicRequire__(path, optional) {
|
||||
return _require(path, parent, optional).exports;
|
||||
};
|
||||
function exports(parent, parentId) {
|
||||
var __DynamicRequire__ =
|
||||
/**
|
||||
* @param {string} path
|
||||
* @param {any} optional
|
||||
*/
|
||||
function __DynamicRequire__(path, optional) {
|
||||
return _require(path, parent, __assign({ parentId: parentId }, optional)).exports;
|
||||
}
|
||||
return __DynamicRequire__
|
||||
}
|
||||
|
||||
function getRequire(parent) {
|
||||
var require = exports(parent)
|
||||
require.resolve = function __DynamicResolve__(name) {
|
||||
/**
|
||||
* @param {string} parent
|
||||
* @param {string} parentId
|
||||
*/
|
||||
function getRequire(parent, parentId) {
|
||||
/**
|
||||
* @type {any} require
|
||||
*/
|
||||
var require = exports(parent, parentId)
|
||||
require.resolve =
|
||||
/**
|
||||
* @param {string} name
|
||||
*/
|
||||
function __DynamicResolve__(name) {
|
||||
return _canonical(new File(resolve(name, parent)))
|
||||
}
|
||||
require.clear =
|
||||
/**
|
||||
* @param {string} name
|
||||
*/
|
||||
function __DynamicClear__(name) {
|
||||
var moduleId = require.resolve(name)
|
||||
console.trace('Clear module ' + name + '(' + moduleId + ') ...')
|
||||
return delete cacheModules[moduleId]
|
||||
}
|
||||
return require;
|
||||
}
|
||||
|
||||
if (typeof parent === 'string') {
|
||||
parent = new File(parent);
|
||||
}
|
||||
var cacheModules = [];
|
||||
var cacheModuleIds = [];
|
||||
var notFoundModules = [];
|
||||
console.info('Initialization require module... ParentDir:', _canonical(parent));
|
||||
return getRequire(parent);
|
||||
});
|
||||
/**
|
||||
* @type {{[key:string]:any}} cacheModules
|
||||
*/
|
||||
var cacheModules = {};
|
||||
/**
|
||||
* @type {{[key:string]:string}} cacheModules
|
||||
*/
|
||||
var cacheModuleIds = {};
|
||||
/**
|
||||
* @type {{[key:string]:boolean}} cacheModules
|
||||
*/
|
||||
var notFoundModules = {};
|
||||
console.info('Initialization require module. ParentDir:', _canonical(parent));
|
||||
return getRequire(parent, "null");
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user