atok
atok

Reputation: 5938

Is there a way to compile a Kotlin class to JavaScript using Gradle?

I'm building a project using Kotlin Gradle plugin. In general, I'm targeting JVM but it would be very useful to compile a part of the project to JavaScript.

I know it's possible for a project to target JavaScript while building with IntellJ plugin (as described in Writing Kotlin in the Browser blog post), but how to do it with gradle? I'm ok with spliting the project into few gradle modules.

Upvotes: 6

Views: 1243

Answers (1)

yole
yole

Reputation: 97148

You need to use the following plugin:

apply plugin: "kotlin2js"

You can also specify additional settings:

compileKotlin2Js.kotlinOptions.sourceMap = true
compileKotlin2Js.kotlinOptions.outputFile = "${projectDir}/web/js/app.js"
compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = true

You can find a complete test project using Gradle to compile to JavaScript here.

The support for JS compilation in the Gradle plugin is a community contribution, and I frankly don't know how well it works. If you run into any problems, please do report them in the Kotlin issue tracker.

Upvotes: 7

Related Questions