This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-07-21
Channels
- # babashka (5)
- # beginners (62)
- # biff (22)
- # calva (22)
- # cider (53)
- # clojure (26)
- # clojure-europe (21)
- # clojure-gamedev (32)
- # clojure-norway (2)
- # clojurescript (22)
- # conjure (1)
- # cursive (2)
- # data-science (14)
- # datomic (22)
- # emacs (6)
- # fulcro (1)
- # ghostwheel (2)
- # gratitude (4)
- # hugsql (15)
- # hyperfiddle (26)
- # lsp (1)
- # melbourne (1)
- # off-topic (19)
- # pathom (5)
- # pedestal (4)
- # practicalli (2)
- # rdf (24)
- # releases (2)
- # shadow-cljs (33)
- # squint (31)
- # tools-deps (24)
- # xtdb (9)
Hi All, I'm having some trouble with dynamically loading modules when using the :esm target. It seems that the shadow.lazy namespace is not intended to be used with :esm, so I'm trying to use shadow.esm/dynamic-import
. I can load my module if I give a specific URL, but I'd prefer to just give a symbol as we do with shadow.lazy/loadable
. Is that possible?
not with shadow.lazy/loadable as of now since that assumes shadow.loader will do the actual loading
but I guess you could take the existing shadow/lazy.clj+cljs and modify it to use esm import instead
this would be the part that needs chaning I guess https://github.com/thheller/shadow-cljs/blob/master/src/main/shadow/lazy.cljs#L31-L65
Related to the above, is shadow.esm/dynamic-import
only intended for use with the :esm target?
correct, in can in theory work elsewhere but is not currently setup to do so. basically it only exists to work around an issue where the closure compiler yells at us for using js/import
directly in code
Is it possible to import es modules when using :browser target, or would that require code changes/a separate namespace from shadow.esm?
hahaha yeah, just checked. Thanks, I'll just do something similar to what you're doing in my own code.
the issue is that the closure compiler doesnt like dynamic import
in the code and will fail
so, we call shadow_esm_import
instead of import
, and then prepend window.shadow_esm_import = function(x) { return import(x); };
to the code AFTER closure the closure compiler is done
and currently this prepend is only done for :target :esm
, so other targets just get a var not found error
Seems like it would be possible for me to give you a PR that adds that code when shadow.esm is used anywhere in the project, right? Then dynamic-import could support other build targets.
again, this is a hack that I hope becomes unnecessary. I don't really want to "spread" it further
in the build config :modules {:main {:prepend "window.shadow_esm_import = function(x) { return import(x); };" :init-fn ...}}
then you can use shadow.esm/dynamic-import
just fine. assuming you are still in a browser of course, otherwise you might need to adjust the snippet
That's perfect, thanks. Would you take a PR for the user guide describing this stuff in a bit more detail so that when I revisit this code in 6 months I have a place to look to understand what's happening?
I’m trying to include a different source file (for build-specific vars that toggle app behavior) for each of my builds. I can’t specific :source-paths
inside the build. Is there a recommended alternative approach?
I put something together with a pre-compilation build hook that reads the build id and spits a file into the src for the given build
I consider this a horrific anti-pattern and it is therefore not supported out of the box
in general the shadow-cljs builds ONLY include what they required, so if you have different builds each build would get its own entry namespace. that namespace you can use to setup build specific things since the others won't include it