LiquidPlayer / LiquidCore

Node.js virtual machine for Android and iOS
MIT License
1.01k stars 127 forks source link

Requiring another js file from resources #146

Closed sohayb closed 4 years ago

sohayb commented 4 years ago

Hello, In my Android app, I'm starting a js file from the res/raw folder as a MicroService, In that file, I'm importing js library from the same folder but it's not working. I tried 2 different approaches:

const converter = require('./converter')
const converter = require('android.resource://org.liquidplayer.helloworld/raw/converter')

Please note that when running those js files on the dev machine, the first way obviously works, but on Android it doesn't. Any help on how to do this? Please note that when I tried the other approach of just loading the library into a JSContext, I got an exception Reference Error: module is not defined

val jsVal = assets.open("converter.js").use {
    it.bufferedReader().readLines().reduce { acc, s ->  "$acc\n$s"}
}
context.evaluateScript(jsVal)
ericwlange commented 4 years ago

@sohayb Node.js in LiquidCore cannot access resources directly. This is intentional. To require other js files, you must use a bundler. liquidcore-cli has this feature. See the Hello World tutorial for info on how to install it. To create the bundle, you can use:

npm run bundler -- --platform=android --entry-file=MyEntryJSFile.js --bundle-output=MyBundledFile.js

If you required another file (e.g. converter.js), it will get bundled into MyBundledFile.js and work as you expect.

sohayb commented 4 years ago

Thanks a lot @ericwlange . Will try that out and let you know and close this ticket 👍

sohayb commented 4 years ago

This worked perfectly, thanks 🙌