Hi everybody I’m trying to create a multiplatform ...
# javascript
a
Hi everybody I’m trying to create a multiplatform library with no external deps, in particular I need to cover these two platforms: • JVM => in order to reuse it in Scala/Java/Kotlin projects • Javascript => in order to use in Cloud Functions (Azure/AWS/Google Cloud) The output of the compilation works perfectly for the JVM target but struggling with the output of Javascript Compilation (I’m using
IR
). The produced code seems not do depend to Kotlin as it contains internally all the required data structures and they’re not compatible with the Kotlin.js library Can somebody help me or point to some useful resource that explain how to use kotlin-to-js compiled code into pure js applications?
b
Ir library produced with binaries.library() has all kotlin dependencies bundled in js file and only skips npm dependencies. So no need to add kotlin.js library again
a
@Big Chungus thank you for the answer! I cannot access to Kotlin deps bundled in the js file because they’re not exported in the module
this is the generated js
b
Yes, it's a closed-world approach. Kotlin deps in the bundle are considered "internals" so you can only access stuff your own code marks as @JsExport
even your
public val ...
will not be visible to JS if you don;t annotate that with @JsExport
Have a proper read through the official docs https://kotlinlang.org/docs/js-ir-compiler.html
a
Yep I read that, what is not clear into the docs is, if I have class that implements this:
Copy code
interface IngestionStrategy<KEY, VALUE, EVT> {
    fun mergeNodeEvents(events: Collection<Entity<KEY, VALUE>>): IngestionEvent<EVT>
    fun deleteNodeEvents(events: Collection<Entity<KEY, VALUE>>): IngestionEvent<EVT>
    fun mergeRelationshipEvents(events: Collection<Entity<KEY, VALUE>>): IngestionEvent<EVT>
    fun deleteRelationshipEvents(events: Collection<Entity<KEY, VALUE>>): IngestionEvent<EVT>
}
And the class is annotated with
@JsExport
why the argument (in my case
Collection
because I exported
Entity
) is not exported as well?
Do I need to export the argument?
And how can I do that?
b
Because js has no types, so no way to "export" argument type
your functions in js are represented by arg count
e.g.
function mergeNodeEvents(events)
There are no types nor generics in js world
Also no interfaces
a
yes
but in which way is related to that problem?
I don’t want to export the interface
just the class the implements it
b
And it should get exported by annotating the class with @JsExport
a
it is
b
And Collection is an interface (thus not exportable since js doesn't have that concept)
a
so do you think that I just need to change the method argument to arraylist for instance?
b
having that class exported you should be able to construct it and call its methods from js with any arguments (again, no types in js, so anything goes)
I could easily do this and JS would run:
Copy code
new IngestionStrategy().mergeNodeEvents(1, "not expected type, but string", null, [])
No, you can pass js array to it
Copy code
new IngestionStrategy().mergeNodeEvents([new Entity(), new Entity()])
Just be sure to export Entity as well if it's a class
a
yes
Entity
is exported
but for what I see internally, in
graph-integration.js
the representation of
Collection
exists in a js way, all the classes that implements collections have the same method names
so I continue to don’t understand why internal data structures as
ArrayList
are not exported
b
The answer is simple - because they're not marked with @JsExport
a
And how I can export them?
Because they are Kotlin classes