Fork me on GitHub
#shadow-cljs
<
2021-03-14
>
dvingo19:03:48

I have a use case where I want to ^:export some variables for JS consumption - the build in shadow is :npm-module. To clean things up I wanted to do export a cljs var within a macro, but I'm struggling to find a way to do so - attaching the metadata to the resulting cljs var doesn't get shadow to include the var in the module.exports. I can goog.exportSymbol manually in the macro code, but I do I also need the JS var in the module.exports object. Is this a known limitation or is there a way to get this working?

thheller20:03:02

@danvingo what did you do in the macro? shadow collects the data from the analyzer meta data so if you did it correct it should just work

dvingo21:03:18

the background context is I'm trying to create some react storybook stories - which in the newer versions are just functions that are exported. Here's the code:

(defmacro make-story2 [nm body]
  `(defn ^:export ~nm []
     (reagent.core/as-element ~body)))
I've also tried:
(defmacro make-story2 [nm body]
  `(do
     (defn ~nm []
       (reagent.core/as-element ~body))
     (alter-meta! (var ~nm) assoc :export true)))

dvingo21:03:25

ahhhh I found the magic incantation 🙂

(defmacro make-story2 [nm body]
  `(defn ~nm
     {:export true} []
     (reagent.core/as-element ~body)))

dvingo21:03:54

hmm, so this works for defn, but not for def I guess I can refactor to use defn

thheller21:03:48

^:export ~nm [] isn't valud

thheller21:03:17

~(vary-meta nm assoc :export true) would do it

thheller21:03:59

(defmacro make-story2 [nm body]
  `(defn ~(vary-meta nm assoc :export true) []
     (reagent.core/as-element ~body)))

dvingo21:03:47

I figured the reader macro wouldn't work - ahh very cool!

dvingo21:03:56

thanks for the help