nbb

Chris McCormick 2024-11-16T09:00:07.855619Z

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. 🤔

borkdude 2024-11-16T09:19:53.974199Z

• 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

borkdude 2024-11-16T09:20:35.042089Z

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

Chris McCormick 2024-11-16T09:30:40.102239Z

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

Chris McCormick 2024-11-16T09:31:51.899739Z

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"

borkdude 2024-11-16T09:32:40.853139Z

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

Chris McCormick 2024-11-16T09:33:03.807239Z

Ah ha cool

Chris McCormick 2024-11-16T10:13:53.504679Z

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

👍 1
johanmynhardt 2024-11-16T17:35:06.241589Z

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.

johanmynhardt 2024-11-16T17:38:39.373189Z

johanmynhardt 2024-11-16T17:44:56.242559Z

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 😅

borkdude 2024-11-16T18:52:16.483859Z

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

borkdude 2024-11-16T18:52:44.562539Z

so:

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

❤️ 1
johanmynhardt 2024-11-16T19:43:16.674469Z

That works! Thank you kindly, @borkdude

johanmynhardt 2024-11-16T19:47:10.843629Z

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

borkdude 2024-11-16T19:57:12.162909Z

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

borkdude 2024-11-16T19:58:16.116249Z

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

borkdude 2024-11-16T19:59:31.822359Z

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

borkdude 2024-11-16T19:59:55.061379Z

anyway, you got it working I guess

johanmynhardt 2024-11-16T21:22:18.939249Z

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!