Last Updated | 30 September 2016 |
In this tutorial we'll see how to
When creating a new application or module that targets JavaScript, we need to select Kotlin - JavaScript
as the target
The next step is going to prompt us on the Kotlin runtime library. By default the plugin selects the one that is associated to the currently installed version. Unless we want to create a different one, we can click Finish at this point after entering the project name and location.
Once the IDE has finished creating the new project, we should be left with the following layout
At this point we can start writing Kotlin code. For this sample, we're going to write some code that will print a string out to the console window.
fun main(args: Array<String>) { val message = "Hello JavaScript!" println(message) }
We now need an HTML page to load the code, so we'll create a file called index.html
. If you want more information on how Kotlin compiles to JavaScript and the output generated, check out the Kotlin to JavaScript tutorial.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Console Output</title> </head> <body> <script type="text/javascript" src="out/production/sampleapp/lib/kotlin.js"></script> <script type="text/javascript" src="out/production/sampleapp/sampleapp.js"></script> </body> </html>
A couple of important points:
kotlin.js
file should be referenced first as it is used by our applicationThe only thing left to do is compile our application (Build|Build Project), and once the JavaScript files have been generated, we can open the index.html
file in the browser and see the result in the console debug window.
This feature is only supported in the Ultimate edition.
In order to debug the application using IntelliJ IDEA, we need to perform two steps:
Install the JetBrains Chrome Extension which allows debugging inside IntelliJ IDEA via Chrome. This is useful for any type of web application developed with IntelliJ IDEA, not just Kotlin.
Configure the Kotlin Compiler to generate source maps, accessible via Preferences|Kotlin Compiler
Once that's done, we simply right-click on our index.html
file and select the Debug option. This launches Chrome and then stops at the breakpoint defined in our code inside IntelliJ IDEA, from where we can evaluate expressions, step through code, etc.
It is also possible to debug Kotlin applications using the standard Chrome debugger. Just make sure that you do generate source maps.
Kotlin provides a series of compiler options that are accessible in IntelliJ IDEA also. In addition to the one we've just seen for generating source maps, we also have the ability to set
kotlin.js
library to be output to. By default it is lib
which is why in the HTML we are referencing this path.In this tutorial we've seen how to create a Kotlin application that targets JavaScript, debug it as well as set compiler options. In other tutorials we'll cover more in-depth topics such as interacting with the DOM, etc.
© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/tutorials/javascript/getting-started-idea/getting-started-with-intellij-idea.html