sci

arohner 2025-05-23T20:45:26.745629Z

Does scittle support requiring with module syntax? i.e (ns foo (:require "/js/foo.js" :refer [Foo]))

borkdude 2025-05-23T21:04:36.824669Z

@arohner next time, #scittle is the right channel for this, but I'll answer your question here. scittle doesn't let you load javascript files (as of now). I think to load an ESM JS file it's best to load it in a type="module" script tag, then globally define the module into a javascript var and then use the javascript var from the scittle code. It's not as convenient but this is the only thing that works currently I think

arohner 2025-05-23T21:05:26.095949Z

Sorry, I was in #sci and just assumed #scittle didn’t exist

borkdude 2025-05-23T21:05:53.095129Z

nbb does a better job with loading ESM code. to make scittle do the same, I'll have to move to async SCI to load ESM modules, e.g. with (require ["https://..."])

borkdude 2025-05-23T21:06:12.394209Z

no worries. you're in London right? are you coming over to reClojure on Monday?

arohner 2025-05-23T21:06:46.541339Z

I used to be in London. Moved back home to Austin, TX last August

borkdude 2025-05-23T21:06:52.941469Z

I see

borkdude 2025-05-23T21:14:14.205349Z

you could do something like this:

<script type="module">
  import { v4 as uuidv4 } from '';
  window.uuid = { v4: uuidv4 };
</script>
<script type="application/x-scittle">
(ns uuid.core)

(defn v4 [] (js/uuid.v4))
</script>
<script type="application/x-scittle">
(ns my-app
  (:require [uuid.core :as uuid]))

(js/setTimeout
  (fn []
    (js/console.log (uuid/v4)))
  1000)
</script>

👍 1
arohner 2025-05-23T21:16:07.868259Z

multiple x-scittle tags can race, right? because each calls XMLHttpRequest, and there’s no ordering guarantee?

borkdude 2025-05-23T21:16:25.416309Z

they are executed in order of the page

borkdude 2025-05-23T21:16:55.168679Z

but type=module is probably executed async so that one can race which complicates things a little bit

borkdude 2025-05-23T21:17:30.088489Z

there is a ordering guarantee of x-scittle script blocks though

arohner 2025-05-23T21:18:21.865309Z

(defn- eval-script-tags* [script-tags]
  (when-let [tag (first script-tags)]
    (if-let [src (.getAttribute tag "src")]
      (let [req (js/XMLHttpRequest.)
            _ (.open req "GET" src true)
            _ (gobject/set req "onload"
                           (fn [] (this-as this
                                    (let [response (gobject/get this "response")]
                                      (gobject/set tag "scittle_id" src)
                                      ;; save source for error messages
                                      (store/swap-ctx! assoc-in [:src src] response)
                                      (sci/binding [sci/file src]
                                        (eval-string response)))
                                    (eval-script-tags* (rest script-tags)))))]
I believe that is guaranteed to start in order. How is there a guarantee that they eval in order?

arohner 2025-05-23T21:18:35.521519Z

because the network/browser controls when the response comes back?

borkdude 2025-05-23T21:19:55.114239Z

because eval-script-tags* is called on the rest of the script tags only after the first one executes

borkdude 2025-05-23T21:20:34.214719Z

so the recursion is done as part of the request callback

arohner 2025-05-23T21:22:44.739549Z

aha, thanks

borkdude 2025-05-23T21:25:20.275479Z

I guess you could kick off eval-script tags manually in the async modue script tag

borkdude 2025-05-23T21:25:39.687159Z

to ensure that's loaded

borkdude 2025-08-21T13:08:36.421899Z

Hey @arohner - now there's finally a sane way of dealing with JS libs in scittle: https://github.com/babashka/scittle/blob/main/doc/js-libraries.md

arohner 2025-08-21T13:46:22.025709Z

Awesome, thank you!