Hello, has anyone here used a Kotlin/JS library in...
# javascript
s
Hello, has anyone here used a Kotlin/JS library in Vite? I have this KMM library that I want to export for both JVM and JS targets. I have a front end that is written using svelte. When I export my KMM library as a web-pack package and install it using npm, it works when I import it in a javascript file and run that independently from vite, but it fails when I have it imported in some javascript code that is run through vite. I suspect it has something to do with an unsupported module format by vite; apparently, it tries to convert umd/commonjs modules to ESM. Could this step fail? I have tried many different ways to export the library code. But to be honest, I have no idea what I am doing most of the time. I know next to nothing about the javascript ecosystem, I mainly want to just export my KMM code and have it useable in the frontend. If someone could point me in the right direction, that would be greatly appreciated :)
t
Possible options (not tested): 1. Commonjs a. Use
commonjs
format for bundle, produced by Kotlin/JS b. Activate
commonjs
for Vite, which isn’t enabled by default 2. ESM a. Use
commonjs
format for bundle, produced by Kotlin/JS b. Convert
commonjs
bundle to ESM via custom Gradle plugin c. Vite configuration isn’t required
Do you use IR compiler? Which Kotlin version do you use?
n
@Stan van der Bend did you figure this out in the end?
@turansky can useEsModules() help here?
t
Vite must work fine with ES modules
n
Thanks, I got it all working in the end reading your comments on an issue on YouTrack!
So much undocumented stuff in kotlin js
t
It works! :)
🤟 1
s
@Nick Williams hey, I did figure it out in the end, its been a while but could spin up a repo for it.
Oh nvm I just read u already figured it out :))
n
I did, but thanks for getting back 👍
a
How about the rest of us, we don't know what you figured out 🙂
maybe was this:
Copy code
plugins {
    kotlin("js") version "1.8.20-RC2"
}

kotlin {
    js(IR) {
        useEsModules()
        browser()
    }
}
from here https://youtrack.jetbrains.com/issue/KT-12784/JS-generate-ES2015-compatible-modules
593 Views