https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
d

Drew

01/24/2019, 4:40 PM
Does anyone know how to generate the
kotlin.js
from a
js
mpp target?
h

hallvard

01/25/2019, 9:22 AM
I found this somewhere, the 'outputFile' setting did the trick in my case:
Copy code
kotlin {
    targets {
        fromPreset(presets.js, 'js') {
            tasks.getByName(compilations.main.compileKotlinTaskName).kotlinOptions {
                // verbose = true
                sourceMap = true
                sourceMapEmbedSources = "always"
                suppressWarnings = true
                metaInfo = true
                outputFile = "$project.buildDir.path/js/${project.name}.js"
...
d

Drew

01/25/2019, 3:43 PM
@hallvard are you talking about your project’s
output.js
, or the Kotlin platform
kotlin.js
that needs to go with it? I think
outputFile
refers to
output.js
But I did find out how get the
kotlin.js
as part of the build process, after searching this Slack:
Copy code
task linkJs {
    dependsOn "compileKotlinJs"

    doLast {
        def jsMain = kotlin.targets.js.compilations.main
        jsMain.runtimeDependencyFiles.each { file ->
            copy {
                from(zipTree(file.absolutePath), {
                    includeEmptyDirs = false
                    include { fileTreeElement ->
                        def path = fileTreeElement.path
                        path.endsWith(".js") && (path.startsWith("META-INF/resources/") ||
                                !path.startsWith("META-INF/"))
                    }
                })
                into jsMain.output.classesDirs.getAsPath()
            }
        }
    }
}

jsMainClasses.dependsOn("linkJs")
h

hallvard

01/25/2019, 3:44 PM
Ah, yes, let me try that! I didn't see it before now, but your question was actually my question (have been asking today myself!). And this might be my answer ... Thanks!
d

Drew

01/25/2019, 3:45 PM
Nice. It worked great for me. I also added a small part that I left out here, where I write all the JS (kotlin and output) to one single file
h

hallvard

01/25/2019, 6:47 PM
Could you share that last bit too? It could be useful ...
a

Allison

01/30/2019, 2:56 PM
@Drew Any chance you could please share your code to output all the JS to one file?
d

Drew

01/30/2019, 7:06 PM
@Allison And @hallvard yes sorry
@Allison I'm not at my computer at the moment. But basically I just make a list of all expected output files (kotlin.js, output.js, etc.). Then I make a new file called "common_output.js" and just append each file into that one file, making one large file.
I can send code later
a

Allison

01/31/2019, 5:31 PM
ok that's really helpful @Drew, thanks very much!
d

Drew

01/31/2019, 6:48 PM
Here’s the full code:
Copy code
task linkJs {
    dependsOn "compileKotlinJs"

    doLast {
        def jsMain = kotlin.targets.js.compilations.main
        jsMain.runtimeDependencyFiles.each { file ->
            copy {
                from(zipTree(file.absolutePath), {
                    includeEmptyDirs = false
                    include { fileTreeElement ->
                        def path = fileTreeElement.path
                        path.endsWith(".js") && (path.startsWith("META-INF/resources/") ||
                                !path.startsWith("META-INF/"))
                    }
                })

                into jsMain.output.classesDirs.getAsPath()
            }
        }

        // Append all JS into one file
        // Very rudimentary, but you get the idea
        List<File> jsFiles = new ArrayList<File>()

        File kotlin = new File(jsMain.output.classesDirs.getAsPath() + "/kotlin.js")
        File kotlinSerialization = new File(jsMain.output.classesDirs.getAsPath() + "/kotlinx-serialization-runtime-js.js")
        File output = new File(jsMain.output.classesDirs.getAsPath() + "/output.js")

        jsFiles.add(kotlin)
        jsFiles.add(kotlinSerialization)
        jsFiles.add(output)

        File commonOutput = new File(jsMain.output.classesDirs.getAsPath() + "/common_output.js")
        commonOutput.createNewFile()
        commonOutput.write("")

        for(File jsFile : jsFiles) {
            commonOutput.append(jsFile.text + "\n")
        }
    }
}

jsMainClasses.dependsOn("linkJs")
🙏 2
a

Allison

02/01/2019, 10:12 AM
^^ worked a treat, thanks a lot for sharing
3 Views