Hi, I'm an experienced app developer and have used...
# javascript
c
Hi, I'm an experienced app developer and have used Kotlin Multiplatform for Android & iOS successfully for some time now. I've experimented with using Compose Multiplatform for those platforms. Still, I have found the best success using KMP for the logic and implementing UI natively on Android and iOS. I've been looking more into web development lately and need some help figuring out how Kotlin JS fits into the picture. I've seen some documentation on using Kotlin to build complete web apps with frameworks like KVision, fritz2, or Doodle. However, I have yet to find much documentation or best practices on integrating common code into more industry-standard web frameworks like React, Angular, or Vue JS. With some digging, I found out that you can export common Kotlin code to Typescript and consume that in the framework of your choosing, with some JsExport annotations and the generateTypeScriptDefinitions gradle function. However, given how much digging I had to do to find that, I wonder if that's a recommended approach. The generateTypeScriptDefinitions function seems to have been experimental for years, and its YouTrack issues seem to have been stale for many years. Could someone help me discover how Kotlin Js fits into modern web development?
a
Hey 👋 I'm a little bit biased, because I work on the Kotlin/JS project, but just want to add, that the needs you described (use common code from the modern frameworks) is covered with the current JsExport. Now days we still have it as an experimental part just because we change the way of exporting and representing Kotlin in TS, and we are working on stabilizing it (for example we are still thinking on the way of suspend-function exports and collections export is in development). If you are looking for a way to share your existed Kotlin code with web app - seems like this is the single option (except rewriting it in pure TS). So, I can't give you strong stability guaranties for this part of Kotlin, but we are working on it to make it stable as soon as possible, and if you decide to work with it, you are always welcome to ask questions here, and we will help from our side.
r
Use pure Kotlin/JS solutions if you don't like to work with JS/TS and just want to have single Kotlin codebase and the ability to use Kotlin specific things (e.g. coroutines).
c
@Robert Jaros The unique value proposition I see in Kotlin Multiplatform is the ability to share code across platforms while still using the latest and greatest technologies each platform has to offer. A pure Kotlin/JS solution is great because it gives you all the niceties Kotlin has to offer. However, at that point, it becomes just another framework in the myriad of web frameworks out there, and it wouldn't be my first choice because of popularity (and all the community/support/new features/job opportunities that come with a critical mass of developers). @Artem Kobzar It's great to hear that you guys are still working on TS representations and that it will be a supported way to do things in the future. Is there any reason why this way of consuming Kotlin code isn't emphasized more in the documentation, or do you guys simply feel it's not there yet?
a
We need to describe the current state and put it into the documentation in the near future because it seems like there is nothing to change with the current way of interoping with TS and keep only experimental parts of the export model (like collections interop) experimental. I hope that we will add more information about the exporting model in the near future.
e
I've been developing a multiplatform solution targeting Windows/JVM/JS for quite some months, with JS as the main target. No mobile involved. I did lay down some issues we found to be impacting our experience, and I think some of them have been closed by now. https://kotlinlang.slack.com/archives/C0B8L3U69/p1688562207300679 The major "issue" for me is still exporting suspend functions. It requires writing wrappers with a proper coroutine scope. And the second one that has come up recently is implementing exported interfaces in TS. Still checking what the implications are if I just do it regardless of recommendation (which means I'll have to find out where and how the code breaks).
My experience as of now has been pretty good. 40k+ LOC so far that are basically written to work from a JS-first perspective. What you'll find still lacking is types for external libraries. If you're used to DefinitelyTyped, you won't be happy.
c
@Edoardo Luppi That sounds promising. What approach did you guys take for your Web solution? I assume you don't use a Kotlin web framework but export your common module to a TS library with generateTypeScriptDefinitions? If so, are you consuming the TS library straight from the build folder of your common project, or do you publish it elsewhere for your workflow?
e
My goal was to develop on a monorepo, with no external dependencies. I didn't want to deal with Verdaccio or customized steps between Kotlin builds and consuming the files from other packages, so the monorepo itself is just a npm workspace, and Kotlin distributions are generated under a directory known to the npm workspace. To orchestrate builds I'm using Nx.
With a setup like that I can easily switch sides. Setting up source maps for debugging was mostly free of pain as it's all local and you can also work with relative paths.