Building KotlinJS with gradle


#1

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.



mainProject.zip (115 KB)

#2

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?


#3

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:

main.java.srcDirs += ‘src’


$ find ./src
./src
./src/main.kt

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: http://blog.jetbrains.com/kotlin/2013/10/writing-kotlin-in-the-browser/

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”)
  el.appendChild(document.createTextNode(“Hello!”))
  document.body.appendChild(el)

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

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

Thanks!


#4

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 src.main.java. It also should work.