6

I'm trying to compile my Kotlin app and set of Kotlin libraries to JavaScript. I've got that working well, but when I try to run it it can't find kotlin.js.

So what's going on here? When I compile using IDEA (instead of Gradle), it outputs kotlin.js just fine. I tried making my build script more like an example I found, but that wouldn't compile...


Here's a link to the code and project in question: https://github.com/BlueHuskyStudios/Decision-Cruncher/blob/SO/q/53582651/1/build.gradle

Ben Leggiero
  • 25,904
  • 38
  • 161
  • 267
  • Please accompany any downvotes with a comment letting me know why my question shows no research effort, is unclear, andor is not useful. That way I can write better questions in the future – Ben Leggiero Dec 11 '18 at 19:19
  • Have you read this doc: [Using Gradle](https://kotlinlang.org/docs/reference/using-gradle.html) – Kousic Dec 12 '18 at 05:58
  • Looks like you already have found a solution. – bashor Dec 12 '18 at 09:24
  • @bashor Why do you say that? – Ben Leggiero Dec 13 '18 at 04:08
  • @Kousic yes that's where I got most of my build.gradle script – Ben Leggiero Dec 13 '18 at 04:08
  • @BenLeggiero Did you figure it out in the end? Facing the same issue and starting to pull my hair – Alexandre G Mar 30 '19 at 00:08
  • 1
    @AlexandreG It's been out of my head for a few months, but I seem to recall wiping the slate clean (something that I often do because I didn't want to deal with a problem anymore, and which works disturbingly often). Uninstalled IDEA and deleted all non-code files, then re-installed IDEA and created a new project from those sources. Maybe the same could work for you! – Ben Leggiero Apr 01 '19 at 04:44

3 Answers3

2

This only worked for me. unzip for some reason was no working

task assembleWeb() {
    configurations.implementation.setCanBeResolved(true)
    configurations.implementation.each { File file ->
        if (file.path.indexOf('kotlin-stdlib-js') >= 0) {
            exec {
                workingDir "$projectDir/web"
                standardOutput = new ByteArrayOutputStream()
                commandLine "7z", "e", file.absolutePath, "kotlin.js", "-aos", "-r"
            }
        }
    }
    dependsOn classes
}

assemble.dependsOn assembleWeb

Be aware of "-aos" param. This flag will prevent from overwriting of existing file

Vlad
  • 5,448
  • 2
  • 43
  • 39
1

Here you can find the code snippet to extract all .js files from Kotlin/JS libraries:

task assembleWeb(type: Sync) {
    configurations.compile.each { File file ->
        from(zipTree(file.absolutePath), {
            includeEmptyDirs = false
            include { fileTreeElement ->
                def path = fileTreeElement.path
                path.endsWith(".js") && (path.startsWith("META-INF/resources/") || 
                    !path.startsWith("META-INF/"))
            }
        })
    }
    from compileKotlin2Js.destinationDir
    into "${projectDir}/web"

    dependsOn classes
}

assemble.dependsOn assembleWeb
bashor
  • 6,563
  • 2
  • 30
  • 32
  • 1
    [I've tried placing that in my build.gradle script](https://github.com/BlueHuskyStudios/Decision-Cruncher/blob/SO/53582651/a/3939277/c/1/build.gradle#L90-L106) and it doesn't work – Ben Leggiero Dec 13 '18 at 04:14
  • This literally does nothing. The output is completely unchanged... My whole codebase is available for you to [clone and see for yourself](https://github.com/BlueHuskyStudios/Decision-Cruncher/blob/SO/53582651/a/3939277/c/1/build.gradle#L90-L106) – Ben Leggiero Dec 14 '18 at 01:00
0

For any others struggling in the future, I've had the following issue:

IntelliJ Kotlin/JS starter project has been generated with this in the gradle file:

implementation "org.jetbrains.kotlin:kotlin-stdlib-js"

which needs to be this to get the kotlin.js file

compile "org.jetbrains.kotlin:kotlin-stdlib-js"

Alexandre G
  • 1,450
  • 16
  • 27