Skip to content

Instantly share code, notes, and snippets.

@mfikes
Last active September 17, 2018 18:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfikes/da9a7bde16a5087ca5c6ee2133e62be2 to your computer and use it in GitHub Desktop.
Save mfikes/da9a7bde16a5087ca5c6ee2133e62be2 to your computer and use it in GitHub Desktop.
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 "https://gist.github.com/mfikes/da9a7bde16a5087ca5c6ee2133e62be2" :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.net 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