Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 423
atok Avatar asked Mar 08 '15 10:03

atok


People also ask

Can Kotlin be compiled to JavaScript?

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.

Can I use Gradle for JavaScript?

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.


1 Answers

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.

like image 98
yole Avatar answered Oct 28 '22 06:10

yole