feat: optimize require module & update asm version
This commit is contained in:
parent
074e6e8a10
commit
6b34566489
88
README.md
88
README.md
@ -1,87 +1,11 @@
|
||||
# MiaoScript
|
||||
|
||||
> 排版什么的 不存在的 这辈子都不会有排版的 除非什么时候论坛支持 `MarkDown` 了
|
||||
> 一个兼容 Spigot Sponge Nukkit BungeeCord 的 脚本插件运行时
|
||||
|
||||
### 简介
|
||||
## 简介
|
||||
|
||||
> 这个坑是我自己刨的 但是发现坑太大 需要更多的人一起填
|
||||
## 安装
|
||||
|
||||
#### 起源
|
||||
|
||||
- 诞生于 `2016年08月25日` 这是 Git 上的第一个提交 具体啥时候我也忘了
|
||||
- 起初 `MiaoScript` 只是用于服务器其他插件的变量执行 并且依赖于PAPI(不知道是啥的自己百度)
|
||||
- 比如 [`MiaoMenu`](http://w.yumc.pw/zc/MiaoMenu.html) 的部分复杂脚本
|
||||
- 比如 [`MiaoChat`](http://mcbbs.net/thread-631240-1-1.html) 的聊天变量
|
||||
- 突然有一天 圈内的大佬 `QSB` @qiu1995 过来找我 说能不能用脚本监听玩家的事件
|
||||
- PS: 这货自从用过 `DeluxeMenu` 之后就喜欢上了用JS写菜单
|
||||
- 当初感觉没啥问题 就出了第一个简易的 `MiaoScript` 版本 还是用 yml 做的配置文件
|
||||
- 但是由于设计 BukkitAPI 等内容 对Java要求太高 后来 邱也弃坑了 我也弃坑了
|
||||
|
||||
#### 刨坑
|
||||
|
||||
- 时隔多年(也就一年) 看到了Sponge的兴起 (估摸着是MCPC系列的MOD端都弃坑了)
|
||||
- 同时 这期间 收到很多腐竹的单子 但是又是非常基础的东西
|
||||
- 比如 开服给玩家发一条消息啦
|
||||
- 比如 修改玩家某些数据啦
|
||||
- 这些东西实际上也就几行代码的事情
|
||||
- 同时 很多想入坑 插件开发 但是又有一些被卡死在环境搭建上
|
||||
- 比如 `Bukkit` 需要 `BukkitAPI`
|
||||
- `Sponge` 需要 `SpongeAPI` 如果涉及 `MOD` 还要 `Forge` 环境
|
||||
- 再或者 BungeeCord 的插件开发 我也是经常懒得搞
|
||||
- 当然 最主要的是 某个 咕咕咕的群 天天有人问我 喵系插件能不能支持 Sponge
|
||||
- 内心当然是拒绝的 现在要上班养老婆孩子(咳咳 不要以为我是大叔 我也才刚毕业而已) 那里还有时间免费给你们写插件
|
||||
- 于是乎 我又想起了当初的 `MiaoScript`
|
||||
- 突发奇想 一个插件的雏形出现在我的脑海中
|
||||
- 可以兼容多种服务器
|
||||
- 不需要开发环境 有记事本就可以开发
|
||||
- 语法要简单 比如 JavaScript
|
||||
- 能够自动搜索安装依赖(毕竟很多人天天问我为何喵系插件跑不起来 都是缺少PAPI)
|
||||
- 能够不重启更新插件(当然得保证代码安全的前提下)
|
||||
- 在 2017年9月14号(距离 第一个版本正式版发布(2016-09-21) 相差一年整)
|
||||
- 一个全新的 `MiaoScript` 诞生了
|
||||
- Java部分代码 只有一个启动类
|
||||
- 核心全部由 JS 编写
|
||||
- 兼容 `CommonJS` 规范
|
||||
- 实时重载
|
||||
- 不兼容 MOD 服 (咳咳 当然现在已经支持了)
|
||||
- 基础结构如下
|
||||
|
||||
```txt
|
||||
└─src
|
||||
└─main
|
||||
├─java 引导类
|
||||
└─resources
|
||||
├─bios.js 核心启动类 用于释放文件和初始化
|
||||
├─api 全平台兼容的接口
|
||||
├─core 核心代码 例如 require 模块
|
||||
│ └─ext 扩展代码 例如 Object.toJson()
|
||||
├─internal 内部实现 用于各个平台实现API
|
||||
│ ├─bukkit BukkitAPI内部实现
|
||||
│ └─sponge SpongeAPI内部实现
|
||||
├─modules JS模块 例如 js-yaml, http 等
|
||||
└─plugins 这里当然是插件啦
|
||||
├─bukkit 只兼容bukkit的插件
|
||||
├─sponge 只兼容Sponge的插件
|
||||
└─ext 插件扩展类库 用于多个插件共用代码 当然最好是是用 `modules` 啦
|
||||
```
|
||||
- 没错 第一个版本只兼容了 BukkitAPI
|
||||
- 我还用 `MiaoScript` 给某位腐竹写了一个抽奖插件
|
||||
- 当时因为没解决 MOD 服兼容问题 所以就退款了 放上[源码](http://paste.yumc.pw/pknd8q6e1)
|
||||
- 由于当时没有封装相关的API所以很多方法是直接调用了 `Bukkit` 原生的代码
|
||||
- 所以不兼容 `Sponge`
|
||||
|
||||
### 进展
|
||||
|
||||
- [项目发布](https://git.yumc.pw/502647092/MiaoScript/releases)
|
||||
- [项目代码](https://git.yumc.pw/502647092/MiaoScript)
|
||||
- [项目脑图](http://naotu.baidu.com/file/293b9a0fc7cef23c69de81c55e3617d5?token=1eee8fd759198eb7)
|
||||
|
||||
### 规划
|
||||
|
||||
- 初期只会支持JS类型的插件开发
|
||||
- 二期会出一个建议版本的MS脚本 可以用简单的语法实现简单的功能
|
||||
- 各个层级会有依赖控制 比如 `MS脚本 => JS脚本 => 调用Java原生API`
|
||||
|
||||
### 填坑
|
||||
|
||||
- 实际上说了那么多 最终希望的就是 有大佬能一起来填坑 毕竟这个坑太大了
|
||||
- 下载后放入对应服务器目录
|
||||
- Bukkit Nukkit BungeeCord 以及其分支 => `plugins`
|
||||
- Sponge => `mods`
|
||||
|
2
pom.xml
2
pom.xml
@ -27,10 +27,10 @@
|
||||
<properties>
|
||||
<env.GIT_COMMIT>DEV</env.GIT_COMMIT>
|
||||
<update.changes>
|
||||
§623-10-25 §afeat: 升级 asm 版本;
|
||||
§623-07-30 §afeat: 优化 require 性能;
|
||||
§cfix: 修复 require 加载特殊文件异常;
|
||||
§623-07-22 §afeat: 网络加载 jtar 优化包大小;
|
||||
§afeat: 移除 Spring 相关支持;
|
||||
§aremove: 移除 Spring 相关支持;
|
||||
§adeps: 更新 Nashorn 和 GraalvmJS 引擎版本
|
||||
</update.changes>
|
||||
|
@ -1,5 +1,6 @@
|
||||
package pw.yumc.MiaoScript.api;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.SneakyThrows;
|
||||
import pw.yumc.MiaoScript.engine.MiaoScriptEngine;
|
||||
|
||||
@ -17,6 +18,7 @@ public class ScriptEngine {
|
||||
private final Object logger;
|
||||
private final String root;
|
||||
private final Base base;
|
||||
@Getter
|
||||
private MiaoScriptEngine engine;
|
||||
private Object future;
|
||||
|
||||
@ -71,8 +73,4 @@ public class ScriptEngine {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MiaoScriptEngine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,6 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class MavenDependLoader {
|
||||
public static final String MavenRepo = "https://maven.aliyun.com/repository/public";
|
||||
|
@ -2,7 +2,6 @@ package pw.yumc.MiaoScript.engine;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.val;
|
||||
import pw.yumc.MiaoScript.api.loader.JarLoader;
|
||||
import pw.yumc.MiaoScript.api.loader.MavenDependLoader;
|
||||
|
||||
@ -34,13 +33,21 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
|
||||
System.setProperty("nashorn.debug", "true");
|
||||
}
|
||||
MavenDependLoader.load(this.libsRoot, "org.kamranzafar", "jtar", "2.3");
|
||||
this.loadScriptEngine(engineRoot);
|
||||
if (this.engine == null)
|
||||
throw new UnsupportedOperationException("当前环境不支持 Nashorn 或 GraalJS 脚本引擎.");
|
||||
}
|
||||
|
||||
private void loadScriptEngine(String engineRoot) {
|
||||
if (new File(engineRoot, "graal").exists()) {
|
||||
this.engine = this.loadNetworkGraalJS();
|
||||
return;
|
||||
}
|
||||
if (getJavaVersion() > 15) {
|
||||
this.loadGraalJS();
|
||||
} else {
|
||||
this.loadNashorn();
|
||||
}
|
||||
if (engine == null)
|
||||
throw new UnsupportedOperationException("当前环境不支持 Nashorn 或 GraalJS 脚本引擎.");
|
||||
}
|
||||
|
||||
private void loadGraalJS() {
|
||||
@ -61,7 +68,7 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
try {
|
||||
val extDirs = System.getProperty("java.ext.dirs");
|
||||
String extDirs = System.getProperty("java.ext.dirs");
|
||||
if (this.engine == null && extDirs != null) {
|
||||
this.engine = this.loadLocalNashorn(extDirs);
|
||||
}
|
||||
@ -93,7 +100,7 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
|
||||
}
|
||||
|
||||
private ScriptEngine loadLocalNashorn(String extDirs) {
|
||||
val dirs = extDirs.split(File.pathSeparator);
|
||||
String[] dirs = extDirs.split(File.pathSeparator);
|
||||
for (String dir : dirs) {
|
||||
File nashorn = new File(dir, "nashorn.jar");
|
||||
if (nashorn.exists()) {
|
||||
@ -104,52 +111,50 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String nashornVersion = "15.4";
|
||||
private String asmVersion = "9.6";
|
||||
|
||||
private ScriptEngine loadNetworkNashorn() {
|
||||
MavenDependLoader.load(this.libsRoot, "org.openjdk.nashorn", "nashorn-core", "15.4");
|
||||
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm", "9.5");
|
||||
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-commons", "9.5");
|
||||
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-tree", "9.5");
|
||||
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-util", "9.5");
|
||||
MavenDependLoader.load(this.libsRoot, "org.openjdk.nashorn", "nashorn-core", this.nashornVersion);
|
||||
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm", this.asmVersion);
|
||||
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-commons", this.asmVersion);
|
||||
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-tree", this.asmVersion);
|
||||
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-util", this.asmVersion);
|
||||
return createEngineByFactoryClassName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory", false);
|
||||
}
|
||||
|
||||
private ScriptEngine parentLoadNetworkNashorn() {
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.openjdk.nashorn", "nashorn-core", "15.4");
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm", "9.5");
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-commons", "9.5");
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-tree", "9.5");
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-util", "9.5");
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.openjdk.nashorn", "nashorn-core", this.nashornVersion);
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm", this.asmVersion);
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-commons", this.asmVersion);
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-tree", this.asmVersion);
|
||||
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-util", this.asmVersion);
|
||||
return createEngineByFactoryClassName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory", false);
|
||||
}
|
||||
|
||||
private String graalVersion = "23.0.2";
|
||||
private String icu4jVersion = "72.1";
|
||||
|
||||
@SneakyThrows
|
||||
private ScriptEngine loadNetworkGraalJS() {
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.js", "js", "23.0.1");
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.js", "js-scriptengine", "23.0.1");
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.regex", "regex", "23.0.1");
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.sdk", "graal-sdk", "23.0.1");
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.truffle", "truffle-api", "23.0.1");
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.js", "js", this.graalVersion);
|
||||
MavenDependLoader.load(this.libsRoot, "com.ibm.icu", "icu4j", this.icu4jVersion);
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.js", "js-scriptengine", this.graalVersion);
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.regex", "regex", this.graalVersion);
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.sdk", "graal-sdk", this.graalVersion);
|
||||
MavenDependLoader.load(this.libsRoot, "org.graalvm.truffle", "truffle-api", this.graalVersion);
|
||||
System.setProperty("polyglot.engine.AllowExperimentalOptions", "true");
|
||||
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");
|
||||
System.setProperty("polyglot.js.nashorn-compat", "true");
|
||||
System.setProperty("polyglot.js.scripting", "true");
|
||||
System.setProperty("polyglot.js.ecmascript-version", "5");
|
||||
System.setProperty("polyglot.js.allowAllAccess", "true");
|
||||
Class<?> NashornScriptEngineFactory = Class.forName("com.oracle.truffle.js.scriptengine.GraalJSEngineFactory");
|
||||
Method getScriptEngine = NashornScriptEngineFactory.getMethod("getScriptEngine");
|
||||
Object factory = NashornScriptEngineFactory.newInstance();
|
||||
return (ScriptEngine) getScriptEngine.invoke(factory);
|
||||
// Class<?> GraalJSScriptEngine = Class.forName("com.oracle.truffle.js.scriptengine.GraalJSScriptEngine");
|
||||
// Method createScriptEngine = GraalJSScriptEngine.getMethod("create", Class.forName("org.graalvm.polyglot.Engine"), Class.forName("org.graalvm.polyglot.Context"));
|
||||
// Class<?> Context = Class.forName("org.graalvm.polyglot.Context");
|
||||
// Method newBuilder = Context.getMethod("newBuilder", String[].class);
|
||||
// Class<?> Builder = Class.forName("org.graalvm.polyglot.Context.Builder");
|
||||
// Method allowExperimentalOptions = Builder.getMethod("allowExperimentalOptions", boolean.class);
|
||||
// Method allowAllAccess = Builder.getMethod("allowAllAccess", boolean.class);
|
||||
// Method option = Builder.getMethod("option", String.class, String.class);
|
||||
// Object context = newBuilder.invoke(null, (Object) new String[]{"js"});
|
||||
// allowExperimentalOptions.invoke(context, true);
|
||||
// allowAllAccess.invoke(context, true);
|
||||
// option.invoke(context, "js.nashorn-compat", "true");
|
||||
// return (ScriptEngine) createScriptEngine.invoke(null, null, context);
|
||||
ScriptEngine engine = (ScriptEngine) getScriptEngine.invoke(factory);
|
||||
Bindings bind = engine.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||
bind.put("polyglot.js.allowAllAccess", true);
|
||||
return engine;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@ -168,7 +173,7 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
|
||||
if (getJavaVersion() >= 11 && jdk) {
|
||||
engineArgs.add("--no-deprecation-warning");
|
||||
}
|
||||
return (ScriptEngine) getScriptEngine.invoke(factory, (Object) engineArgs.toArray(new String[]{}));
|
||||
return (ScriptEngine) getScriptEngine.invoke(factory, (Object) engineArgs.toArray(new String[] {}));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -17,9 +17,7 @@ var global = this;
|
||||
// Development Env Detect
|
||||
global.root = root || "src/main/resources"
|
||||
readEnvironment()
|
||||
if (!global.debug) {
|
||||
checkUpgrade()
|
||||
}
|
||||
if (!global.debug) { checkUpgrade() }
|
||||
return bootEngineThread(checkClassLoader())
|
||||
}
|
||||
|
||||
@ -27,8 +25,9 @@ var global = this;
|
||||
logger.info("ScriptEngine: " + ScriptEngineContextHolder.getEngine().getEngine().class.name)
|
||||
var future = new FutureTask(function () {
|
||||
Thread.currentThread().contextClassLoader = loader
|
||||
var faster = load(System.getenv("MS_NODE_CORE_POLYFILL") || 'classpath:core/polyfill.js')(root, logger)
|
||||
return faster.default ? require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core')).default : false
|
||||
load(System.getenv("MS_NODE_CORE_POLYFILL") || 'classpath:core/polyfill.js')(root, logger)
|
||||
var core = require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core'))
|
||||
return core.default || core
|
||||
})
|
||||
// Async Loading MiaoScript Engine
|
||||
new Thread(future, "MiaoScript thread").start()
|
||||
@ -36,21 +35,18 @@ var global = this;
|
||||
}
|
||||
|
||||
global.enable = function (future) {
|
||||
if (!future.isDone()) {
|
||||
logger.info("Waiting MiaoScript booted...")
|
||||
}
|
||||
// await polyfill loading
|
||||
if (!future.isDone()) { logger.info("MiaoScript booting...") }
|
||||
// faster load core
|
||||
var core = future.get()
|
||||
logger.info("MiaoScript booted starting...")
|
||||
var disable = core ? core.enable() : require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core')).default
|
||||
global.engineDisableImpl = disable || function () {
|
||||
logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...')
|
||||
}
|
||||
logger.info("MiaoScript starting...")
|
||||
global.engineDisableImpl = core.enable()
|
||||
}
|
||||
|
||||
global.disable = function () {
|
||||
global.engineDisableImpl && global.engineDisableImpl()
|
||||
(global.engineDisableImpl || function () {
|
||||
logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...')
|
||||
})()
|
||||
}
|
||||
|
||||
function readEnvironment() {
|
||||
@ -91,11 +87,10 @@ var global = this;
|
||||
var classLoader = Thread.currentThread().contextClassLoader
|
||||
if (classLoader.getResource("bios.js") === null) {
|
||||
throw Error("Error class loader: " + classLoader.class.name + " Please contact the author MiaoWoo!")
|
||||
} else {
|
||||
logger.info("Class loader compatible: " + classLoader.class.name)
|
||||
if (classLoader.parent) {
|
||||
logger.info("Parent class loader: " + classLoader.parent.class.name)
|
||||
}
|
||||
}
|
||||
logger.info("Class loader compatible: " + classLoader.class.name)
|
||||
if (classLoader.parent) {
|
||||
logger.info("Parent class loader: " + classLoader.parent.class.name)
|
||||
}
|
||||
return classLoader
|
||||
}
|
||||
|
@ -70,7 +70,7 @@
|
||||
"v8", "vm", "wasi", "worker_threads", "zlib"
|
||||
]
|
||||
|
||||
var ModulesVersionLock = {}
|
||||
var VersionLockModules = {}
|
||||
|
||||
/**
|
||||
* @param {...object} t
|
||||
@ -106,15 +106,6 @@
|
||||
return file.canonicalPath
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得文件绝对路径
|
||||
* @param {any} file
|
||||
* @returns {*}
|
||||
*/
|
||||
function _absolute(file) {
|
||||
return file.absolutePath
|
||||
}
|
||||
|
||||
function __error(message, name) {
|
||||
var error = new Error(message)
|
||||
if (name) { error.name = name }
|
||||
@ -139,11 +130,14 @@
|
||||
if (optional.local) {
|
||||
return resolveAsFile(name, parent) || resolveAsDirectory(name, parent) || undefined
|
||||
} else {
|
||||
// 解析 root 模块目录
|
||||
var rootModule = resolveAsFile(name, MS_NODE_PATH) || resolveAsDirectory(name, MS_NODE_PATH)
|
||||
if (rootModule) { return rootModule }
|
||||
// 解析Node目录
|
||||
var dir = [parent, 'node_modules'].join(separatorChar)
|
||||
return resolveAsFile(name, dir) || resolveAsDirectory(name, dir) ||
|
||||
(parent && parent.toString().startsWith(root) ?
|
||||
resolve(name, new File(parent).getParent(), optional) : resolveAsDirectory(name, MS_NODE_PATH) || undefined)
|
||||
resolve(name, new File(parent).getParent(), optional) : undefined)
|
||||
}
|
||||
}
|
||||
|
||||
@ -161,12 +155,12 @@
|
||||
return file
|
||||
}
|
||||
// JS文件
|
||||
var js = new File(normalizeName(_absolute(file), '.js'))
|
||||
var js = new File(normalizeName(_canonical(file), '.js'))
|
||||
if (js.isFile()) {
|
||||
return js
|
||||
}
|
||||
// JSON文件
|
||||
var json = new File(normalizeName(_absolute(file), '.json'))
|
||||
var json = new File(normalizeName(_canonical(file), '.json'))
|
||||
if (json.isFile()) {
|
||||
return json
|
||||
}
|
||||
@ -235,7 +229,7 @@
|
||||
var filename = file.name
|
||||
var lastDotIndexOf = filename.lastIndexOf('.')
|
||||
if (lastDotIndexOf == -1) {
|
||||
throw __error('require ' + file + ' error: module must include file ext.')
|
||||
throw __error("can't require file " + file + '. error: module must include file ext.')
|
||||
}
|
||||
var name = filename.substring(0, lastDotIndexOf)
|
||||
var ext = filename.substring(lastDotIndexOf + 1)
|
||||
@ -254,12 +248,12 @@
|
||||
exports: {},
|
||||
loaded: false,
|
||||
loader: loader,
|
||||
path: _absolute(file.parentFile),
|
||||
filename: _absolute(file),
|
||||
path: _canonical(file.parentFile),
|
||||
filename: _canonical(file),
|
||||
children: []
|
||||
}
|
||||
module.require = getRequire(module)
|
||||
if (module.parent && module.parent.children) {
|
||||
if (module.parent && module.parent.children && module.parent.children.indexOf(module) == -1) {
|
||||
module.parent.children.push(module)
|
||||
}
|
||||
console.trace('Loading module', name + '(' + id + ')', 'Optional', JSON.stringify(__assign(optional, { parent: undefined })))
|
||||
@ -286,9 +280,6 @@
|
||||
* @returns {any}
|
||||
*/
|
||||
function compileJs(module, script, optional) {
|
||||
if (optional.hook) {
|
||||
script = optional.hook(script)
|
||||
}
|
||||
if (optional.beforeCompile) {
|
||||
script = optional.beforeCompile(script)
|
||||
}
|
||||
@ -377,12 +368,11 @@
|
||||
var module_version = name_arr[1]
|
||||
try {
|
||||
var target = MS_NODE_PATH + separatorChar + module_name
|
||||
var _package = new File(target, 'package.json')
|
||||
if (_package.exists()) { return name }
|
||||
if (new File(target, 'package.json').exists()) { return name }
|
||||
var info = fetchPackageInfo(module_name)
|
||||
if (!module_version) {
|
||||
// if not special version get from lock or tag
|
||||
module_version = ModulesVersionLock[module_name]
|
||||
module_version = VersionLockModules[module_name]
|
||||
} else if (!/\d+\.\d+\.\w+/.test(module_version)) {
|
||||
// maybe module_version = latest if special version not exist then fallback latest
|
||||
console.log('try get node_module ' + module_name + ' version from ' + module_version + ' tag waiting...')
|
||||
@ -555,7 +545,7 @@
|
||||
* @param {string} path
|
||||
* @param {any} optional
|
||||
*/
|
||||
return function __DynamicRequire__(path, optional) {
|
||||
var require = function __DynamicRequire__(path, optional) {
|
||||
if (!path) {
|
||||
throw __error("require path can't be undefined or empty!")
|
||||
}
|
||||
@ -567,17 +557,14 @@
|
||||
}, optional)
|
||||
return _require(path, parent.path, optional).exports
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} path
|
||||
* @param {any} optional 附加选项
|
||||
*/
|
||||
function __DynamicResolve__(path, optional) {
|
||||
return _canonical(new File(resolve(path, root, __assign({
|
||||
cache: true,
|
||||
local: path.startsWith('.') || path.startsWith('/')
|
||||
}, optional))))
|
||||
require.resolve = function __DynamicResolve__(path, optional) {
|
||||
return _canonical(new File(resolve(path, root, __assign({
|
||||
parent: parent,
|
||||
cache: true,
|
||||
local: path.startsWith('.') || path.startsWith('/')
|
||||
}, optional))))
|
||||
}
|
||||
return require
|
||||
}
|
||||
|
||||
/**
|
||||
@ -617,8 +604,8 @@
|
||||
* @type {any} require
|
||||
*/
|
||||
var require = exports(parent)
|
||||
require.main = mainRequire
|
||||
require.cache = cacheModules
|
||||
require.resolve = __DynamicResolve__
|
||||
require.clear = __DynamicClear__
|
||||
require.disable = __DynamicDisable__
|
||||
require.setUpgradeMode = __setUpgradeMode__
|
||||
@ -627,12 +614,13 @@
|
||||
get: getLoader,
|
||||
unregister: unregisterLoader,
|
||||
}
|
||||
require.loaders = requireLoaders
|
||||
require.internal = {
|
||||
coreModules: CoreModules,
|
||||
cacheModules: cacheModules,
|
||||
cacheModuleIds: cacheModuleIds,
|
||||
notFoundModules: notFoundModules,
|
||||
requireLoaders: requireLoaders
|
||||
versionLockModules: VersionLockModules
|
||||
}
|
||||
require.loadCoreScript = loadCoreScript
|
||||
return require
|
||||
@ -693,24 +681,24 @@
|
||||
function initVersionLock() {
|
||||
try {
|
||||
var version_lock_url = MS_SCRIPT_PACKAGE_CENTER + '?name=version_lock' + (global.debug ? '-debug' : '')
|
||||
ModulesVersionLock = JSON.parse(fetchContent(version_lock_url, 5000))
|
||||
VersionLockModules = JSON.parse(fetchContent(version_lock_url, 5000))
|
||||
try {
|
||||
ModulesVersionLock = __assign(ModulesVersionLock, JSON.parse(base.read(localVersionLockFile)))
|
||||
VersionLockModules = __assign(VersionLockModules, JSON.parse(base.read(localVersionLockFile)))
|
||||
} catch (e) {
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("无法获取到最新的版本锁定信息 使用默认配置.")
|
||||
console.warn("InitVersionLock Error:", error)
|
||||
console.debug(error)
|
||||
ModulesVersionLock = {
|
||||
VersionLockModules = {
|
||||
"@babel/standalone": "7.12.18",
|
||||
"crypto-js": "3.3.0",
|
||||
"core-js": "3.19.3"
|
||||
"core-js": "3.33.1"
|
||||
}
|
||||
}
|
||||
console.info('Lock module version List:')
|
||||
for (var key in ModulesVersionLock) {
|
||||
console.info('- ' + key + ': ' + ModulesVersionLock[key])
|
||||
for (var key in VersionLockModules) {
|
||||
console.info('- ' + key + ': ' + VersionLockModules[key])
|
||||
}
|
||||
}
|
||||
|
||||
@ -725,6 +713,7 @@
|
||||
console.debug(error)
|
||||
registerLoader('ms', compileJsFile)
|
||||
}
|
||||
require.main = mainRequire = require
|
||||
return require
|
||||
}
|
||||
|
||||
@ -738,6 +727,7 @@
|
||||
if (typeof parent === 'string') {
|
||||
parent = new File(parent)
|
||||
}
|
||||
var mainRequire = undefined
|
||||
/**
|
||||
* require 支持的后缀
|
||||
* @type {string[]} requireExts
|
||||
|
Loading…
Reference in New Issue
Block a user