clojurescript

true.neutral 2024-11-04T11:50:37.122429Z

Hey all! I apologize in advance since I don't have any experience with JS/CLJS and may say dumb things. So there's a tool from Grafana K6 (written in Golang) that runs a JavaScript load test definition on top of a bundled JS runtime with extra goodies. I want to use CLJS instead of JS/TS. As soon as I specify (:require ["k6" :as k6]), clojure compiler - understandably - refuses to compile anything, since the package will only be available at runtime. Hence the question - is there any way to work around this?

true.neutral 2024-11-05T10:36:47.650059Z

Awesome, thank you very much @stubbs.ray and @thheller, I'll try that today!

true.neutral 2024-11-05T10:37:57.358979Z

Oh @thheller, I see you answered my question on SO as well :)

thheller 2024-11-05T10:43:08.339539Z

yep 🙂

true.neutral 2024-11-05T11:14:06.158099Z

Indeed, :keep-as-import works flawlessly, thank you all so much for the help!

👍 1
p-himik 2024-11-04T11:52:44.873459Z

How is it used from JS/TS in a way that doesn't make the corresponding bundler complain?

true.neutral 2024-11-04T12:30:26.616809Z

@p-himik Great question! I suspect, looking at a working TS example, it doesn't complain as it uses webpack's "externals" config option

p-himik 2024-11-04T12:45:07.443239Z

If what's loaded is an ESM module, you can also load it dynamically from CLJS, at least with shadow-cljs: https://shadow-cljs.github.io/docs/UsersGuide.html#_dynamic_module_import

true.neutral 2024-11-04T12:48:53.928589Z

Thanks, I'll try! I now realised that I can't make K6 pick up even a barebones script, gotta solve that first.

true.neutral 2024-11-04T13:20:13.716809Z

Got the barebones script working with :esm target, but K6 fails to run the dynamic import version with "import()" marked as "unexpected token" - guess it's somehow not available.

Ray Stubbs 2024-11-04T15:31:23.369859Z

If compiling to ESM, you can set :js-provider to :import to have the compiler output raw import ... syntax. Or to :require for CommonJS, to have it emit require(...) calls. But this controls all js requires, you can't tell it to do this for specific modules. Other alternatives might be things like (js* "require('foo')") (CommonJS) or (js* "import("...")) (ESM dynamic import). Or (js/require "...") (CommonJS raw require, not sure if this will work).

👍 1
❤️ 1
thheller 2024-11-04T16:42:13.629719Z

for shadow-cljs when using :target :esm you may set :js-options {:keep-as-import #{"k6"}}. that will make shadow-cljs bundle all other dependencies except k6

🔥 3
❤️ 1
thheller 2024-11-04T16:43:46.748099Z

assuming that runtime can actually run ESM code that should work

p-himik 2024-11-04T16:45:26.240539Z

I've been using shadow-cljs for probably 6 years now, and I still see options pop up that I have never seen before. :)

thheller 2024-11-04T16:58:03.913789Z

I always forget to add them to the docs 😉