Fork me on GitHub
#shadow-cljs
<
2022-03-15
>
pmooser10:03:49

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.

wombawomba16:03:46

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?

thheller16:03:38

@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

thheller16:03:02

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

wombawomba16:03:33

Got it, thanks!

wombawomba16:03:36

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.

wombawomba16:03:58

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

wombawomba16:03:36

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).

thheller16:03:29

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

thheller16:03:51

so you must disable caching for the namespace using this

wombawomba16:03:56

Good to know.

thheller17:03:12

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

wombawomba17:03:01

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?

wombawomba17:03:31

(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)

wombawomba17:03:24

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).