Fork me on GitHub
#shadow-cljs
<
2024-03-11
>
cmdrdats13:03:05

I'm trying to get a macro to compile differently based on the environment (dev vs prod) it's being built as, and I'm not quite sure how I should go about that - currently I'm got a config namespace with ENVIRONMENT that I override in the closure-defines in shadow-cljs.edn, but the config namespace for the clojure side of things (obviously) doesn't get set at compile time. For example, the current code evaluates to:

(defmacro environment []
  `{:local config/ENVIRONMENT :compile ~config/ENVIRONMENT})
{:local "dev" :compile :dev} because in the config.clj I have (def ENVIRONMENT :dev) and in config.cljs I have (goog-define ENVIRONMENT "dev") (this is needed because of some fickle api policy code that needs to inline for performance reasons at build time, but breaks because of being a macro at dev time during hot reloads) I need to be able to have the above macro yield: {:local "dev" :compile "dev"} at dev time time and {:local "prod" :compile "prod"} when I'm building a release version.. any pointers?

cmdrdats14:03:44

I'm working around this for now by using export ENV=dev/prod in the build scripts, #shadow/env "ENV" in the shadow-cljs.edn, and (*def* ENVIRONMENT (System/getenv "ENV")) in the config.clj.. would have preferred being able to access the build environment of shadow-cljs (being able to distinguish between watch vs release at compile time would be neat)

thheller15:03:45

(shadow.build/mode &env) in a macro. will be :dev for watch/compile and :release for release builds

cmdrdats16:03:16

Amazing, that's exactly what I'm looking for, thanks 🙏