https://kotlinlang.org logo
s

Stan van der Bend

01/14/2023, 3:58 PM
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

turansky

01/15/2023, 7:54 PM
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

Nick Williams

06/08/2023, 2:03 PM
@Stan van der Bend did you figure this out in the end?
@turansky can useEsModules() help here?
t

turansky

06/08/2023, 5:44 PM
Vite must work fine with ES modules
n

Nick Williams

06/08/2023, 5:50 PM
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

turansky

06/08/2023, 5:53 PM
It works! :)
🤟 1
s

Stan van der Bend

06/18/2023, 1:49 PM
@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

Nick Williams

06/18/2023, 3:00 PM
I did, but thanks for getting back 👍
a

apatrida

09/21/2023, 12:18 AM
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
216 Views