From 7aebb5bbb080c6cfe3624ac9a81be363753b6a3e Mon Sep 17 00:00:00 2001 From: Chris McCormick Date: Thu, 21 Aug 2025 17:08:56 +0800 Subject: [PATCH] Support string requires of `globalThis` js deps #95 (#129) * Add a :load-fn for js libs on globalThis. Fixes #95. * Update changelog. --- CHANGELOG.md | 2 ++ src/scittle/core.cljs | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcc6994..9bbe01f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ +- [#95](https://github.com/babashka/scittle/issues/121): support string requires of `globalThis` js deps ([@chr15m](https://github.com/chr15m)) + ## v0.7.26 (2025-08-20) - [#121](https://github.com/babashka/scittle/issues/121): add `cjohansen/dataspex` plugin ([@jeroenvandijk](https://github.com/jeroenvandijk)) diff --git a/src/scittle/core.cljs b/src/scittle/core.cljs index 6ba88ad..6a7ac60 100644 --- a/src/scittle/core.cljs +++ b/src/scittle/core.cljs @@ -53,13 +53,19 @@ 'sci.core {'stacktrace sci/stacktrace 'format-stacktrace sci/format-stacktrace}}) +(defn load-fn [{:keys [ctx] :as opts}] + (when-let [lib (and (string? (:namespace opts)) + (gobject/get js/globalThis (:namespace opts)))] + (sci/add-js-lib! ctx (:namespace opts) lib))) + (store/reset-ctx! (sci/init {:namespaces namespaces :classes {'js js/globalThis :allow :all 'Math js/Math} :ns-aliases {'clojure.pprint 'cljs.pprint} - :features #{:scittle :cljs}})) + :features #{:scittle :cljs} + :load-fn load-fn})) (unchecked-set js/globalThis "import" (js/eval "(x) => import(x)"))