To actually start transpiling some Kotlin into JavaScript, let's add a main.kt file to our project. To do so, we'll walk through the following steps:
- Right-click on the src directory in the project pane and select New | Kotlin File/Class.
- In the New Kotlin File/Class dialog, type main.kt and then click OK.
- Navigate to main.kt and add the following code. This code will log a simple string message out to the console of the target environment:
fun main() {
val outputMessage = "Hello Kotlin JavaScript"
println(outputMessage)
}
To this point, this should look very familiar. The Kotlin code we've written in main.kt is no different than what we would write if we were targeting the JVM. However, if we now build the project, we can see a difference in the resulting build artifacts, as shown in the following screenshot:
After building our project, the following four artifacts have been created:
- lib/kotlin.js
- lib/kotlin.meta.js
- ch14.js
- ch14.meta/js
In the next section, we're going to look more closely at these outputs, and what they contain.