Kotlin and JavaScript interoperability
In the following recipe, we are going to configure and implement a sample web app project in order to explore how Kotlin can be compiled to JavaScript. We are going to implement a simple web app that will open an alert dialog when the app starts. The following example is going to present a way of combining Kotlin and JavaScript code together and configuring a JavaScript compilation with the Gradle build script.
Getting ready
In order to set up the project to compile Kotlin files into JavaScript, we need to add the following properties to the module-level Gradle build script. First, we need to apply the Kotlin2Js plugin. We can do it with the following declaration:
apply plugin: "kotlin2js"
At this point, whenever we execute the Gradle build
task, the Kotlin2JS compiler is going to generate JavaScript code with the corresponding functions and classes from the Kotlin files and write them under the build/classes/kotlin/
directory to the JS file named after...