Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile kotlin code to both JVM and JavaScript

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?

like image 599
Jack Shade Avatar asked Jun 05 '15 16:06

Jack Shade


People also ask

Can Kotlin compile to JavaScript?

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.

Does Kotlin compile to JVM?

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.

Does Kotlin compile to machine code?

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.

Is Kotlin faster than Nodejs?

In general Kotlin is supposed to be faster since it compiled language compared to JS which is interpreted language.


2 Answers

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

like image 102
Sergey Mashkov Avatar answered Oct 13 '22 17:10

Sergey Mashkov


I created a projet kotlin maven targeted JVM, it can be compiled to both JVM and JS.

  1. Open Intellij IDEA -> File -> new -> Project -> Maven -> check "create from archetype" -> choose "org.jetbrains.kotlin:kotlin-archetype-jvm"

  2. 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.

  1. Create a class kotlin named Person in path src/main/kotlin/com.example.training/

    data class Person (
        val id, Int,
        val firstname: String)
    
  2. 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" enter image description here

    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>
    
    1. Run mvn clean compile

    2. 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.

enter image description here

like image 34
zichen.L Avatar answered Oct 13 '22 19:10

zichen.L