Jack Shade
Jack Shade

Reputation: 501

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?

Upvotes: 7

Views: 1667

Answers (3)

zichen.L
zichen.L

Reputation: 370

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

Upvotes: 1

C.A.B.
C.A.B.

Reputation: 583

For some time this would cause the problems. However the idea is very good, so people keep asking.

Check my project https://github.com/C06A/KUrlet where I did just this: include shared code in the root-level module and included its source directory into sourceSets property of each submodule (one targeting JVM and one -- JS).

Upvotes: 0

Sergey Mashkov
Sergey Mashkov

Reputation: 4760

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

Upvotes: 7

Related Questions