Hi there! Is there a thorough documentation somewh...
# javascript
a
Hi there! Is there a thorough documentation somewhere on how to use Kotlin MPP with a Javascript browser target? I've been struggling for hours to get it working but no luck so far. I have a simple function:
Copy code
fun main() {
    document.addEventListener("DOMContentLoaded", object : EventListener {
        override fun handleEvent(event: Event) {
            println("Foobar")
        }
    })
}
and the corresponding Gradle setup:
Copy code
plugins {
    kotlinMultiplatform
}

kotlin {

    js {
        browser()
    }
}
but I can't see "Foobar" printed and if I just copy the generated code into my browser's console it says
Error loading module 'mymodule'. Its dependency 'kotlin' was not found
. Can someone point me to the docs so I can figure out how to get this working (and also solve a bunch of other issues like how to rename my module and how to run it in dev mode)?
👀 1
After debugging it a bit: I created a KotlinJS client + KotlinJVM server sample project in IDEA and I can see that this part is what does the magic:
Copy code
jvmJar {
    dependsOn(jsBrowserWebpack)
    from(new File(jsBrowserWebpack.entry.name, jsBrowserWebpack.outputPath))
}
but I can't get this to work with the Kotlin build dsl
when I tried this:
Copy code
tasks {
    named<Jar>("jvmJar") {
        dependsOn(withType(KotlinWebpack::class.java))
    }
}
It created the
distributions
folder but got stuck at running my code in the browser during the build (why???)
i
This guide https://kotlinlang.org/docs/reference/js-project-setup.html You can run task
browserRun -t
and it will open browser with js application If you run
build
task, in distributions folder you can get your final application bundle
a
I checked that but it is of no help
if i run
gradle clean build
my
build
folder is just deleted and there is no
distributions
folder
the docs page you linked doesn't work with MPP
i
Do you use parallel build? There is some issues related with gradle, that clean can be run after build You can try separately run these commands
a
i do, wait a moment
i'm gonna try it
so do you have docs for kotlin.js (the new one) somewhere?
I saw the talk on Kotlinconf and I'm eager to jump in 😄
i
We are updating docs now, for setup there is guide above, and it is actual If you use for mpp, you only need to replace plugin on “multiplatform” and “target” word in
kotlin
extension on “js” word as you did
a
ok I ran it without the parallel flag but I still see no
distributions
folder
I think it is because of the missing dependsOn I talk about above ☝️
i
Hmm, maybe yes, dependsOn can be missed, what version do you use? You can try 1.3.70-eap-42, and check if it helps
a
i think the only thing which is missing is
Copy code
jvmJar {
    dependsOn(jsBrowserWebpack)
    from(new File(jsBrowserWebpack.entry.name, jsBrowserWebpack.outputPath))
}
I just don't know how to translate this to
kts
ok now i switched my
kts
with the code from the sample project which IDEA generates and now I can confirm that this was because of the missing
jvmJar
block
and also I needed to use
--no-parallel
is this going to get a fix in the future?
also can you point me to the relevant docs on how to translate the
jvmJar
block to
kts
?
this whole thing doesn't work at all
even if I copy out the generated webpack-built js file
and including it in a html file
it is not doing anything at all
where can I get help?
how am i supposed to use kotlin.js in an mpp environment?
why there are no docs/tutorials?
i
You can show your build script And generated project worked for you?
a
the generated project worked but now with
--continuous
which was in this talk

https://www.youtube.com/watch?v=L4DRD9eWKXo&amp;feature=emb_title

--continuous
is not doing anything and the generated javascript file is 12MB in size not the promised 30KB from the talk
the problem is that the generated project is single-module
my project is multi-module so this part doesn't work
Copy code
jvmJar {
    dependsOn(jsBrowserWebpack)
    println("entry name: ${jsBrowserWebpack.entry.name} output: ${jsBrowserWebpack.outputPath}")
    from(new File(jsBrowserWebpack.entry.name, jsBrowserWebpack.outputPath))
}
the
jsBrowserWebpack.entry.name
doesn't have the same file name as
jsBrowserWebpack.outputPath
one includes the root module the other one does not
so I had to hardcode the file name into it
now it works, but I still don't know what is the supposed way of doing this since I can't find any documentation on this think
and I also can't go into production with a 12MB js file
I also don't know how to translate this into `kts`:
Copy code
jvmJar {
    dependsOn(jsBrowserWebpack)
    println("entry name: ${jsBrowserWebpack.entry.name} output: ${jsBrowserWebpack.outputPath}")
    from(new File(jsBrowserWebpack.entry.name, jsBrowserWebpack.outputPath))
}

task run(type: JavaExec, dependsOn: [jvmJar]) {
    group = "application"
    main = "MyServerKt"
    classpath(configurations.jvmRuntimeClasspath, jvmJar)
    args = []
}
I tried it but it didn't work (see above)
i
@addamsson Can you please create an issue here http://kotl.in/issue and attach your project?
s
@addamsson at least regarding your issue with the 12MB JS file; starting with 1.3.70 we have DCE enabled by default. This will bring you down to the kilobyte range 🙂