abner / flutter_js

A Javascript engine to use with flutter. It uses quickjs on Android and JavascriptCore on IOS
MIT License
464 stars 116 forks source link

Is it possible to access a js class from another file ? #115

Open behzad-tmr opened 1 year ago

behzad-tmr commented 1 year ago

I have to communicate with a js code with lots of files and classes . the problem is that when I run runtime.evaluate("""${jsFile}myFunction()"""); the other classes can not be imported . following is a simple example that what i need to do .

lets have two js files like following in the flutter assets folder . first file : ClassOne.js

class ClassOne { constructor() { this.id = 1; }

sayHi() {
    console.log(`Hello from ${this.constructor.name} with ID: ${this.id}`);
}

}

and second file : ClassTwo.js

function myFunction( ) { let j = new ClassTwo(); j.sayHi(); return 0 ; }

class ClassTwo extends ClassOne{ constructor() { super(); this.id = 2; }

sayHi() {
    console.log(`Hello from ${this.constructor.name} with ID: ${this.id}`);
}

}

Is it possible to import the first class in second one . ) runtime.evaluate("""${jsFile}myFunction()""");

visign3d commented 1 year ago

you can try minify all your classes in one single file and copy/paste the code at the start of your file. then write some logic for interop.

jpolstre commented 1 year ago

I use https://rollupjs.org/