查看: 3066|回复: 4

[原创发布] [技术验证]: 插件的ES模块化加载 (附代码)

[复制链接]
梦石
0
星屑
1907
在线时间
1555 小时
注册时间
2013-4-13
回帖
783
发表于 2020-12-12 16:40:32 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
使用ES模块来写脚本

原理:

给PluginManager添加loadModule方法用来加载模块
在scripts文件夹中放入插件的入口插件(本例中: TestModule.js)
在入口插件中加载入口模块
入口插件可以和其他插件一样使用插件参数和插件命令
在js文件夹中新建modules文件夹, 将模块(本例中TestModule)文件夹
我们约定模块文件夹下用index.js作为统一的入口

使用 import {} from './def.js' 来导入相对路径模块
使用 import {} from '/js/modules/abc/def.js 来导入绝对路径模块(以工程目录为根)

vscode中增加支持路径跳转的设置, 在根目录添加jsconfig.json
在其中增加:
{"compilerOptions": {"paths": {"/*": ["*"]}}}

Example 见附件

[pre lang="JavaScript"]//=============================================================================
// ModulePlugin
//=============================================================================

/*:
* @target MZ
* @plugindesc Allow Load Module Plugins
* @author Heartcase
*
* @help ModulePlugin.js
*
* Put the Module Folder under js/modules and the entry file should be index.js
* To invoke module, add the module entry plugin in plugins folder and enable it
* then call PluginManager.loadModule inside your entry plugin
*/

PluginManager.loadModule = (filename) => {
  const url = `js/modules/${filename}/index.js`;
  const script = document.createElement('script');
  script.type = 'module';
  script.src = url;
  document.body.appendChild(script);
};
[/pre]

js.zip

2.32 KB, 下载次数: 48

夏普的道具店

塞露提亚-道具屋的经营妙方同人作品
发布帖:点击这里
梦石
0
星屑
4082
在线时间
302 小时
注册时间
2019-4-23
回帖
185
发表于 2020-12-14 09:58:22 | 显示全部楼层
这样不好配置参数吧

点评

6666  发表于 2020-12-15 09:56
每个模块插件会有一个入口插件放在plugins里面, 主楼的插件只是提供模块插件的底层支持, 参数和命令一样可以用.  发表于 2020-12-14 20:34
回复

使用道具 举报

梦石
0
星屑
25
在线时间
4 小时
注册时间
2022-2-22
回帖
30
发表于 2022-2-27 21:33:30 | 显示全部楼层
~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部