Fork me on GitHub
#shadow-cljs
<
2021-01-03
>
Vincent Cantin09:01:05

Using Shadow-cljs, is there a way to run a hook inside the context of the reloaded cljc source files? From my test on the hooks, it seems that the source code is only loaded once.

thheller10:01:01

scanning for generic things like that is difficult but build hooks receive the entire build state and can inspect it however they need

Vincent Cantin10:01:44

I took a look at the build-state and made the mistake of printing its content 😛

thheller10:01:56

the best option I have found so far however is using a dedicated css macro or so that collects these things during compilation. much easier to extract but slightly more verbose in the code

(def button-style (css :border :p-2 :shadow :lg/p-4))
or so

thheller10:01:14

(tap> build-state) and look at it in the inspect UI

Vincent Cantin10:01:31

oh, good idea (for the tap>)

thheller10:01:55

:sources has all the sources

thheller10:01:29

:output has all the outputs of those sources (in :flush) stage, otherwise might still be empty

Vincent Cantin10:01:50

I made a macro which adds the css in the metadata attached to the clojure var already. I am looking at how to read it from the hook.

thheller10:01:54

it stores state in the analyzer data for the namespace

thheller10:01:23

you could read that out later in the build hook via [:compiler-env :cljs.analyzer/namespaces your.ns :your.thing/key] or so

Vincent Cantin10:01:21

Thx, I am taking a look now.

Vincent Cantin12:01:25

@U05224H0W this worked !

(tap> (get-in build-state
                [:compiler-env
                 :cljs.analyzer/namespaces
                 '
                 :defs
                 'my-fn
                 :css]))
Big thank you !

thheller12:01:15

when you add custom keys to the analyzer data make sure they are namespaced

thheller12:01:26

otherwise you might at some point clash with implementation details

👌 3