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?
Awesome, thank you very much @stubbs.ray and @thheller, I'll try that today!
Oh @thheller, I see you answered my question on SO as well :)
yep 🙂
Indeed, :keep-as-import works flawlessly, thank you all so much for the help!
How is it used from JS/TS in a way that doesn't make the corresponding bundler complain?
@p-himik Great question! I suspect, looking at a working TS example, it doesn't complain as it uses webpack's "externals" config option
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
Thanks, I'll try! I now realised that I can't make K6 pick up even a barebones script, gotta solve that first.
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.
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).
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
assuming that runtime can actually run ESM code that should work
I've been using shadow-cljs for probably 6 years now, and I still see options pop up that I have never seen before. :)
I always forget to add them to the docs 😉