Hi, can you please point me to an example or docum...
# javascript
m
Hi, can you please point me to an example or documentation on how to use javascript modules in Kotlin js code? I only found this document but there are no actual examples on where/how to implement the extenal functions/classes/interfaces defined in Kotlin code. The use case is that I would like to develop a KMP SDK/library and publish it as an npm package as well. Thanks!
a
Hey. Could you please help me to understand your use-case? Do you want to consume JS module from Kotlin, or consume Kotlin/JS code in JS?
For the first one (to consume NPM-package in Kotlin, as an example), you need to check this document For the other case (consumption of Kotlin code from JS) - this document. Also, I have a small example of KMP application with the use-case of sharing code in between of platforms: https://github.com/JSMonk/make-friends-with-kotlin-devs/tree/step-3-share-api-requests
m
Hi, I would like to implement part of the kotlin/js code in js. Because eg. some APIs are not available yet in Kotlin
as a concrete example: if I have a declaration like this:
Copy code
@JsModule("hello")
external fun sayHello(name: String)
where should I implement sayHello in js?
r
m
Thanks @Robert Jaros, unfortunately, there is no example on using a local JS module in Kotlin in that article
e
I think what you're looking for is to have a small subset of code developed in JS locally, inside the same Gradle module. In that case you can place your
.js
files under the
resources
directory, e.g.,
resources/hello.js
m
Exactly @Edoardo Luppi. And then how do I bind them in the JsModule annotation?
e
If you're using default export in JS, you can use
Copy code
@file:JsModule("./hello")
And on the function
Copy code
@JsName("default")
public external fun sayHello()
I suppose it should work.
m
Thank you, this worked, even without the default export. The only caveat was that I had to use ESM syntax