Fork me on GitHub
#cljs-dev
<
2022-02-24
>
martinklepsch19:02:22

What’s the “recommended way” for dynamic requires based on e.g. targeting node/browser? I know that shadow has embraced reader conditionals for this comes with other issues and I’m curious if there’s another answer to this question that would be worth exploring… E.g. is there something funky you could do with .js Closure modules to conditionally load code between node/browser? And then just use if when you want to switch on which environment you’re on? Is this something that is considered “in scope” for the ClojureScript compiler itself? What other tools should I look at to solve this kind of problem?

thheller06:02:22

"embraced" is a overstating it. I offered it as an option. Another options is :build-options {:ns-aliases {foo.bar foo.bar-node}}. That would make the build using this config use the foo.bar-node ns whenever foo.bar is required anywhere. so no change required in the code but still able to replace entire namespaces

thheller06:02:55

but I believe I recommended this approach before and this is what I still recommend doing https://gist.github.com/thheller/fa044450a6470fdd1afdbcf3f37a65e3

borkdude19:02:07

@martinklepsch Perhaps make two namespace for browser and node that define something in the JS context that you can then use in other namespaces

martinklepsch19:02:14

Right but how would I decide which one to load? I guess I’d need different source paths for node/browser?

borkdude19:02:37

For example yes

thheller19:02:04

(ns test.a
  (:require [test.b :refer (+)]))

(+ "a" "b")

thheller19:02:16

(ns test.b
  (:refer-clojure :exclude [+]))

(defn + [a b]
  :yo)

thheller19:02:27

$ clj -M -m cljs.main -c test.a
WARNING: cljs.core/+, all arguments must be numbers, got [string string] instead at line 4 /mnt/c/Users/thheller/code/tmp/cljs-plus/src/test/a.cljs

thheller19:02:03

that seems incorrect? the :refer (+) seems to be ignored? not sure why (tested with 1.11.4)

thheller19:02:18

jira search fails me but this appears to be an issue with any core macro. cljs.analyzer/get-expander apparently ignoring :uses?