Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Kotlin to JavaScript

I've started looking into Kotlin and I'd like to use it instead of TypeScript for my front end language. Both compile down to JavaScript so I'd like to set it up so that I make a Kotlin file, lets call it myFile.kt, then when I run the compiler it makes a myFile.js file.

Just like how TypeScript takes a myFile.ts and compiles it to a myFile.js.

I'm using the latest version of IntelliJ 15 with release candidate 1 of Kotlin.

I've been searching all over the internet for the way to do this but so far all I've found is setting up IntelliJ so that Kotlin creates a JavaScript library from your code in a JAR file. I also haven't been able to get that to compile any of the kt files in my project (they were previously js files).

Is what I'd like to do currently possible or am I thinking about this in the wrong way?

like image 383
Graham Avatar asked Sep 26 '22 10:09

Graham


1 Answers

I think this should help:

https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/kotlin2JsProject/libraryProject/build.gradle

There are sample of library project. JS sources can be founded in buildDir. So yes, you can get js files from Kotlin sources.

Kotlin language a very different from JavaScript (even ES6), so you can't just rename js to kt, this will not work. You need rewrite javascript source files to Kotlin.

For example:

console.log('Hello, World!');

Should be rewrited as:

fun main(args: Array<String>) {
  println("Hello, World!")
}
like image 93
Ruslan Avatar answered Oct 10 '22 02:10

Ruslan