(defn load-libs
[libs seen reloads deps ns-name]
(let [{:keys [options js-dependency-index]} @env/*compiler*
{:keys [target optimizations]} options
loaded-libs (munge 'cljs.core.*loaded-libs*)
loaded-libs-temp (munge (gensym 'cljs.core.*loaded-libs*))
[node-libs libs-to-load] (let [libs (remove (set (vals seen)) (filter (set (vals libs)) deps))]
(if (= :nodejs target)
(let [{node-libs true libs-to-load false} (group-by ana/node-module-dep? libs)]
[node-libs libs-to-load])
[nil libs]))
{global-exports-libs true libs-to-load false} (group-by ana/dep-has-global-exports? libs-to-load)]
(when (-> libs meta :reload-all)
(emitln "if(!COMPILED) " loaded-libs-temp " = " loaded-libs " || cljs.core.set([\"cljs.core\"]);")
(emitln "if(!COMPILED) " loaded-libs " = cljs.core.set([\"cljs.core\"]);"))
(doseq [lib libs-to-load]
(cond
#?@(:clj
[(ana/foreign-dep? lib)
;; we only load foreign libraries under optimizations :none
;; under :modules we also elide loads, as the module loader will
;; have handled it - David
(when (and (= :none optimizations)
(not (contains? options :modules)))
(if (= :nodejs target)
;; under node.js we load foreign libs globally
(let [ijs (get js-dependency-index (name lib))]
(emitln "cljs.core.load_file("
(-> (io/file (util/output-directory options)
(or (deps/-relative-path ijs)
(util/relative-name (:url ijs))))
str
escape-string
wrap-in-double-quotes)
");"))
(emitln "goog.require('" (munge lib) "');")))]
:cljs
[(and (ana/foreign-dep? lib)
(not (keyword-identical? optimizations :none)))
nil])
(or (-> libs meta :reload)
(= (get reloads lib) :reload))
(emitln "goog.require('" (munge lib) "', 'reload');")
(or (-> libs meta :reload-all)
(= (get reloads lib) :reload-all))
(emitln "goog.require('" (munge lib) "', 'reload-all');")
:else
(emitln "goog.require('" (munge lib) "');")))
(doseq [lib node-libs]
(emitln (munge ns-name) "."
(ana/munge-node-lib lib)
" = require('" lib "');"))
(doseq [lib global-exports-libs]
(let [{:keys [global-exports]} (get js-dependency-index (name lib))]
(emitln (munge ns-name) "."
(ana/munge-global-export lib)
" = goog.global." (get global-exports (symbol lib)) ";")))
(when (-> libs meta :reload-all)
(emitln "if(!COMPILED) " loaded-libs " = cljs.core.into(" loaded-libs-temp ", " loaded-libs ");"))))