Building KotlinJS with gradle

I've attempted to build the example included in libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/kotlin2JsProject/mainProject

I made a few modifications so that it didn’t depend on a locally defined version of kotlin libraries (in fact I wasn’t sure how to build it otherwise). I’ve attached the entire project as a zip file for convenience.

The main (possibly only) issue is that ‘${projectDir}/web/js/app.js’ does not seem to be generated. (115 KB)

Hi, I couldn't reproduce your problem: 'gradle build' generates app.js file in output directory for me in your project. Which task have you executed?

Well, now I'm not sure exactly what I was doing...; it does seem to work as described when I run the copyJsFilesFromDependencies task.

However, if I move the source file up a few directories and change the source sets accordingly, we no longer get the app.js: += ‘src’

$ find ./src

But I didn’t see any other references to ‘example’ in the code, so I found that just doing: mv src/main/kotlin/example src/main/kotlin/nonexample

would work. However, the critical bit seems to be that sources must reside in src/main/kotlin; is this the case just for kotlin2js plugin? It seems not necessary for the kotlin gradle plugin.
For reference I was trying to follow this outdated blog post, so things have probably changed a bit since then:

Oh, and just so we are on the same page, I had to comment out a few lines in main.kt to get it to compile standalone:

package example import kotlin.js.dom.html.document //import example.library.Counter

fun main(args: Array<String>) {
  val el = document.createElement(“div”)

  // val counterDiv = document.createElement(“div”)
  // val counterText = document.createTextNode(“Counter!”)
  // counterDiv.appendChild(counterText)
  // document.body.appendChild(counterDiv)

  // val counter = Counter(counterText)
  // counter.start()


Do you use the full project kotlin-gradle-plugin/src/test/resources/testProject/kotlin2JsProject with the library module? I didn't see it in your zip file. The class Counter is in library part of example Counter.kt

Moving kotlin classes somewhere instead of src/main/kotlin should work.

A small tip: you can write src.main.kotlin instead of It also should work.