Declaring Support for Kotlin/JS

Your first step is to declare a new target in your code for JavaScript. This will once again involve updating your build.gradle file.

Listing 26.1  Declaring a JavaScript target (build.gradle)

...
kotlin {
    jvm()
    macosX64() {
        binaries {
            executable()
        }
    }
    js {
        browser()
        binaries {
            executable()
        }
    }
    sourceSets {
        ...
    }
}
...

The only thing new here is the browser function call. This specifies that you intend to run your JavaScript inside a browser. You can also use nodejs to have your project compile for Node.js, a standalone JavaScript runtime environment commonly used to build web servers.

Perform a Gradle sync after making these changes.

Once again, your project will no longer compile, because ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.