Skip to content

Quick start: ClojureScript (shadow cljs)

Kovas Palunas edited this page May 23, 2022 · 2 revisions

ClojureScript is a robust, practical, and fast programming language with a set of useful features that together form a simple, coherent, and powerful tool.

shadow-cljs - ClojureScript compilation made easy!

Conjure has deep support for ClojureScript through a few different helper tools such as figwheel (Conjure setup) and shadow-cljs. We’re going to focus on shadow-cljs here since it’s really neat and works well with Conjure.

Prerequisites

  1. Install the latest Neovim.

  2. Install the Conjure plugin.

  3. Install and learn about shadow-cljs through the beginner guide.

Add CIDER middleware

By this point you should be able to start your application through shadow-cljs and have it loaded in your browser (or any other JavaScript runtime). We need to add the CIDER middleware by adding the following to shadow-cljs.edn.

:dependencies [[cider/cider-nrepl "0.24.0"]]

Here’s the example shadow-cljs.edn I use to test support.

{:source-paths
 ["dev/clojure/src"]

 :dependencies
 [[cider/cider-nrepl "0.24.0"]]

 :dev-http {8080 "dev/clojure/public"}

 :builds
 {:app {:target :browser
        :output-dir "dev/clojure/public/js"
        :asset-path "/js"
        :modules {:main {:entries [dev.sandbox]}}}}}

Now start shadow-cljs again, your command may be different if you didn’t name your build app.

npx shadow-cljs watch app

Connect and select

We can’t evaluate ClojureScript with Conjure right away, first we have to open a Clojure or ClojureScript file and have Conjure connect to the nREPL port. All evaluations right now will be run as Clojure within shadow-cljs' JVM.

To switch over to evaluating in our browser (or other JavaScript environment depending on your configuration) we must use :ConjureShadowSelect [build name] from within Neovim like so.

:ConjureShadowSelect app

A hacky way to automate this build selection is to use this autocommand:

function! AutoConjureSelect()
  let shadow_build=system("ps aux | grep 'shadow-cljs watch' | head -1 | sed -E 's/.*?shadow-cljs watch //' | tr -d '\n'")
  let cmd='ConjureShadowSelect ' . shadow_build
  execute cmd
endfunction
command! AutoConjureSelect call AutoConjureSelect()
autocmd BufReadPost *.cljs :AutoConjureSelect

Now all further evaluations will be in your configured ClojureScript runtime.

If you’re unsure how to evaluate things with Conjure, please refer to :help conjure, :help conjure-client-clojure-nrepl and :ConjureSchool (an interactive tutorial).