Gradle doesn't emit kotlin.js

﹥>﹥吖頭↗ 提交于 2020-01-22 20:15:31

问题


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


回答1:


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"




回答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




回答3:


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


来源:https://stackoverflow.com/questions/53582651/gradle-doesnt-emit-kotlin-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!