How do I provide separate implementations for brow...
# javascript
m
How do I provide separate implementations for browser vs nodejs environments in K/JS projects?
t
In library or in application?
m
In a library; I've added both
browser
and
nodejs
as targets in both js and wasmjs, but I can't seem to find anything signifying which platform would be in use, and it looks like I'll need to use
js()
blocks to interface with platform-specific parts of JS?
t
Which platform-specific API do you need?
m
One example would be filesystem access on nodejs
t
Do you have custom Node.js declarations for it?
m
I mean, I'm assuming I'd just use
js("require('fs')")
or
js("process")
to get handles to the APIs, no?
t
In common case you can declare required functions like:
Copy code
@JsQualifier("fs")
fun readFileSync(path: String)
With
require('fs')
you will receive warnings or errors in browser bundlers
Safer require -
require('node:fs')
m
yeah but the point is to only run that code if I can figure out we're on nodejs somehow
t
Bundler will see all code
r
See also this ticket which has some discussion and workarounds. I have no inside knowledge but I'm not aware of any plan to implement it any time soon. https://youtrack.jetbrains.com/issue/KT-47038/
➕ 1