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.
Kotlin/JS provides the ability to transpile your Kotlin code, the Kotlin standard library, and any compatible dependencies to JavaScript. The current implementation of Kotlin/JS targets ES5. The recommended way to use Kotlin/JS is via the kotlin.
Set up a Kotlin/JS project Kotlin/JS projects use Gradle as a build system. To let developers easily manage their Kotlin/JS projects, we offer the kotlin. js Gradle plugin that provides project configuration tools together with helper tasks for automating routines typical for JavaScript development.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With