Skip to content

Instantly share code, notes, and snippets.

Last active Sep 17, 2018
What would you like to do?
loaded-namespaces REPL tool

If you'd like to know the namespaces currently loaded by ClojureScript, it is easy to get some insight using a macro:

(defmacro loaded-namespaces []
  `(remove nil? [~@(map (fn [ns] `(when (cljs.core/exists? ~ns) '~ns))
                     (sort (keys (:cljs.analyzer/namespaces @cljs.env/*compiler*))))]))

This command uses this gist to set up this macro along with a REPL:

clj -Srepro -Sdeps '{:deps {github-mfikes/gist-da9a7bde16a5087ca5c6ee2133e62be2 {:git/url "" :sha "654749eab580390124eb12ca1d22796b37d5eff9"}}}' -m cljs.main -co @compile-opts.edn -r

Here is an example of using it:

cljs.user=> (loaded-namespaces)
(cljs.core cljs.repl cljs.spec.alpha cljs.spec.gen.alpha cljs.user clojure.browser.event clojure.browser.repl clojure.browser.repl.preload clojure.string clojure.walk)

This is the kind of dev-time macro you could refer using the new user.cljs feature. For example: put the macro in a utils.core Clojure file (in src/utils/core.clj) and then in src/user.cljs put:

(ns cljs.user
 (:require-macros [utils.core :refer [loaded-namespaces]]))
{:repl-requires [[cljs.repl :refer [doc source pst]]
[repl-utils :refer [loaded-namespaces]]]
:warnings {:single-segment-namespace false}}
{:paths ["."]
:deps {org.clojure/clojurescript {:mvn/version "1.10.339"}}}
(ns repl-utils)
(defmacro loaded-namespaces []
`(remove nil? [~@(map (fn [ns] `(when (cljs.core/exists? ~ns) '~ns))
(sort (keys (:cljs.analyzer/namespaces @cljs.env/*compiler*))))]))
(ns repl-utils
(:require-macros [repl-utils]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment