Writing javascript applications with Kotlin

前端 未结 3 490
时光取名叫无心
时光取名叫无心 2021-02-05 07:31

I recently started to have a look at Kotlin and managed to create my first JVM applications. It\'s so cool to have a single language that compiles both to Java and JS. So, now I

相关标签:
3条回答
  • 2021-02-05 07:58

    Using the pom below:

    <?xml version="1.0" encoding="UTF-8"?>
    <project
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
            xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example.kotlin</groupId>
        <artifactId>kotlin-js</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>    
        <properties>
            <kotlin.version>1.3.61</kotlin.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
            <kotlin.compiler.languageVersion>1.3</kotlin.compiler.languageVersion>
            <kotlin.compiler.apiVersion>1.3</kotlin.compiler.apiVersion>
    
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib-js</artifactId>
                <version>${kotlin.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <version>${kotlin.version}</version>
                    <configuration/>
                    <executions>
                        <execution>
                                <id>compile</id>
                                <phase>compile</phase>
                                <goals>
                                    <goal>js</goal>
                                </goals>                        
                                <configuration>
                                    <sourceDirs>
                                      <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                                    </sourceDirs>
                                    <moduleKind>umd</moduleKind>
                                </configuration>
                        </execution>
                    </executions>
                </plugin>                                    
            </plugins>        
        </build>
    </project>
    

    Inside src/main/kotlin you can add Hello.kt containing a fun main(args : Array<String>){println("hello")}

    then the following would print hello

    mvn clean package
    cd target
    npm i kotlin
    node js/kotlin-js.js
    

    or you can add a non-main method and then use require() from other js code or add it to the browser using <script> (together with kotlin runtime).

    0 讨论(0)
  • 2021-02-05 08:23

    Meanwhile Kotlin/JS is available in version 1.2 and there is an introduction, tutorial and reference on the official website.

    0 讨论(0)
  • 2021-02-05 08:23

    if you're interested to learn about using Kotlin in Node.js app, I've put together a starter project that running a node.js server written in Kotlin. https://github.com/techprd/kotlin_node_js_seed

    The aim of this project is to write a full stack web application written entirely with Kotlin JS

    let me know what you guys think about this...

    0 讨论(0)
提交回复
热议问题