Skip to content

Commit

Permalink
cljs ns read cache POC
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurC authored and ggeoffrey committed Dec 19, 2023
1 parent 89649d6 commit 7c17b77
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/hyperfiddle/electric/impl/expand.clj
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,22 @@
;; if ::lang/current = :clj expand with clj environment
;; if ::lang/current = :cljs expand with cljs environment

;; the ns cache relies on external eviction in shadow-cljs reload hook
(def !cljs-ns-cache (atom {}))

(defn enrich-for-require-macros-lookup [cljs-env nssym]
(if-some [src (cljs-ana/locate-src nssym)]
(let [ast (:ast (with-redefs [cljs-ana/missing-use-macro? (constantly nil)]
(binding [cljs-ana/*passes* []]
(cljs-ana/parse-ns src {:load-macros true, :restore false}))))]
;; we parsed the ns form without `ns-side-effects` because it triggers weird bugs
;; this means the macro nss from `:require-macros` might not be loaded
(run! serialized-require (-> ast :require-macros vals set))
(assoc cljs-env ::ns ast))
cljs-env))
(if-some [ast (get @!cljs-ns-cache nssym)]
(assoc cljs-env ::ns ast)
(if-some [src (cljs-ana/locate-src nssym)]
(let [ast (:ast (with-redefs [cljs-ana/missing-use-macro? (constantly nil)]
(binding [cljs-ana/*passes* []]
(cljs-ana/parse-ns src {:load-macros true, :restore false}))))]
;; we parsed the ns form without `ns-side-effects` because it triggers weird bugs
;; this means the macro nss from `:require-macros` might not be loaded
(run! serialized-require (-> ast :require-macros vals set))
(swap! !cljs-ns-cache assoc nssym ast)
(assoc cljs-env ::ns ast))
cljs-env)))

(tests "enrich of clj source file is noop"
(cljs.env/ensure (enrich-for-require-macros-lookup {:a 1} 'clojure.core)) := {:a 1})
Expand Down
4 changes: 3 additions & 1 deletion src/hyperfiddle/electric/shadow_cljs/hooks.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns hyperfiddle.electric.shadow-cljs.hooks
(:require [hyperfiddle.electric :as-alias e]
[hyperfiddle.electric.impl.lang :as lang]
[clojure.string :as str]))
[clojure.string :as str]
[hyperfiddle.electric.impl.expand :as expand]))

(let [!first-run? (volatile! true)] ; first run is noop
(defn reload-clj
Expand All @@ -18,6 +19,7 @@ double reloads (i.e. from :require-macros)."
(doseq [{ns-sym :ns, macro-requires :macro-requires} cljc-infos]
(when (and (not (get macro-requires ns-sym)) (-> ns-sym find-ns meta ::lang/has-edef?))
(prn ::reloading ns-sym)
(swap! expand/!cljs-ns-cache dissoc ns-sym)
(require ns-sym :reload))))))
build-state))

Expand Down

0 comments on commit 7c17b77

Please sign in to comment.