Run ClojureScript functions in Node.js with boot-eval-cljs
In the past year of diving deep into ClojureScript, I've found myself writing the same Boot task many times: a simple .cljs.edn
file is compiled to JavaScript, written to target/
and then executed in Node.js.
So I decided to provide a minimal Boot task as a library. No fancy options or complex configuration, just a symbol for a function to evaluate.
The result is called boot-eval-cljs.
To get started, add the library to the dependencies in build.boot
and require the eval-cljs
task:
(set-env! :dependencies '[[adamrenklint/boot-eval-cljs "1.1.1"]])
(require '[adamrenklint.boot-eval-cljs :refer [eval-cljs]])
Let's assume we have a file called src/foo/bar.cljs
with the contents:
(ns foo.bar)
(defn print-hello-world []
(js/console.log "Hello world!"))
You could now evaluate the function, i.e. compile to JavaScript and execute in Node.js, from the command line:
> boot eval-cljs -f foo.bar/print-hello-world
Or if you want to create a specific task in build.boot
, it can easily be composed with other parts of your build pipeline:
(deftask print-hello-world []
(eval-cljs :fn 'foo.bar/print-hello-world))
(deftask dev []
(comp (watch)
(print-hello-world)))
There is no need to create a .cljs.edn
file, just provide the task with a symbol that resolves to a function. It can't get much simpler than that.
The final benefit is the support for sourcemaps - the necessary NPM modules are installed in target/
, giving you stacktraces with up to 100 frames, which points to the correct location in the original source and makes debugging more straightforward and productive.
The library is published to Clojars, and as always, the source code is available on GitHub under the MIT license.