Fork me on GitHub

Is it possible to write a macro that will emit code (or not emit code) based on the value of a goog-define ? I'm trying to figure out whether I can have a debug macro that basically disappears in production builds.


I'm trying to use :reader-features to conditionally execute some top-level code (basically #?(:my-feature (reset! my-atom :my-value))) at the start of a namespace in a library I'm including my build (in order to control how a few macros work), but this code doesn't seem to actually get executed at compile time. Is this intended, and if so, is there some other way to get build-specific compile-time configuration into a library?


@pmooser @wombawomba you can write macros that access the cljs.env/*compiler* atom. that has an :options key which is :compiler-options from the build config. so you can set stuff there and do conditional stuff in the macro


@wombawomba yes, as the :reader-features name implies it happens at read time. so emitting them in a macro is not possible


Got it, thanks!


To clarify, though, I'm not trying to use reader conditionals to emit anything in a macro — rather I'm trying to conditionally set some state before defining my macros.


...the problem with my approach, though, is that the code is getting loaded in clojure, so only :clj reader conditionals end up getting evaluated.


Actually, I think the easiest way to handle this for me would be to use Java properties (I'm already using these to do something similar when building the same library via GraalVM).


that also works but be aware that caching will not recognize these


so you must disable caching for the namespace using this


Good to know.


:compiler-options will invalidate so it sort of works with caching


So I tried this out (I'm using shadow-cljs with leiningen, so I just set :jvm-opts in my project.clj) and it seems to work — just one problem: I only want this behavior sometimes. Is is possible to control whether the property is set or not from the command line?


(It seems I can set a leiningen profile in my shadow-cljs.edn, but I can't see how to control which profile gets applied when invoking shadow-cljs)


To answer my own question: I was able to make it work by setting JAVA_OPTS. (It initially seemed like it wasn't being picked up by leiningen, but that was because I'd made a mistake).