How to setup a Clojurescript REPL with emacs

clojurescriptread-eval-print-loop

I'm trying to setup an environment for Clojurescript. The problem I'm having is not knowing how to set it up so that I can connect to a Clojurescript Browser REPL from emacs, so I can evaluate forms right from the editor and have it show up in the browser.

Things I've tried:

I tried using Cemerick's piggieback and piggybacking on nREPL. I don't really know how to configure it from the documentation. I was able to get it to work after figuring out I had to make an index.html file in the root of the project folder. Except the server doesn't serve my assets.

I tried setting up inferior-lisp with the bash script from the Clojurescript wiki. However, whenever I try to run the inferior-lisp-program I'm getting "Wrong type argument: stringp, nil".

Here's my ideal workflow:

I have a project folder/resources/public folder that has my assets and html files. I can start a web server that serves those files somehow, either by ring or using python's simple http server. I'd be able to connect to a REPL from emacs and evaluate forms into it.

Best Answer

Cemerick to the rescue again: Austin (https://github.com/cemerick/austin) is exactly what you're after.

Austin gives you two options: either a REPL where the JS is evaluated in a browser (a "project REPL"), or a more complete, integrated browser-connected REPL. It sounds like the latter is what you're after, and you need to dig a little harder for its docs: https://github.com/cemerick/austin/tree/master/browser-connected-repl-sample

To get the browser-connected REPL working, Chas's example is fairly straightforward to follow, and boils down to:

  1. Add a little middleware or template magic to append a JS script element to your HTML page, and that script needs to contain the output of (cemerick.austin.repls/browser-connected-repl-js)
  2. Start up nREPL, start up your ring server, and then
    (def repl-env (reset! cemerick.austin.repls/browser-repl-env (cemerick.austin/repl-env)))
    to create a REPL environment.
  3. Turn the nREPL session from a Clojure to a ClojureScript REPL with
    (cemerick.austin.repls/cljs-repl repl-env)
  4. Connect to your still-running Ring server app with a browser, and you should be connected.
    (.alert js/window "Hi!") should prove it.
  5. Using the standard Emacs nREPL commands will work as expected, compiling ClojureScript into JavaScript and sending it to the browser for evaluation.

The main difference between Piggieback and Austin is those first and second steps: the atom is used by the middleware to add an inline JavaScript block that connects back to the nREPL. Since the HTTP URL is determined at runtime, the Ring server and client-side JavaScript need to work in concert.

FWIW I created a function in my user namespace to speed connecting the CLJS REPL:

(defn cljs-browser-repl
  "Fire up a browser-connected ClojureScript REPL"
  []
  (let [repl-env (reset! cemerick.austin.repls/browser-repl-env
                         (cemerick.austin/repl-env))]
    (cemerick.austin.repls/cljs-repl repl-env)))
Related Topic