Compile kotlin code for both JVM and JavaScript

I really like the idea of ​​encoding the framework once, and then the ability to compile it both jvm byte code and javascript for use on the Internet.

Is this possible with the kotlin compiler?

+6
source share
3 answers

Perhaps, but you may encounter some difficulties. First of all, you can only create and customize it using Maven: just tweak both executions. The second problem is that the IDE cannot deal with multiple goals, so you can use tricks to turn stdlib / kotlin-js-library on / off

You can see it at https://github.com/Kotlin/kotlinx.html

This is a multi-module project. The jvm module is compiled for JVM only, the js module is for javascript only, a module common to both

Pay attention to maven profiles: when editing a general module, you can enable js or jvm, but not both: otherwise the IDE will go crazy. During compilation, both profiles are active by default, so you will get a multi-charge jar

+7
source

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

Check out my project https://github.com/C06A/KUrlet , where I did just that: included common code at the root level and included its source directory in the sourceSets property of each submodule (one JVM target and one JS).

0
source

I created a JVM for the Projet Kotlin Maven, which can be compiled for both the JVM and JS.

  1. Open Intellij IDEA → File → New → Project → Maven → tick “create from archetype” → select “org.jetbrains.kotlin: kotlin-archetype-jvm”

  2. Change group id: com.example.training; Artifact: kotlin2js; Version: 1.0-SNapshot

NOTE: the name of the project (module) must not contain “-” (dash), but “_” (underscore) is ok.

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

    data class Person ( val id, Int, val firstname: String) 
  2. Edit pom.xml

    a) Add the dependency "kotlin-stdlib.js"

      <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-js</artifactId> <version>${kotlin.version}</version> </dependency> 

    b) Add the target "js" to the plugin "kotlin-maven-plugin" enter image description here

    c) (Optional for Kotlin / JS compatibility) Add a plugin to unpack the necessary js files that are in the 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. The class folder contains Person.class for Java. The js folder contains kotlin2js.js and kotlin2js.meta.js for JS, all the unpacked js files are in the lib child folder.

enter image description here

0
source

Source: https://habr.com/ru/post/988592/


All Articles