Next up on our Kotlin Multiplatform journey I thought it would be interesting to look at bringing PeopleInSpace to the web….using Kotlin/JS and making use of same Kotlin shared code that we’ve used on other platforms. In line with project in general we’re going to make the minimum changes needed to support this (these posts are primarily aimed at (a) giving people a “flavor” of what’s possible, and (b) providing minimal setup needed to get someone up and running using particular technology). Note also that this is heavily inspired by this excellent Kotlin/JS codelab.
In a Kotlin/JS project we’re writing our web client in, as you’d expect, Kotlin and making use of Kotlin DSL for HTML (and also, in this case, React and Kotlin Wrappers for React).
The following is all our Kotlin client code (shows very basic list of people in space). Note it makes use of PeopleInSpaceApi()
(and Assignment
data class) which are part of our shared Kotlin Mutiplatform code.
The following is the index.html
file used. It contains root
div element which above Kotlin code refers to and also includes
web.js
which is generated from that Kotlin code.
Gradle updates
These are the main Kotlin/JS related updates to build.gradle.kts
for shared code module.
And following then is build.gradle.kts
for the web
module. Note dependency on common
module which is where our shared Kotlin code resides.
To test this out: from root of project you can run ./gradlew :web:browserDevelopmentRun --continuous
which will automatically open
http://localhost:8080/
and also update that whenever you make changes to any of the Kotlin/JS code.
And the following is the very basic UI you get (an exercise perhaps for reader is to use the React
Kotlin wrappers to make something a little less basic :) )
The above code is included in PeopleInSpace repo (in web
module).
Featured in Kotlin Weekly Issue #196
Related tweet
(purpose of providing this is primarily to help “close the loop” and provide way to post feedback/ask questions etc (in absence for now of that functionality in blog itself).
Next up on PeopleInSpace's #KotlinMulitplatform journey...Kotlin/JS!
— John O'Reilly (@joreilly) May 2, 2020
PeopleInSpace hits the web with Kotlin/JS and React https://t.co/ExmKtBUtq4
This is basically all the Kotlin code included (making use of code from common Kotlin Multiplatform module) pic.twitter.com/Mjf8odvP8l https://t.co/GAapdPKrz7