feat: optimize require module & update asm version

This commit is contained in:
MiaoWoo 2024-01-08 15:43:21 +08:00
parent 074e6e8a10
commit 6b34566489
7 changed files with 97 additions and 187 deletions

View File

@ -1,87 +1,11 @@
# MiaoScript # MiaoScript
> 排版什么的 不存在的 这辈子都不会有排版的 除非什么时候论坛支持 `MarkDown` > 一个兼容 Spigot Sponge Nukkit BungeeCord 的 脚本插件运行时
### 简介 ## 简介
> 这个坑是我自己刨的 但是发现坑太大 需要更多的人一起填 ## 安装
#### 起源 - 下载后放入对应服务器目录
- Bukkit Nukkit BungeeCord 以及其分支 => `plugins`
- 诞生于 `2016年08月25日` 这是 Git 上的第一个提交 具体啥时候我也忘了 - Sponge => `mods`
- 起初 `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`
### 填坑
- 实际上说了那么多 最终希望的就是 有大佬能一起来填坑 毕竟这个坑太大了

View File

@ -27,10 +27,10 @@
<properties> <properties>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT> <env.GIT_COMMIT>DEV</env.GIT_COMMIT>
<update.changes> <update.changes>
§623-10-25 §afeat: 升级 asm 版本;
§623-07-30 §afeat: 优化 require 性能; §623-07-30 §afeat: 优化 require 性能;
      §cfix: 修复 require 加载特殊文件异常;       §cfix: 修复 require 加载特殊文件异常;
§623-07-22 §afeat: 网络加载 jtar 优化包大小; §623-07-22 §afeat: 网络加载 jtar 优化包大小;
      §afeat: 移除 Spring 相关支持;
      §aremove: 移除 Spring 相关支持;       §aremove: 移除 Spring 相关支持;
      §adeps: 更新 Nashorn 和 GraalvmJS 引擎版本       §adeps: 更新 Nashorn 和 GraalvmJS 引擎版本
</update.changes> </update.changes>

View File

@ -1,5 +1,6 @@
package pw.yumc.MiaoScript.api; package pw.yumc.MiaoScript.api;
import lombok.Getter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import pw.yumc.MiaoScript.engine.MiaoScriptEngine; import pw.yumc.MiaoScript.engine.MiaoScriptEngine;
@ -17,6 +18,7 @@ public class ScriptEngine {
private final Object logger; private final Object logger;
private final String root; private final String root;
private final Base base; private final Base base;
@Getter
private MiaoScriptEngine engine; private MiaoScriptEngine engine;
private Object future; private Object future;
@ -71,8 +73,4 @@ public class ScriptEngine {
} }
} }
} }
public MiaoScriptEngine getEngine() {
return engine;
}
} }

View File

@ -13,8 +13,6 @@ import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MavenDependLoader { public class MavenDependLoader {
public static final String MavenRepo = "https://maven.aliyun.com/repository/public"; public static final String MavenRepo = "https://maven.aliyun.com/repository/public";

View File

@ -2,7 +2,6 @@ package pw.yumc.MiaoScript.engine;
import lombok.Getter; import lombok.Getter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.val;
import pw.yumc.MiaoScript.api.loader.JarLoader; import pw.yumc.MiaoScript.api.loader.JarLoader;
import pw.yumc.MiaoScript.api.loader.MavenDependLoader; import pw.yumc.MiaoScript.api.loader.MavenDependLoader;
@ -34,13 +33,21 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
System.setProperty("nashorn.debug", "true"); System.setProperty("nashorn.debug", "true");
} }
MavenDependLoader.load(this.libsRoot, "org.kamranzafar", "jtar", "2.3"); 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) { if (getJavaVersion() > 15) {
this.loadGraalJS(); this.loadGraalJS();
} else { } else {
this.loadNashorn(); this.loadNashorn();
} }
if (engine == null)
throw new UnsupportedOperationException("当前环境不支持 Nashorn 或 GraalJS 脚本引擎.");
} }
private void loadGraalJS() { private void loadGraalJS() {
@ -61,7 +68,7 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
ex.printStackTrace(); ex.printStackTrace();
} }
try { try {
val extDirs = System.getProperty("java.ext.dirs"); String extDirs = System.getProperty("java.ext.dirs");
if (this.engine == null && extDirs != null) { if (this.engine == null && extDirs != null) {
this.engine = this.loadLocalNashorn(extDirs); this.engine = this.loadLocalNashorn(extDirs);
} }
@ -93,7 +100,7 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
} }
private ScriptEngine loadLocalNashorn(String extDirs) { private ScriptEngine loadLocalNashorn(String extDirs) {
val dirs = extDirs.split(File.pathSeparator); String[] dirs = extDirs.split(File.pathSeparator);
for (String dir : dirs) { for (String dir : dirs) {
File nashorn = new File(dir, "nashorn.jar"); File nashorn = new File(dir, "nashorn.jar");
if (nashorn.exists()) { if (nashorn.exists()) {
@ -104,52 +111,50 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
return null; return null;
} }
private String nashornVersion = "15.4";
private String asmVersion = "9.6";
private ScriptEngine loadNetworkNashorn() { private ScriptEngine loadNetworkNashorn() {
MavenDependLoader.load(this.libsRoot, "org.openjdk.nashorn", "nashorn-core", "15.4"); MavenDependLoader.load(this.libsRoot, "org.openjdk.nashorn", "nashorn-core", this.nashornVersion);
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm", "9.5"); MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm", this.asmVersion);
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-commons", "9.5"); MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-commons", this.asmVersion);
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-tree", "9.5"); MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-tree", this.asmVersion);
MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-util", "9.5"); MavenDependLoader.load(this.libsRoot, "org.ow2.asm", "asm-util", this.asmVersion);
return createEngineByFactoryClassName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory", false); return createEngineByFactoryClassName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory", false);
} }
private ScriptEngine parentLoadNetworkNashorn() { private ScriptEngine parentLoadNetworkNashorn() {
MavenDependLoader.parentLoad(this.libsRoot, "org.openjdk.nashorn", "nashorn-core", "15.4"); MavenDependLoader.parentLoad(this.libsRoot, "org.openjdk.nashorn", "nashorn-core", this.nashornVersion);
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm", "9.5"); MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm", this.asmVersion);
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-commons", "9.5"); MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-commons", this.asmVersion);
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-tree", "9.5"); MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-tree", this.asmVersion);
MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-util", "9.5"); MavenDependLoader.parentLoad(this.libsRoot, "org.ow2.asm", "asm-util", this.asmVersion);
return createEngineByFactoryClassName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory", false); return createEngineByFactoryClassName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory", false);
} }
private String graalVersion = "23.0.2";
private String icu4jVersion = "72.1";
@SneakyThrows @SneakyThrows
private ScriptEngine loadNetworkGraalJS() { private ScriptEngine loadNetworkGraalJS() {
MavenDependLoader.load(this.libsRoot, "org.graalvm.js", "js", "23.0.1"); MavenDependLoader.load(this.libsRoot, "org.graalvm.js", "js", this.graalVersion);
MavenDependLoader.load(this.libsRoot, "org.graalvm.js", "js-scriptengine", "23.0.1"); MavenDependLoader.load(this.libsRoot, "com.ibm.icu", "icu4j", this.icu4jVersion);
MavenDependLoader.load(this.libsRoot, "org.graalvm.regex", "regex", "23.0.1"); MavenDependLoader.load(this.libsRoot, "org.graalvm.js", "js-scriptengine", this.graalVersion);
MavenDependLoader.load(this.libsRoot, "org.graalvm.sdk", "graal-sdk", "23.0.1"); MavenDependLoader.load(this.libsRoot, "org.graalvm.regex", "regex", this.graalVersion);
MavenDependLoader.load(this.libsRoot, "org.graalvm.truffle", "truffle-api", "23.0.1"); 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.nashorn-compat", "true");
System.setProperty("polyglot.js.scripting", "true"); System.setProperty("polyglot.js.scripting", "true");
System.setProperty("polyglot.js.ecmascript-version", "5"); System.setProperty("polyglot.js.ecmascript-version", "5");
System.setProperty("polyglot.js.allowAllAccess", "true");
Class<?> NashornScriptEngineFactory = Class.forName("com.oracle.truffle.js.scriptengine.GraalJSEngineFactory"); Class<?> NashornScriptEngineFactory = Class.forName("com.oracle.truffle.js.scriptengine.GraalJSEngineFactory");
Method getScriptEngine = NashornScriptEngineFactory.getMethod("getScriptEngine"); Method getScriptEngine = NashornScriptEngineFactory.getMethod("getScriptEngine");
Object factory = NashornScriptEngineFactory.newInstance(); Object factory = NashornScriptEngineFactory.newInstance();
return (ScriptEngine) getScriptEngine.invoke(factory); ScriptEngine engine = (ScriptEngine) getScriptEngine.invoke(factory);
// Class<?> GraalJSScriptEngine = Class.forName("com.oracle.truffle.js.scriptengine.GraalJSScriptEngine"); Bindings bind = engine.getBindings(ScriptContext.ENGINE_SCOPE);
// Method createScriptEngine = GraalJSScriptEngine.getMethod("create", Class.forName("org.graalvm.polyglot.Engine"), Class.forName("org.graalvm.polyglot.Context")); bind.put("polyglot.js.allowAllAccess", true);
// Class<?> Context = Class.forName("org.graalvm.polyglot.Context"); return engine;
// 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);
} }
@SneakyThrows @SneakyThrows

View File

@ -17,9 +17,7 @@ var global = this;
// Development Env Detect // Development Env Detect
global.root = root || "src/main/resources" global.root = root || "src/main/resources"
readEnvironment() readEnvironment()
if (!global.debug) { if (!global.debug) { checkUpgrade() }
checkUpgrade()
}
return bootEngineThread(checkClassLoader()) return bootEngineThread(checkClassLoader())
} }
@ -27,8 +25,9 @@ var global = this;
logger.info("ScriptEngine: " + ScriptEngineContextHolder.getEngine().getEngine().class.name) logger.info("ScriptEngine: " + ScriptEngineContextHolder.getEngine().getEngine().class.name)
var future = new FutureTask(function () { var future = new FutureTask(function () {
Thread.currentThread().contextClassLoader = loader Thread.currentThread().contextClassLoader = loader
var faster = load(System.getenv("MS_NODE_CORE_POLYFILL") || 'classpath:core/polyfill.js')(root, logger) 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 var core = require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core'))
return core.default || core
}) })
// Async Loading MiaoScript Engine // Async Loading MiaoScript Engine
new Thread(future, "MiaoScript thread").start() new Thread(future, "MiaoScript thread").start()
@ -36,21 +35,18 @@ var global = this;
} }
global.enable = function (future) { global.enable = function (future) {
if (!future.isDone()) {
logger.info("Waiting MiaoScript booted...")
}
// await polyfill loading // await polyfill loading
if (!future.isDone()) { logger.info("MiaoScript booting...") }
// faster load core // faster load core
var core = future.get() var core = future.get()
logger.info("MiaoScript booted starting...") logger.info("MiaoScript starting...")
var disable = core ? core.enable() : require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core')).default global.engineDisableImpl = core.enable()
global.engineDisableImpl = disable || function () {
logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...')
}
} }
global.disable = function () { global.disable = function () {
global.engineDisableImpl && global.engineDisableImpl() (global.engineDisableImpl || function () {
logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...')
})()
} }
function readEnvironment() { function readEnvironment() {
@ -91,12 +87,11 @@ var global = this;
var classLoader = Thread.currentThread().contextClassLoader var classLoader = Thread.currentThread().contextClassLoader
if (classLoader.getResource("bios.js") === null) { if (classLoader.getResource("bios.js") === null) {
throw Error("Error class loader: " + classLoader.class.name + " Please contact the author MiaoWoo!") throw Error("Error class loader: " + classLoader.class.name + " Please contact the author MiaoWoo!")
} else { }
logger.info("Class loader compatible: " + classLoader.class.name) logger.info("Class loader compatible: " + classLoader.class.name)
if (classLoader.parent) { if (classLoader.parent) {
logger.info("Parent class loader: " + classLoader.parent.class.name) logger.info("Parent class loader: " + classLoader.parent.class.name)
} }
}
return classLoader return classLoader
} }
})() })()

View File

@ -70,7 +70,7 @@
"v8", "vm", "wasi", "worker_threads", "zlib" "v8", "vm", "wasi", "worker_threads", "zlib"
] ]
var ModulesVersionLock = {} var VersionLockModules = {}
/** /**
* @param {...object} t * @param {...object} t
@ -106,15 +106,6 @@
return file.canonicalPath return file.canonicalPath
} }
/**
* 获得文件绝对路径
* @param {any} file
* @returns {*}
*/
function _absolute(file) {
return file.absolutePath
}
function __error(message, name) { function __error(message, name) {
var error = new Error(message) var error = new Error(message)
if (name) { error.name = name } if (name) { error.name = name }
@ -139,11 +130,14 @@
if (optional.local) { if (optional.local) {
return resolveAsFile(name, parent) || resolveAsDirectory(name, parent) || undefined return resolveAsFile(name, parent) || resolveAsDirectory(name, parent) || undefined
} else { } else {
// 解析 root 模块目录
var rootModule = resolveAsFile(name, MS_NODE_PATH) || resolveAsDirectory(name, MS_NODE_PATH)
if (rootModule) { return rootModule }
// 解析Node目录 // 解析Node目录
var dir = [parent, 'node_modules'].join(separatorChar) var dir = [parent, 'node_modules'].join(separatorChar)
return resolveAsFile(name, dir) || resolveAsDirectory(name, dir) || return resolveAsFile(name, dir) || resolveAsDirectory(name, dir) ||
(parent && parent.toString().startsWith(root) ? (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 return file
} }
// JS文件 // JS文件
var js = new File(normalizeName(_absolute(file), '.js')) var js = new File(normalizeName(_canonical(file), '.js'))
if (js.isFile()) { if (js.isFile()) {
return js return js
} }
// JSON文件 // JSON文件
var json = new File(normalizeName(_absolute(file), '.json')) var json = new File(normalizeName(_canonical(file), '.json'))
if (json.isFile()) { if (json.isFile()) {
return json return json
} }
@ -235,7 +229,7 @@
var filename = file.name var filename = file.name
var lastDotIndexOf = filename.lastIndexOf('.') var lastDotIndexOf = filename.lastIndexOf('.')
if (lastDotIndexOf == -1) { 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 name = filename.substring(0, lastDotIndexOf)
var ext = filename.substring(lastDotIndexOf + 1) var ext = filename.substring(lastDotIndexOf + 1)
@ -254,12 +248,12 @@
exports: {}, exports: {},
loaded: false, loaded: false,
loader: loader, loader: loader,
path: _absolute(file.parentFile), path: _canonical(file.parentFile),
filename: _absolute(file), filename: _canonical(file),
children: [] children: []
} }
module.require = getRequire(module) 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) module.parent.children.push(module)
} }
console.trace('Loading module', name + '(' + id + ')', 'Optional', JSON.stringify(__assign(optional, { parent: undefined }))) console.trace('Loading module', name + '(' + id + ')', 'Optional', JSON.stringify(__assign(optional, { parent: undefined })))
@ -286,9 +280,6 @@
* @returns {any} * @returns {any}
*/ */
function compileJs(module, script, optional) { function compileJs(module, script, optional) {
if (optional.hook) {
script = optional.hook(script)
}
if (optional.beforeCompile) { if (optional.beforeCompile) {
script = optional.beforeCompile(script) script = optional.beforeCompile(script)
} }
@ -377,12 +368,11 @@
var module_version = name_arr[1] var module_version = name_arr[1]
try { try {
var target = MS_NODE_PATH + separatorChar + module_name var target = MS_NODE_PATH + separatorChar + module_name
var _package = new File(target, 'package.json') if (new File(target, 'package.json').exists()) { return name }
if (_package.exists()) { return name }
var info = fetchPackageInfo(module_name) var info = fetchPackageInfo(module_name)
if (!module_version) { if (!module_version) {
// if not special version get from lock or tag // 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)) { } else if (!/\d+\.\d+\.\w+/.test(module_version)) {
// maybe module_version = latest if special version not exist then fallback latest // 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...') console.log('try get node_module ' + module_name + ' version from ' + module_version + ' tag waiting...')
@ -555,7 +545,7 @@
* @param {string} path * @param {string} path
* @param {any} optional * @param {any} optional
*/ */
return function __DynamicRequire__(path, optional) { var require = function __DynamicRequire__(path, optional) {
if (!path) { if (!path) {
throw __error("require path can't be undefined or empty!") throw __error("require path can't be undefined or empty!")
} }
@ -567,18 +557,15 @@
}, optional) }, optional)
return _require(path, parent.path, optional).exports return _require(path, parent.path, optional).exports
} }
} require.resolve = function __DynamicResolve__(path, optional) {
/**
* @param {string} path
* @param {any} optional 附加选项
*/
function __DynamicResolve__(path, optional) {
return _canonical(new File(resolve(path, root, __assign({ return _canonical(new File(resolve(path, root, __assign({
parent: parent,
cache: true, cache: true,
local: path.startsWith('.') || path.startsWith('/') local: path.startsWith('.') || path.startsWith('/')
}, optional)))) }, optional))))
} }
return require
}
/** /**
* @param {string} name * @param {string} name
@ -617,8 +604,8 @@
* @type {any} require * @type {any} require
*/ */
var require = exports(parent) var require = exports(parent)
require.main = mainRequire
require.cache = cacheModules require.cache = cacheModules
require.resolve = __DynamicResolve__
require.clear = __DynamicClear__ require.clear = __DynamicClear__
require.disable = __DynamicDisable__ require.disable = __DynamicDisable__
require.setUpgradeMode = __setUpgradeMode__ require.setUpgradeMode = __setUpgradeMode__
@ -627,12 +614,13 @@
get: getLoader, get: getLoader,
unregister: unregisterLoader, unregister: unregisterLoader,
} }
require.loaders = requireLoaders
require.internal = { require.internal = {
coreModules: CoreModules, coreModules: CoreModules,
cacheModules: cacheModules, cacheModules: cacheModules,
cacheModuleIds: cacheModuleIds, cacheModuleIds: cacheModuleIds,
notFoundModules: notFoundModules, notFoundModules: notFoundModules,
requireLoaders: requireLoaders versionLockModules: VersionLockModules
} }
require.loadCoreScript = loadCoreScript require.loadCoreScript = loadCoreScript
return require return require
@ -693,24 +681,24 @@
function initVersionLock() { function initVersionLock() {
try { try {
var version_lock_url = MS_SCRIPT_PACKAGE_CENTER + '?name=version_lock' + (global.debug ? '-debug' : '') 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 { try {
ModulesVersionLock = __assign(ModulesVersionLock, JSON.parse(base.read(localVersionLockFile))) VersionLockModules = __assign(VersionLockModules, JSON.parse(base.read(localVersionLockFile)))
} catch (e) { } catch (e) {
} }
} catch (error) { } catch (error) {
console.warn("无法获取到最新的版本锁定信息 使用默认配置.") console.warn("无法获取到最新的版本锁定信息 使用默认配置.")
console.warn("InitVersionLock Error:", error) console.warn("InitVersionLock Error:", error)
console.debug(error) console.debug(error)
ModulesVersionLock = { VersionLockModules = {
"@babel/standalone": "7.12.18", "@babel/standalone": "7.12.18",
"crypto-js": "3.3.0", "crypto-js": "3.3.0",
"core-js": "3.19.3" "core-js": "3.33.1"
} }
} }
console.info('Lock module version List:') console.info('Lock module version List:')
for (var key in ModulesVersionLock) { for (var key in VersionLockModules) {
console.info('- ' + key + ': ' + ModulesVersionLock[key]) console.info('- ' + key + ': ' + VersionLockModules[key])
} }
} }
@ -725,6 +713,7 @@
console.debug(error) console.debug(error)
registerLoader('ms', compileJsFile) registerLoader('ms', compileJsFile)
} }
require.main = mainRequire = require
return require return require
} }
@ -738,6 +727,7 @@
if (typeof parent === 'string') { if (typeof parent === 'string') {
parent = new File(parent) parent = new File(parent)
} }
var mainRequire = undefined
/** /**
* require 支持的后缀 * require 支持的后缀
* @type {string[]} requireExts * @type {string[]} requireExts