mercredi 6 septembre 2017

Kotlin to Javascript, Gradle (Grails 3)

It is challenging to make all those happy things working together.

First, because like me, you are too lazy to read the complete Gradle stuffs, and second, because if you read Kotlinlang website, dedicated sections related to this subject, it will not work :/ .

TL; TR, here is the build.gradle file sections modified for Kotlin2Js to be alive:


buildscript {
    ext.kotlin_version = "1.1.4-3"//'${kotlinVersion}'    
    ext.web_dir = "grails-app/assets/javascripts/kt"    

[. . .]
    dependencies {

        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
} [. . .] apply plugin: 'kotlin2js' [. . .] dependencies { [. . .] compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"}
    [. . .]
assets {
    [. . .] // Nothing to change
}
/* Kotlin javascript section +++ */ clean.doFirst() { delete("${web_dir}") }
build.doLast() {
        // Copy kotlin.js and kotlin-meta.js from jar into web directory    configurations.compile.each { File file ->
        configurations.compile.each { File file ->
        copy {
includeEmptyDirs = false from zipTree(file.absolutePath)
            into "${projectDir}/${web_dir}"
            include "*.js"
} } } compileKotlin2Js.doLast() { copy { includeEmptyDirs = false from new File("build/classes/main")
        include "*.js"
        into "${web_dir}"    }
        }
}
/* Kotlin javascript section --- */ bootRun { jvmArgs('-Xmx3064m', '-Dspring.output.ansi.enabled=always', '-server') } runCommand { jvmArgs('-Xmx8064m', '-Dspring.output.ansi.enabled=always', '-server') }

Assets plugin will compact the JavaScript code as expected.

If you are feed up with AngularJS, 2, 4, AngularDart ... and other Javascript FW who wants to rule the world with MVVMC and others, give Kotlin and simple DOM manipulation a chance! Everything is not perfect, but I used to enjoy static code analysis, to avoid bad behavior at runtime.

You will benefit almost simple Gradle integration (which is not yet available in Dart), IDE more efficient auto-completion, with real static analysis (AngularTS massive JSON uses, makes it more difficult).