better late than never, just updated https://github.com/nextjournal/advent-of-clerk for this season. 🎄
If I'm defining viewer functions in a separate cljs file to be used with :require-cljs, how do I add and load JS libraries in that file? What's the cljs equivalent of using [nextjournal.clerk.render/with-d3-require {:package ["pkg@version"]} ...]?
The example cljs viewer fns appear to use libraries like React that are already bundled with Clerk's front-end code - it's not clear to me how to require other ones.
you can't load external JS libraries (e.g. from CDN) in viewer functions yet
for this to work, viewer functions need to be async because fetching those libraries is async (which is something that is under consideration)
I see. So there’s no way to force loading with something like js-await?
I guess for the time being I can give https://github.com/mentat-collective/Clerk-Utils a try, which seems to have some utility functions to help with this
@afoltzm js-await doesn't work in SCI, but it does work in cherry https://snapshots.nextjournal.com/clerk/build/d9cab25aae62029557cff3346e7bd06e878999cd/index.html#/notebooks/cherry
but cherry doesn't work with :require-cljs yet
looking at the cherry example, I guess a viewer may return a promise? if that's true, then you could just use js/import in a SCI function as well
together with promesa (not sure if that's exposed, else just use JS interop)
I can take a look at this tomorrow to check if it works
Sorry it took me a while but js/import definitely works with SCI viewers. Here is a notebook:
(ns scratch
(:require [nextjournal.clerk :as clerk]
[clojure.core :as b]))
(clerk/with-viewer
{:render-fn
'(fn [_]
(nextjournal.clerk.render/render-promise
(->
(js/import "https:/esm.sh/date-fns@3.0.0/index.mjs")
(.then
(fn [lib]
(let [;; TODO: SCI bug, you can't put the local values in call position
formatRelative lib.formatRelative
subDays lib.subDays]
(nextjournal.clerk/html
[:pre (formatRelative (subDays (js/Date.) 2) (js/Date.))])))))))}
nil)
and here is what I see as an output:thank you!
Meanwhile, I also fixed the SCI bug so doing (lib.formatRelative ...) will also work in future clerk releases (where lib is a local)