I really like the idea of coding a framework once, and then being able to compile it as jvm byte code as well as to javascript for web use.
Is this currently possible with the kotlin compiler?
Kotlin/JS provides the ability to transpile your Kotlin code, the Kotlin standard library, and any compatible dependencies to JavaScript. The current implementation of Kotlin/JS targets ES5. The recommended way to use Kotlin/JS is via the kotlin.
The Kotlin compiler for JVM compiles Kotlin source files into Java class files. The command-line tools for Kotlin to JVM compilation are kotlinc and kotlinc-jvm . You can also use them for executing Kotlin script files.
Yes, when targeting the JVM, Kotlin is compiled to JVM *. class files, which is a bytecode format that can later be either interpreted by a JVM, or compiled to the machine code by the JVM during the program run (JIT), or even compiled ahead-of-time (AOT) down to the machine code.
In general Kotlin is supposed to be faster since it compiled language compared to JS which is interpreted language.
It is possible but you may face some difficulties. First of all you can build and configure it only with Maven: just setup both executions. The second issue is that IDE can't deal with multiple targets so you can use tricks to enable/disable stdlib/kotlin-js-library
You can see it at https://github.com/Kotlin/kotlinx.html
It is multimodule project.. Module jvm is only compiled for JVM, module js only to javascript, module shared to both
Notice maven profiles: when you edit shared module you can enable js or jvm but not both: otherwise IDE will go crazy. During compilation both profiles are active by default so you will get multitarget jar
I created a projet kotlin maven targeted JVM, it can be compiled to both JVM and JS.
Open Intellij IDEA -> File -> new -> Project -> Maven -> check "create from archetype" -> choose "org.jetbrains.kotlin:kotlin-archetype-jvm"
Edit GroupId:com.example.training; ArtifactId:kotlin2js; Version:1.0-SNAPSHOT
NOTE: a name of project (module) should not contains "-" (a dash) but "_" (an underscore) is ok.
Create a class kotlin named Person in path src/main/kotlin/com.example.training/
data class Person (
val id, Int,
val firstname: String)
Edit pom.xml
a) Add dependency "kotlin-stdlib.js"
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-js</artifactId>
<version>${kotlin.version}</version>
</dependency>
b) Add goal "js" in plugin "kotlin-maven-plugin"
c) (Optional for interoperability Kotlin/JS) Add plugin for unpacking js files needed which are in lib "kotlin-stdlib-js"
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-js</artifactId>
<version>${kotlin.version}</version>
<outputDirectory>${project.build.directory}/js/lib</outputDirectory>
<includes>*.js</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Run mvn clean compile
Folder classes contains Person.class for java. Folder js contains kotlin2js.js and kotlin2js.meta.js for JS, all js files unpacked are in child folder lib.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With