Welcome! Please see the About page for a little more info on how this works.

+1 vote
in ClojureScript by

I'm writing library that targets both Java and JS and have problem with the tests. Ideally I'd like to write one test file conforming to the lowest common denominator because the library API and the behavior is designed to be target agnostic.

Did some helper functions and macros for the testing and can't find a way to make the macros resolve the async library for compiling to cljs. The only thing that comes to mind is to have the macros twice using the proper fully-qualified function name but that's very brutish and I'd expect there to be better way.

1 Answer

+1 vote
by

Technique from clara-rules project:
https://github.com/cerner/clara-rules/blob/main/src/main/clojure/clara/rules/compiler.clj#L88

(ns my.macro) ;; .clj file

(defn compiling-cljs? []
  (boolean
    (when-let [n (find-ns 'cljs.analyzer)]
      (when-let [v (ns-resolve n '*cljs-file*)]
        @v))))

(defmacro my-macro [& body]
  (if (compiling-cljs?)
    (cljs.async/bla)
    (clojure.async/bla)))

;; in cljc call: (my-macro ...)
by
Thanks. I ended up using macros and checking for cljs via (some? (:ns &env)). Not sure which check is more reliable.
by
@alexmiller Consider making a ticket for a feature "reader conditionals that can be used inside macros" as there are a number of hacks used in the wild - most common `(:ns &env)` check, probably because this thread: https://groups.google.com/g/clojurescript/c/iBY5HaQda4A ;
...