shadow-cljs

wilkerlucio 2025-10-27T20:38:42.189239Z

question, we are trying ot use (js/import ...) to dynamically load some code, but it fails claiming import$ isn't defined. when we try to use the import from the chrome console it works fine. why is shadow renaming the call to import$? and how we can use the import call from clojurescript in this way?

thheller 2025-10-28T08:23:15.078039Z

shadow_esm_import is defined for :target :esm builds. what are you using this with?

thheller 2025-10-28T08:24:29.928109Z

import gets munged by the cljs compiler to import$. technically not necessary, but not easy to change from shadow-cljs, so not its fault. 😉

thheller 2025-10-28T08:25:49.705639Z

nah wait it actually needs to be munged, otherwise the closure compiler will see it and try to compile it away

thheller 2025-10-28T08:26:06.422369Z

thats why the shadow.esm/dynamic-import thing exists in the first place

thheller 2025-10-28T08:26:55.951319Z

you can also get this to work in :target :browser but just making the :prepend "function shadow_esm_import(x) { return import(x); };", same as you already have basically

thheller 2025-10-28T08:27:24.203309Z

its 2025 so I recommend using :target :esm 😉

valerauko 2025-10-28T10:05:29.827289Z

note that static analyzers like storybook will still trip over that since they're looking for literal import calls and ignore the wrapped function

wilkerlucio 2025-10-28T14:25:27.403519Z

thanks for the clarification, I tried :esm but things stopped working, so I'll keep the hacky way for now. @vale that's fine for our case, we are building a platform and wanna have external code developed by different teams to load, the whole environment is in our control, so I think it should be fine for the time being (at this stage is mostly a POC, we may get back to this if the idea moves forward)

valerauko 2025-10-28T14:30:33.760169Z

what things stopped working? i haven't had issues using :esm

wilkerlucio 2025-10-28T14:31:11.978319Z

I haven't tried to debug, but once change, nothing was loading at all, so we just reverted right away

valerauko 2025-10-28T14:34:56.670699Z

might still be "cheaper" to look into that than struggling with the alternatives

thheller 2025-10-28T17:49:50.777179Z

note that :esm also has (:require ["esm:/path/to/other.js" :as foo])) meaning it can require esm normally (and doesn't need dynamic import), just in case you are always loading the file anyway

thheller 2025-10-28T17:50:29.814219Z

I should really write an updated code splitting guide for esm as well. generally :esm is much better than :browser pretty much always 😛

👍 1
👀 1
wilkerlucio 2025-10-27T20:41:33.072959Z

we also tried the way described here: https://shadow-cljs.github.io/docs/UsersGuide.html#_dynamic_module_import but we get a different error:

Uncaught ReferenceError: shadow_esm_import is not defined

wilkerlucio 2025-10-27T20:58:04.314249Z

we were able to make it work via little dirty hack, in our HTML file we created something like:

function myImport(url) { return import(url); }
and this handles our needs just fine by calling (js/myImport). but I'm still puzzled on why shadow doesn't let me call js/import directly 🤔