Search code examples
kotlinwebassembly

How to compile Kotlin into WebAssembly in command line?


I would like to compile a few Kotlin classes and enums into WebAssembly in command line. It worked several years ago with kotlinc-native but the wasm32 target was removed. I try to use this example: kotlinc-js -output lib.js -Xir-produce-js -Xwasm -libraries kotlin-stdlib-wasm-2.0.20.klib input.kt but the lib directory has no file named kotlin-stdlib-wasm-2.0.20.klib in Kotlin 2.0.20. How can I simply compile Kotlin into WebAssembly without using Intellij IDEA (and Gradle)?


Solution

  • Follow these steps:

    • Run git clone https://github.com/Kotlin/kotlin-wasm-browser-template.git
    • Edit KotLin version number in gradle/libs.versions.toml if you want to use another version
    • Add -Xwasm-generate-wat into build.gradle.kts as stated here
    • Replace the Kotlin source and test files by your own files
    • Run gradle build
    • Go to build/dist, you should find a subdirectory named compileSync containing the resulting WAT file

    N.B: I don't need IntelliJ IDEA but I haven't found a solution working without Gradle.