I'm having a lot of difficulty getting @JsExport t...
# multiplatform
b
I'm having a lot of difficulty getting @JsExport to work reliably. I have a separate KotlinJs project where it seems okay, but within a multiplatform project the behavior appears inconsistent. From my testing here's the most I know: • At the root level, @JsExport doesn't do anything. However,
main
is always exported regardless. • Within a package, @JsExport works as expected (making sure any identifiers included are not mangled). However, any function not referenced is not included. Functions that are referenced at the root level but not themselves exposed (likely because they aren't referenced?) are still included. Unlike the root level,
main
is not exported. In hindsight, it seems like the issue here is that functions are only included in the output if 1) they're referenced, or 2) they're the root main (rather than being an issue with @JsExport). Is this intended? How can I force the output to include these functions for the purpose of a library?
a
Mark every definition you want exported with
@JsExport
. Do not expect that it will transitively export other definitions that have been referenced by the exported function, but it would be cool if it were
b
That's what I am doing, however the behavior for whether a function is included in the output appears to ignore whether it's annotated with @JsExport or not. Instead, the function must be referenced to be included.
a
I am not getting that behaviour. Everything I mark as with
@JsExport
is correctly being exported, regardless of being referenced or not. How does your typescript definition file look like? does it have those functions?
b
Where would I have a typescript definition file?
Here's the file where I have these exports defined, which is within a package. The
eval
function is referenced from
main
defined in the root package, while
unreferenced
isn't.
And this is the result I see for the JS object -
eval
is present, but
unreferenced
isn't.
a
Where would I have a typescript definition file?
check in top level
build/js/packages
b
Not seeing anything typescript there. I have .kjsm files?
a
does the method call
dev.rhovas.interpreter.unreferenced()
throw?
b
Doesn't look like it, but it is showing up there.
Copy code
public fun unreferenced(): kotlin.Unit { /* compiled code */ }
a
assuming you have
binaries.executable()
or
binaries.library()
in place, you should have a subfolder
build/js/packages/<project-name>/kotlin
and your typescript definitions would be in.? I like to use the typescript definition because it does tend to help you know how our definitions are exported
b
I added
binaries.executable()
(will paste build.gradle after this), but no change.
Here's that section for
build.gradle
. Sidenote; I'm not using the
js(IR)
version here - with that the entire
distributions
folder isn't generated; haven't gotten around to tracking that issue down yet. It seems unrelated but you never know.
message has been deleted
a
change
Copy code
js {
  // . . .
}
to
Copy code
js(IR) {
  // . . .
}
b
I don't get a distributions output if I do that.
a
just go for it. Humor me
also, do not remove the
binaries.executable()
line
b
Right, yeah - just pieced that one together. I believe I had a library error when testing this earlier; going to see what's up.
I've tried so many combinations they're all a bit of a blur with what exactly happens now 😅
Perfect, looks like that did it. No idea what I messed up with that approach earlier but seems to be working now.
Quick followup - the only other difference I have between the current build.gradle and the KotlinJS one is this line. What does this do exactly? Is it needed?
Copy code
tasks.named<org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile>("compileKotlinJs").configure {
    kotlinOptions.moduleKind = "plain"
}
a
That tells the compiler what format it should use to compile the javascript down to. You are better of with commonJs, instead of plain, but there is also umd. To understand further, read here https://kotlinlang.org/docs/js-modules.html#join-the-kotlin-js-community
b
Appreciate it; thanks for your help!
a
You are welcome son 😄