shadow-cljs

https://github.com/thheller/shadow-cljs | https://github.com/sponsors/thheller | https://www.patreon.com/thheller
2021-03-14T19:24:48.438300Z

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?

thheller 2021-03-14T20:01:02.439Z

@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

2021-03-14T21:33:18.441200Z

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

2021-03-14T21:41:25.441700Z

ahhhh I found the magic incantation 🙂

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

2021-03-14T21:43:54.442700Z

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

thheller 2021-03-14T21:53:48.443Z

^:export ~nm [] isn't valud

thheller 2021-03-14T21:54:17.443600Z

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

thheller 2021-03-14T21:54:59.443800Z

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

2021-03-14T21:58:47.444900Z

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

2021-03-14T21:58:56.445200Z

thanks for the help