I have a KMP library with a JS library export. My ...
# javascript
s
I have a KMP library with a JS library export. My Kotlin declarations are namespaced in JS so for example the user has to do:
Copy code
const { PokeApi } = require('pokekotlin').co.pokeapi.pokekotlin
Is there any way to override that namespacing so JS users can use my library like:
Copy code
const { PokeApi } = require('pokekotlin')
Or alternatively, is there a way to include some custom js and .d.ts in my Kotlin/JS library export? So I can define an index.js myself that re-exports my declarations from the root My goal is to have a single KMP library with an idiomatic API for Kotlin, Java, JS/TS, and Swift/ObjC, but the package name being exposed to JS is awkward there. It’s more than just the import syntax; LSP tooling like automatic imports and stuff don’t work well when the importable declarations are buried under a package hierarchy.
e
You can obtain that result in ESM mode, instead of CJS.
s
oh nice, that works! it’s as easy as:
Copy code
kotlin {
  js(IR) {
    browser()
    nodejs()
    useEsModules()
    binaries.library()
    generateTypeScriptDefinitions()
  }
}
and it seems to work in both browser and nodejs. I don’t care too much about losing commonjs support; many libraries have moved on to ESM-only so I have no problem doing that too
nice spin 3
e
Yes, unless your environment is constrained, ESM is a good pick nowadays.