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

Technique from clara-rules project:

(ns my.macro) ;; .clj file

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

(defmacro my-macro [& body]
  (if (compiling-cljs?)

;; in cljc call: (my-macro ...)
Thanks. I ended up using macros and checking for cljs via (some? (:ns &env)). Not sure which check is more reliable.
@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 ;