Instantiate object from browserify-bundled NPM package in JavaScriptCore in Swift

51 Views Asked by At

I have bundled up an NPM package using Browserify. Then, I import it into Swift

 guard let npmPackage = Bundle.main.path(forResource: "mynpmpackage", ofType: "js") else {
            print("Unable to read resource file")
            return nil
}


npmContent = try String(contentsOfFile: npmPackage, encoding: String.Encoding.utf8)

let jsSource = "var window = this; \(npmContent)"
// I also tried executing npmContent directly below:
context?.evaluateScript(jsSource)
            

Then I want to create an instance of the NPM object, would I need to pass it into the Javascriptcore function?

I don't seem to be able to access the browserify bundled objects, how can I create an instance?

    let jsCode =
"""
async function lookupDataUsingNPMPackage(myaddress, callback, alc) {
            try {
                const config = {
                  apiKey: "myapikey",
                  network: "mynetwork",
                };
                const myInstance = new mynpmpackage(config);
                console.log("hola")
                const { data } = await fetch(myInstance.data.getDataForOwner(myaddress));
                callback(data)
            } catch (error) {
                console.log( error);
            }
        };
    """
0

There are 0 best solutions below