compilation - Compile kotlin code to both JVM and JavaScript - Stack Overflow

I really like the idea of coding a framework once, and then being able to pile it as jvm byte code as w

I really like the idea of coding a framework once, and then being able to pile it as jvm byte code as well as to javascript for web use.

Is this currently possible with the kotlin piler?

I really like the idea of coding a framework once, and then being able to pile it as jvm byte code as well as to javascript for web use.

Is this currently possible with the kotlin piler?

Share Improve this question asked Jun 5, 2015 at 16:33 Jack ShadeJack Shade 5015 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

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./Kotlin/kotlinx.html

It is multimodule project.. Module jvm is only piled 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 pilation both profiles are active by default so you will get multitarget jar

I created a projet kotlin maven targeted JVM, it can be piled 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:.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/.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"

    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>pile</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 pile

    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.

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

Check my project https://github./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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744270807a4566091.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信