Kotlin to JavaScript and Kotlin to JVM(Servlets) in one project


#1

It is interesting to know when the opportunity is being prepared to create web projects on the “pure” Kotlin? I would like to know how it is planned to implement client-server data exchange (AJAX) and how will the code be shared between the client and the server?


#2

Currently, we are working both on multi-platform projects and Kotlin serialization. This will allow to write code shared between JVM and JS (and possible Native) and communicate between them. However, work is incomplete and we can’t even estimate the time.

As a workaround, you can create two modules, one for JVM, another for JS, first one containing code in its src folder, and second one containing just synmlink to sources of the first one. This approach has many drawbacks, but for now we can’t propose something better.

To communicate between client and server, you should use corresponding. On the server you may use any existing Java or Kotlin framework (personally, I prefer CXF, while Kotlin has its own framework called Ktor) to create a REST endpoint. On the client you may use XMLHttpRequest, which is merely an external declaration of corresponding JavaScript API.

BTW, here’s an example of fullstack application which includes code both for JVM and JS: https://github.com/Kotlin/kotlin-fullstack-sample.


#3

In my project I used the “metadata” approach: placing the code shared between JS and JVM in a “share” project which will only produce metadata. The JS or JVM compiler will this pickup and compile the final code. Is this approach not recommended? (one problem is the @JsName annotation which is not in kotlin-std-common)


#4

I have reverted back to using gradle to copy the shared (JVM) code and create javascript from it. It uses the gradle copy task but it then actually filters the source files to strip all java based annotations out of it (Both @JvmName and @SuppressWarnings), but have not really tried out using type aliases instead. Obviously if you need @JsName you can do the reverse with JVM code or have a shared base and two “generated” copies for JVM and JS.


#5

In my project I used the “metadata” approach: placing the code shared between JS and JVM in a “share” project which will only produce metadata.

How are you going to produce the metadata? In what format?

What is this shared code expected to do?

The JS or JVM compiler will this pickup and compile the final code

How?


#6

I don’t understand your question, but this is my maven config:

pom.xml from share:

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

        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>metadata</id>
                    <goals>
                        <goal>metadata</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-metadata</id>
                    <goals>
                        <goal>test-metadata</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

JVM pom.xml:

    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
        <plugin>
            <artifactId>kotlin-maven-plugin</artifactId>
            <groupId>org.jetbrains.kotlin</groupId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <multiPlatform>true</multiPlatform>
            </configuration>
        </plugin>

This works - its true, its not comfortable (IntelliJ doesn’t quite understand this setup, eg. I cannot execute the unit tests from the JVM project, since IntelliJ complains about missing code from share). But since project is small, executing the root goal (like “test” or “package”) is not a problem.

Share contains code which only uses code from kotlin-std-common. Its just code…?