nbb 2024-11-16

I'm am building a script with nbb that will go up on npm. I'm following the example at https://github.com/babashka/nbb/tree/main/doc/publish and one thing I have run into is my .cljs script uses *command-line-args* but this is not available when I do loadFile from a .mjs script. Is this a bug or expected? I guess I should pass process.argv to my main function myself instead? Also, is there a way to export a function? Doing ^:export didn't seem to expose the function when I loaded the cljs with loadFile. 🤔

• command line args: perhaps a bug, feel free to post an issue • export: due to this being interpreted, export doesn't work. you can "export" stuff like you do it with scittle, just attach the function on the global object

about command line args: you could still just get the js/process.argv

Ok great, thank you. I assume that's js/globalThis in nbb. I'll experiment with it.

By the way I noticed this, not sure if it's a bug:

Welcome to nbb v1.3.195!
user=> js/globalThis
"RangeError: Maximum call stack size exceeded"

That's not a bug, it's the same as when you print this in CLJS, it's a circularity issue because some things link back to the global object

Ah ha cool

Filed the command line args thing with a minimal repro: https://github.com/babashka/nbb/issues/372

👍 1

Oh hi! I recently tried out nbb again and 👌 What I'm having an issue with today, and I've been stuck for a long long time, is to do multiple esm imports and get the default export... I don't know why someone would put data structures in ts (compiled to .js) 😭 Anyhow, I've been scratching around nbb.core code to find how that may be done, and I'm succeeding with "simple" imports. But what I'm struggling to get right, is to pair up multiple :key and file location items and "hydrate" the files. I'm trying to do this:

[[:k1 "loc1"] [:k2 "loc2"] ,,,]
=> [[:k1 #js {}] [:k2 #js {}] ,,,]
I just can't figure out how to do it correctly. Sample code to follow.

I need to add that I'm not well versed in promesa at all, and also spent a fair amount of time trying to understand how it works, so I do blame my ignorance for not succeeding 😅

yeah this won't work:

(p/map
      (fn [[k v]] [k (js/import v)])
you're destructuring a vector with a k value and a promise, so p/map will just return that. you need to wrap the entire thing in a promise for p/map to make sense

so:

(fn [[k v] (p/let [v (js/import v)] [k v])

❤️ 1

That works! Thank you kindly, @borkdude

Interestingly, the p/map-version still locks up 🤔 The last one works with the suggested change.

bb is a JS promise, but running @bb won't work, js Promises aren't deref-able

I haven't read the docstring of p/map yet, but this was just a guess ;)

oh I guess p/map is indented to be called on one promise that returns a seq

anyway, you got it working I guess

Yes, I got it working for my needs, it's beautiful. Thanks again! I need to up-skill on promesa a bit more when I get the chance!