not sure how to implement it
# server
h
not sure how to implement it
g
What do you exactly want? Kotlin already can be compiled to JavaScript and can use JS libraries and also Kotlin code can be shared between platforms (use the same Kotlin code on JVM and JS) Maybe I just didn’t get your question
h
okay so i’m using javalin currently. and i have a public folder in my resources with a bunch of javascript files. if i were to use kotlin js, i’m not sure how i would set it up
im currently using gradle for kotlin on the jvm
g
Just compile Kotlin to JS and set this output js dir as a path with static content
h
and how do i go about doing that?
g
It actually depends how you deploy your app
doing what? Compile to js or configure javalin?
h
compile
h
and will it do this to all files, or can i chose only to compile js files in a specific directory
g
It exactly the same as with JVM
you create a gradle project (and configure it for Kotlin JS, see official docs, there is Gradle docs) put your sources to src dir and compile all of them
h
i don’t want to do all of them. only the ones i have in /resources
g
I don’t understand you
to compile Kotlin source files to js you need a separate Gradle project
h

https://i.folder.rip/z2v53195ez.png

I already have all of these files running on Kotlin JVM
if i also want kotlin files to run on JS, where should i put them so that only they are compiled in JS?
g
do you want to share some kotlin sources between JVM and JS?
h
yeah!
g
h
oh, okay.
i’ll try that next project. thanks!
g
you create a separate “common” module, put there all the sources that you want to share between platforms and add this module as dependency to platform specific moduls (jvm or js)
h
ah, okay
g
don’t forget that you cannot use JVM or JS specific APIs on common code
but it’s a good way to share business logic or data objects
h
so if i create a multiplatform project, it will create 3 modules JVM JS and Common where jvm can use jvm stuff, js can use its stuff, and common can use neither,and is like a bridge between the two
g
no, it’s not a bridge, you cannot connect js and jvm code (for example call js from jvm), you only can reuse some code
h
what code can be reused?
g
pure Kotlin code that doesn’t use platform specific APIs
in Kotlin JS you can use browser API but you cannot do that in JVM
h
so like i can create data classes with a bunch of shared variables?
g
also on JVM you can use Java APIs
shared variables?
it can be any code that you can write on pure Kotlin + it also supports actual/expect declarations
h
like
Copy code
data class config(setting1: boolean, setting2: boolean)
type of thing
g
check docs
You can share such data class, yes
h
okay. great. Thanks!
g
but not only data classes, you have access to kotlin stdlib (excluding platform specific methods)
h
okay
g
also good talk from Kotlin Team

https://www.youtube.com/watch?v=afc5PUs_EPE

h
ok. thanks!
g
also a few articles about MPP on kotlin-academy https://blog.kotlin-academy.com/multiplatform/home
h
thanks alot!
g
about your original question about JS, you can also keep using js specific API, just do not share it with JVM, you can have pure JS module, check docs about Kotlin JS
g
We're doing this: three modules, backend, JS and common
It works OK, we can reuse some code and of course we get static type checking for the part that gets compiled into Javascript
Using existing Javascript frameworks is a bit of a pain - we're using Vue and sometimes you just can't escape Javascript specifics
Kotlin for example tries to hide Javascript's handling of
this
while Vue depends on the default behavior
If I had to start again I'd try using React because JetBrains has some Kotlin integration
Specifically, we're sharing data transfer classes, some validation code, and we have URL paths defined in Kotlin objects so both parts of the web app can use them
It's still definitely two codebases though
We find that trying to reuse too much causes more pain than gain
d
Writing pure vue is very simple, and it has great editor support. I would advice against trying to use kotlin for JavaScript.
g
Imo vue uses too many dynamic magic under the hood so it works not so smooth as should. React less magic so works better. Kotlin specific UI framework would be probably even better
💯 1
1
p
while playing with vue I discovered it has a really cool hot-reload functionality, you save a file and the component in question updates in place without any actual browser reloading.. so smooth! does react offer that?
h
It Does!
p
cool
almost makes front end development fun.. almost
until you need to reconfigure webpack to do something a little bit different.. then good luck 😄
h
I'm yet to use the cool kotlin stuff. Still getting used to kotlin x html
p
yeah I just use Kotlin for JVM, for some reason Kotlin for JS rubs me the wrong way.. feels out of place
g
It is a bit - our Kotlin JS codebase is pretty small (just the admin backend), so we gave it a try
..would've been simpler to just write it in JS and we wouldn't be worse off
n
If it is a web full stack project then using Kotlin JS would be preferred over JS for the front-end.