Hi guys, is there a built-in way to access whether...
# javascript
j
Hi guys, is there a built-in way to access whether I’m in production or dev from Kotlin/JS code running in the browser?
g
fun isBrowser() = jsTypeOf(window) != “undefined”
but careful, it will be false for web worker
j
Thanks for the info, but I meant from browser code, how can I see whether it was production or dev build?
g
j
Is something like
process.env.NODE_ENV
also available from the browser? How can I access that from Kotlin?
g
the same as from JS
j
Mmmh then do I have to declare the
process
global variable as an external myself? Or is it somewhere in Kotlin/JS?
g
yes, yourself, dev/prod is not something bundled to Kotlin/JS, it’s webpack feature
👍 1
so you can use it the same way as in JS
j
I understand for dev/prod but I thought
process
itself was something more general than just webpack, that’s why I expected some existing declaration from Kotlin/JS.
Thanks a lot for your help
g
yeah, Kotlin doesn’t have it, pure Kotlin/JS is actually pretty close to Vanilla JS, you have only what you have in your source code
j
Yes, fair enough. I was considering it in the same bucket as
window
and the likes, which is AFAIU something that’s provided by the browser, not in the source code. Somehow I imagined
process
to fall in the same category, given its name, but I just checked and it’s not there in the browser 😄
v
process isn't a global in the browser. that is a node thing
everything in the browser is global by default, as in it lives on
window
. For instance, if you didn't use webpack and you just ran some arbitray javascript in the browser and did the following:
Copy code
var x = 4;
that would technically do
window.x = 4
in the browser (note: not in node). That's just how javascript works). webpack and stuff bundle your modules into scopes so that everything gets scoped to a function and there are no true globals. but you can still choose to inject globals on window if you want to with webpack, or you can set them manually from your code