This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-08-15
Channels
- # architecture (5)
- # babashka (34)
- # beginners (72)
- # calva (42)
- # cherry (31)
- # cider (14)
- # clojure (27)
- # clojure-europe (11)
- # clojure-norway (17)
- # clojure-uk (1)
- # clojurescript (25)
- # community-development (13)
- # conjure (1)
- # core-async (11)
- # datascript (18)
- # datomic (11)
- # emacs (12)
- # fulcro (10)
- # integrant (5)
- # introduce-yourself (3)
- # jobs (8)
- # juxt (2)
- # malli (22)
- # off-topic (11)
- # pathom (18)
- # polylith (62)
- # rdf (18)
- # reagent (8)
- # releases (1)
- # shadow-cljs (35)
- # sql (3)
- # squint (141)
- # tools-deps (12)
- # vim (4)
- # xtdb (4)
How do conditional requires work? I've got a cljc file where some of the :requires are only used in node. I don't want to require them at all in the browser. Right now I've got #?@(:cljs [["fs" :as fs] ["path" :as path]])
but that blows up when I try to build for the browser.
I've also tried using :closure-defines
as a conditional and doing the require dynamically, but that doesn't work for either build.
#?(:cljs
(when-not platform/BROWSER
(require '["fs" :as fs])
(require '["path" :as path])))
ha, I'm learning that! How is this problem generally solved, then? Is building for the browser and for node in the same project not supported?
There is actually such feature in shadow-cljs: https://shadow-cljs.github.io/docs/UsersGuide.html#_conditional_reading
if you depend on some platform specific library, then what I typically do is split them into different namespaces
so you might have my-lib.core
and my-lib.node
, where my-lib.node
has the nodejs-specific code
if you can push the I/O to the edges of your library, then my-lib.node
can be quite small. you might realize you don't even need it
Well, I suppose the whole namespace only makes sense on the jvm and node - it's writing the the filesystem. I guess I'll try putting the call site behind a closure-defines conditional and see if dead code elimination will remove it from the browser build
It's just one corner of functionality of the overall package - offering a state-persistence mechanism. The rest of the library works just fine if you don't choose to use it that way.
Another strategy is to have some source paths be different for different builds. E.g., for node: {"src/main" "src/node"]
Then you'd refer to "my-lib" and have it use either node/my-lib.cljs or some other implementation depending on the build paths.
I actually find that approach pretty compelling. Though, if I'm targeting specific files within the ns hierarchy the directory structure will look a bit funny. But that could be ok
In this https://github.com/sauvala/cljs-vscode-extension/blob/main/src/cljs_vscode_extension/main.cljs of a HelloWorld for building a VS Code extension from ClojureScript, the author uses what looks to be a type hint for a vscode.ExtensionContext
instance.
(defn activate [^js/vscode.ExtensionContext context]
(.log js/console "Congratulations, cljs-vscode-extension is now active!")
Is that actually what this does? If so, how might I discover what other types are hintable beyond js
, clj
, and basic primitives?From the compiler perspective, that type hint is equivalent to ^js
. Apart from that, it's just documentation.
Thanks. That was my first guess (right after the brief moment when I took it at face value as a hint). Are fake hints like that common, popular, suggested? Can't quite decide whether it strikes me as a good idea.
Thanks. Maybe I am not reading it correctly, but it does seem like the /Foo.Bar
actually is significant there?
The externs file might differ but I'm 90% certain that the end result of the compilation will be exactly the same - even if you mark the object on which you call .x
with different types in different locations.
I guess specifying the class here is actually fairly meaningful documentation, as it explains why we need externs. If replacing a JS dep with a CLJ dep, you could use these hints to track down where externing is no longer needed. In this case it tells you clearly that this is provided by the VS Code environment and so will always need to be externed, while also implying that you can find documentation on Microsoft's website, by searching for "extensioncontext".