Add ClojureScript Support

Web Development with Clojure, Third Edition — by Dmitri Sotnikov, Scot Brown (27 / 107)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Understand Cloj ureScript | TOC | Build the UI with Reagent 👉

Currently, our guestbook application uses server-side rendering and the browser simply displays a static page.

Let’s modify our project from Chapter 1, Getting Your Feet Wet, to be a single-page application (SPA) using ClojureScript.

Using lein-cljsbuild

The easiest way to add ClojureScript support to a Clojure project is by using the lein-cljsbuild plugin.[38] The plugin compiles the ClojureScript sources and outputs the resulting JavaScript in the specified location.

The first thing we have to do is to add the ClojureScript runtime dependency to our project. Note that it’s scoped as “provided”. Since we’re outputting compiled JavaScript, it only needs to be present for development.

​ :dependencies
​ [...
​ [org.clojure/clojurescript ​"1.10.764"​ :scope ​"provided"​]]

Next, let’s update our project.clj to add the plugin, provide a default configuration for it, and include the generated javascript in our :resource-paths.

guestbook-reagent/project.clj

​ :resource-paths [​"resources"​ ​"target/cljsbuild"​]
​ :target-path ​"target/%s/"​
​ :main ^:skip-aot guestbook.core

​ :plugins [[lein-cljsbuild ​"1.1.8"​]]

​…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.