如何加载XARPackage里的导出模块?
XARPackage的模块,有以下几种被加载的环境:
- XARPackage内部
- node环境
- html5(h5)环境
- react-native(rn)环境
- 微信小程序(wx)环境
在这些环境下,都可以使用 bucky.getCurrentRuntime().loadModule('{xarpacakge_name}:{module_name}')
的方式去加载模块,例如:
let runtime = bucky.getCurrentRuntime();
let [err1, thecalc] = await runtime.loadModule('test:test');
其中,在XARPackage内部,可以使用require方式简化代码,参考下面的说明
如何在XARPackage内加载其他XARPackage的导出模块?
此时,除了使用上面的方式,也可以使用静态require方式。
假设有两个XARPackage: pacakgeA和packageB,需要在packageB里加载packageA里的模块,则可以直接在packageB使用相对路径对packageA的test模块require:
// packageB
let theModule = require('../pacakgeA/test');
包之间循环依赖
需要说明的是XARPackage包之间不能循环依赖,即不能同时:
- a包里面依赖b包的模块,
require('../b/b')同时 - b包里面依赖a包的模块
require('../a/a')
此时,需要将某一个require改成在函数里面动态loadModule
// b/b.js
async function test(){
const [,moduleA] = getCurrentRuntime().loadModule('a:a');
}
module.exports = {
test
}
如何在XARPackage内加载同XAR的其他js模块?
XARPackage内部模块的加载,也使用相对路径,例如同一个包内有test1.js和test2.js两个文件.
// test1.js
async function hello(){
console.log('hello');
return 'hello';
}
module.exports = {
hello
};
// test2.js
// 相对路径方式加载其他js模块
const test1Module = require('./test1');
async function test(){
await test1Module.hello();
}
module.exports = {
test
};
如何在XARPacakge内加载node的库
node库采用白名单机制,当前白名单如下,如果有其他需求,请联系buckycloud技术支持。
{
"util":true,
"path":true,
"assert":true,
"crypto":true,
"url":true,
"dns":true,
"events":true,
"http":true,
"https":true,
"http2":true,
"xmlhttprequest":true,
"mysql":true,
"mongodb":true,
"ws":true,
"request":true,
"moment":true,
"twilio":true,
"authorizenet":true,
"passport-facebook":true,
"pg":true,
"uuid":true,
"async":true,
"node-jose":true
}