clojurescriptclojurescript-javascript-interop

Adding class paths in clojurescript at runtime?


Is it possible to add classpaths in a Clojurescript program running on Nodejs at runtime?


Solution

  • Assuming you are referring to modifying the classpath of a Node-based REPL at dev-time, perhaps if add-lib becomes a thing, then this would be pretty easy to do. Here is an experiment showing using add-lib in a revised version of ClojureScript to dynamically modify the classpath and load a (dynamically fetched) library:

    deps.edn:

    {:deps {org.clojure/clojurescript {:git/url "https://github.com/mfikes/clojurescript"
                                       :sha "4fa9edd736d47b8ef5648b61b199e64ef80735bb"}
            org.clojure/tools.deps.alpha {:git/url "https://github.com/clojure/tools.deps.alpha.git"
                                          :sha "d492e97259c013ba401c5238842cd3445839d020"}}}
    

    Using it:

    $ clj -m cljs.main -re node
    cljs.user=> (require '[cljs.repl :refer [add-lib]])
    nil
    cljs.user=> (add-lib 'spec-provider {:mvn/version "0.4.14"})
    Downloading: spec-provider/spec-provider/0.4.14/spec-provider-0.4.14.pom from https://repo.clojars.org/
    Downloading: spec-provider/spec-provider/0.4.14/spec-provider-0.4.14.jar from https://repo.clojars.org/
    true
    cljs.user=> (require '[spec-provider.provider :as sp])
    nil
    cljs.user=> (sp/infer-specs
     [{:a 8  :b "foo" :c :k}
      {:a 10 :b "bar" :c "k"}
      {:a 1  :b "baz" :c "k"}]
     :toy/small-map)
    ((cljs.spec.alpha/def :toy/c (cljs.spec.alpha/or :keyword cljs.core/keyword? :string cljs.core/string?)) (cljs.spec.alpha/def :toy/b cljs.core/string?) (cljs.spec.alpha/def :toy/a cljs.core/integer?) (cljs.spec.alpha/def :toy/small-map (cljs.spec.alpha/keys :req-un [:toy/a :toy/b :toy/c])))
    cljs.user=>