Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Enable refering goog namespaces #445

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/cljs/snapshot/lumo/js_deps.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
(defn add-js-lib
"Adds a js lib to the index."
[index {:keys [provides] :as js-lib}]
(reduce (fn [index provided-lib]
(assoc index (symbol provided-lib) js-lib))
index
provides))
(into index
(mapcat (fn [provided-lib]
[[(symbol provided-lib) js-lib]
[provided-lib js-lib]]))
provides))

(defn add-js-libs
"Adds multiple js libs to the index."
Expand Down
124 changes: 72 additions & 52 deletions src/cljs/snapshot/lumo/repl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[cljs.tagged-literals :as tags]
[cljs.tools.reader :as r]
[cljs.tools.reader.reader-types :as rt]
[clojure.set :as set]
[clojure.string :as string]
[cljs.spec.alpha :as spec]
[cognitect.transit :as transit]
Expand Down Expand Up @@ -88,12 +89,20 @@
(map second
(re-seq #"'(.*?)'" requires))])
(re-seq #"\ngoog\.addDependency\('(.*)', \[(.*?)\], \[(.*?)\].*"
(js/$$LUMO_GLOBALS.load "goog/deps.js")))]
(into {}
(for [[path provides requires] paths-to-deps
provide provides]
[(symbol provide) {:path (str "goog/" (second (re-find #"(.*)\.js$" path)))
:requires requires}]))))
(js/$$LUMO_GLOBALS.load "goog/deps.js")))
index (into {}
cat
(for [[path provides requires] paths-to-deps
provide provides]
(let [info {:path (str "goog/" (second (re-find #"(.*)\.js$" path)))
:requires requires
:group :goog}]

[[provide info]
[(symbol provide) info]])))]
;; this is fine because we only use the memoized version
(swap! st update-in [:js-dependency-index] merge index)
index))

(def ^:private closure-index (memoize closure-index*))

Expand All @@ -103,16 +112,18 @@
goog.string.StringBuffer
goog.array
goog.crypt.base64
goog.math.Long}))

(defn- goog-dep-source [name]
(let [index (closure-index)]
(when-let [{:keys [path]} (get index name)]
(let [sorted-deps (remove @goog-loaded (deps/topo-sort index name))]
(vswap! goog-loaded into sorted-deps)
goog.math.Long
goog.Uri}))

(defn- goog-dep-source [dep-name]
(let [index (closure-index)
name (name dep-name)]
(when-let [{:keys [path] :as f} (get index name)]
(let [sorted-deps (remove (comp @goog-loaded symbol) (deps/topo-sort index name))]
(vswap! goog-loaded into (map symbol) sorted-deps)
(reduce str
(map (fn [dep-name]
(let [{:keys [path]} (get index dep-name)]
(let [{:keys [path]} (get index (cljs.core/name dep-name))]
(js/$$LUMO_GLOBALS.load (str path JS_EXT)))) sorted-deps))))))

(defn- load-goog
Expand Down Expand Up @@ -182,24 +193,20 @@
name)
(or
(contains?
'#{goog
goog.object
goog.string
goog.string.StringBuffer
goog.array
goog.crypt.base64
goog.math.Long
cljs.core
com.cognitect.transit
com.cognitect.transit.delimiters
com.cognitect.transit.handlers
com.cognitect.transit.util
com.cognitect.transit.caching
com.cognitect.transit.types
com.cognitect.transit.eq
com.cognitect.transit.impl.decoder
com.cognitect.transit.impl.reader
com.cognitect.transit.impl.writer}
(set/union
@goog-loaded
'#{goog
cljs.core
com.cognitect.transit
com.cognitect.transit.delimiters
com.cognitect.transit.handlers
com.cognitect.transit.util
com.cognitect.transit.caching
com.cognitect.transit.types
com.cognitect.transit.eq
com.cognitect.transit.impl.decoder
com.cognitect.transit.impl.reader
com.cognitect.transit.impl.writer})
name)
(ana/node-module-dep? name))))

Expand Down Expand Up @@ -323,19 +330,31 @@
(when-not (load-and-cb! nil path filename false cb)
(cb nil))))

(defn- load [{:keys [name macros path file] :as m} cb]
(cond
file
(defn- load [{:keys [name macros path file dump-index?]
:as m
:or {dump-index? true}} cb]
(if-not (nil? file)
(load-file* file cb)

(skip-load? name macros)
(cb {:source ""
:lang :js})

(re-matches #"^goog/.*" path)
(load-goog name cb)

:else (load-other m cb)))
(let [goog-lib? (re-matches #"^goog/.*" path)]
(cond
(skip-load? name macros)
(let [cenv @env/*compiler*]
(when (and goog-lib? dump-index?
;; `goog.string` is part of the Google Closure Library
;; set of namespaces. This way we avoid searching the
;; JS dependency index for a library with :group :goog
(nil? (get-in cenv [:js-dependency-index "goog.string"])))
(swap! env/*compiler* update-in [:js-dependency-index] merge (closure-index)))
(cb {:source ""
:lang :js}))

goog-lib?
(load-goog name cb)

:else (load-other m cb)))))

(defn- load-no-dump-index [m cb]
(load (assoc m :dump-index? false) cb))

(defn- macros-cache? [cache]
(.endsWith (str (:name cache)) MACROS_SUFFIX))
Expand Down Expand Up @@ -1085,7 +1104,7 @@
[warning-handler]
[ana/default-warning-handler])
cljs/*eval-fn* caching-node-eval
cljs/*load-fn* load
cljs/*load-fn* (if print-nil-result? load load-no-dump-index)
ana/*cljs-ns* @current-ns
*ns* (create-ns @current-ns)
env/*compiler* st
Expand All @@ -1111,6 +1130,10 @@
:else "source")
eval-opts
(fn [{:keys [ns value error] :as ret}]
(let [cenv @st]
;; TODO: change name later, `print-nil-result?` is only for initialization
(when-not print-nil-result?
(swap! cljs/*loaded* set/difference @goog-loaded)))
(if-not error
(when expression?
(when (or (true? print-nil-result?)
Expand All @@ -1136,13 +1159,14 @@
(execute-text source-or-path opts)))

(defn- ^:export execute
[type source-or-path expression? print-nil-result? setNS session-id]
[type source-or-path expression? print-nil-result? setNS session-id dump-js-index?]
(when setNS
(vreset! current-ns (symbol setNS)))
(execute-source source-or-path {:type type
:expression? expression?
:print-nil-result? print-nil-result?
:session-id session-id}))
:session-id session-id
:dump-js-index? dump-js-index?}))

(defn- ^:export is-readable?
([form] (is-readable? form false))
Expand Down Expand Up @@ -1211,11 +1235,7 @@
(setup-assert! elide-asserts)
(setup-print-namespace-maps! repl?)
(common/load-core-analysis-caches st repl?)
(deps/index-js-libs)
(let [index @deps/js-lib-index]
(swap! st assoc :js-dependency-index (into index
(map (fn [[k v]] [(str k) v]))
index))))
(swap! st assoc :js-dependency-index (deps/index-js-libs)))

;; --------------------
;; Introspection
Expand Down
2 changes: 1 addition & 1 deletion src/js/cljs.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ async function startClojureScriptEngine(opts: CLIOptsType): Promise<mixed> {
}

execute(
"(require '[lumo.repl :refer [apropos find-doc] :refer-macros [dir doc source]])",
"(require '[lumo.repl :refer [apropos find-doc dir doc source]])",
'text',
true,
false,
Expand Down
15 changes: 10 additions & 5 deletions src/test/lumo/lumo/js_deps_tests.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@
{:provides ["react.dom.server"]
:requires ["react" "react.dom"]}]]
(is (= (deps/add-js-libs {} js-libs)
'{react {:provides ["react"]}
react.dom {:provides ["react.dom"]
:requires ["react"]}
react.dom.server {:provides ["react.dom.server"]
:requires ["react" "react.dom"]}}))))
'{react {:provides ["react"]}
"react" {:provides ["react"]}
react.dom {:provides ["react.dom"]
:requires ["react"]}
"react.dom" {:provides ["react.dom"]
:requires ["react"]}
react.dom.server {:provides ["react.dom.server"]
:requires ["react" "react.dom"]}
"react.dom.server" {:provides ["react.dom.server"]
:requires ["react" "react.dom"]}}))))

(deftest js-libs-to-load-test
(with-redefs [deps/js-lib-index
Expand Down