Shadow-cljs and leinigen and NPM oh my!

It’s close:

When you first connect to shadow-cljs’ nREPL server, you’re initially inside of a Clojure JVM REPL session. So you can execute Clojure commands in the JVM process that shadow-cljs runs in.

We want to interact with our ClojureScript application running in a browser, though, not the shadow-cljs application. We need to upgrade our REPL to a ClojureScript REPL. piggieback is usually used to upgrade our nREPL connection from a Clojure REPL to a ClojureScript REPL, though shadow-cljs comes with that functionality built-in. To not break editors, it also provides the cemerick.piggieback API as well.

Usually I connect to a the shadow-cljs nREPL connection and then immediately run (shadow/repl :app), which is the shadow-cljs-specific API. Running (cemerick.piggieback/cljs-repl :app) does the exact same thing. That’s what the vim command :Piggieback :app does: run the cemerick.piggieback/cljs-repl command, which is the same thing as running shadow/repl.

Finally, just like a Clojure REPL needs a JVM process to execute expressions in, so does a ClojureScript REPL need a JavaScript environment to execute expressions in. The difference between Clojure and ClojureScript is that usually the Clojure REPL server is running in the same process as the one we’re executing expressions in, but with ClojureScript we send expressions to the REPL server which then compiles it to JS and sends it to the JS process.

In order to start the JS process, we open a web browser to the page we’re running our compiled code in which will then connect to our REPL server and begin to receive compiled expressions.

To summarize the steps needed to begin developing:

  1. Start shadow-cljs shadow-cljs watch app
  2. Connect to the nREPL server (I’m not a Vim user, but something like :Connect ?)
  3. Upgrade our REPL to a CLJS REPL :Piggieback :app
  4. Open our development web page in a web browser

Hope that helps!

1 Like