Fork me on GitHub
#clojurescript
<
2022-12-25
>
Sam Ritchie18:12:13

is there any way to get an export like this to work?

((fn []
   (defn ^:export square [x]
     (* x x))))
or do :export ed vars always have to be top level?

skylize20:12:47

From the JS side of things, you have special syntax for a default export.

// exporting same function two ways
export const foo = x => x
export default foo
// now either of these is valid to get at the same function
import { foo } from "./foo"
import alsoFoo from "./foo"
For ES5 transpiling, that default just gets turned into a .default property on the module. So I strongly suspect you can just export under the symbol default from Cljs to get the same results for your JavaScript consumers? And based on the https://clojure.atlassian.net/browse/CLJS-3235, I think you should also be able to get at that default from ClojureScript by appending $default to the source location.
(require '["./foo$default" :as also-foo :refer [foo])

Sam Ritchie20:12:43

ah, that’s great info, in this case I wasn’t trying necessarily to do a default export

Sam Ritchie20:12:05

I had a macro that was bundling everything into a fn executed when some other code was ready;

Sam Ritchie20:12:22

this was causing defn forms to run as side effects, but when they ran inside the (fn [] …) the export wasn’t working

skylize21:12:05

Oh yeah. I see. My answer was good-to-know, but totally missed the point of your question.

Sam Ritchie21:12:01

it was definitely good to know!

thheller06:12:44

yeah, all exports have to be top level vars, but how you create them doesn't matter

thheller06:12:15

so (def ^:export foo (...)) is fine

👍 1