I need to wrap some JS apis (classes first and for...
# javascript
b
I need to wrap some JS apis (classes first and foremost) with custom wrappers to make them usable from within Kotlin; I was wondering if I could ship that code in a js() call instead of manually adding the JS file at load time
✅ 1
Untitled
Code in question that is currently setting those values on the globalThis object to make it accessible at runtime
needless to say this might produce conflicts at runtime
t
Do you use any bundler?
Is it WA for final application?
b
just the default stuff, so webpack I guess
t
You just want to patch some JS service, right?
If yes - it's classic shim (you can add it in bundler configuration), which will be applied before application start
b
right, I need to do this only for my stuff though; everything else should just use the standard class whereas my class should use the shim; now if I had multiple different scripts and they shared the shim over the window object, they'd all need to receive the same shim or things start to break. I guess the thing that I want is to not leak the shim into global space and keep it private per build process. I guess I'd need to somehow place that file at a location where it can be imported during the webpack build and create actual type definitions in Kotlin? I suppose just inlining that with js() wouldn't do that?
t
In separate subproject you can register js file as NPM dependency and provide declarations for it 😉
Copy code
npm("my-additional-wrapper", file("src/jsMain/resources/my-additional-wrapper.js"))
b
oh, thanks 🙂
t
Limitation - it will work only inside current Gradle project
b
yeah, that's good enough