Learn how to set up a ClojureScript development environment with tools like Figwheel Main and Shadow CLJS for efficient builds and live reloading.
As experienced Java developers, you’re likely familiar with setting up environments for Java applications. Transitioning to ClojureScript, a powerful tool for building modern web applications, involves setting up a development environment that supports live reloading and efficient builds. In this guide, we’ll walk through setting up a ClojureScript environment using tools like Figwheel Main and Shadow CLJS. These tools streamline the development process by providing live reloading, hot code swapping, and efficient build processes.
ClojureScript is a variant of Clojure that compiles to JavaScript, allowing you to write Clojure code that runs in the browser. It leverages the power of functional programming and immutability, offering a robust alternative to JavaScript for frontend development. ClojureScript integrates seamlessly with JavaScript libraries and frameworks, making it a versatile choice for web applications.
Before diving into the setup, let’s explore the key tools we’ll be using:
Ensure that you have Java installed on your system, as ClojureScript relies on the Java Virtual Machine (JVM). You can verify your Java installation by running:
java -version
If Java is not installed, download and install it from the official Oracle website.
Leiningen is essential for managing Clojure projects. Install it by following these steps:
Download the Leiningen script:
curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein -o /usr/local/bin/lein
Make the script executable:
chmod +x /usr/local/bin/lein
Run Leiningen to download dependencies:
lein
Use Leiningen to create a new ClojureScript project. We’ll use Figwheel Main for live reloading:
lein new figwheel-main my-clojurescript-app -- --reagent
This command creates a new project named my-clojurescript-app
with Reagent, a minimalistic React wrapper for ClojureScript.
Navigate to your project directory and open the deps.edn
file. Add the following configuration to enable Figwheel Main:
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.3"}
org.clojure/clojurescript {:mvn/version "1.10.844"}
com.bhauman/figwheel-main {:mvn/version "0.2.14"}
reagent {:mvn/version "1.1.0"}}
:aliases {:fig {:main-opts ["-m" "figwheel.main" "-b" "dev" "-r"]}}}
This configuration specifies the paths, dependencies, and aliases for running Figwheel Main.
Start the Figwheel Main server to enable live reloading:
clojure -A:fig
This command compiles your ClojureScript code and opens a browser window with live reloading enabled. Any changes you make to your code will automatically reflect in the browser.
Shadow CLJS is an alternative to Figwheel Main, offering advanced features like npm integration. To install Shadow CLJS, follow these steps:
Install Node.js: Shadow CLJS requires Node.js. Download and install it from the official Node.js website.
Install Shadow CLJS globally:
npm install -g shadow-cljs
Create a Shadow CLJS configuration file: In your project directory, create a shadow-cljs.edn
file with the following content:
{:source-paths ["src"]
:dependencies [[reagent "1.1.0"]]
:builds {:app {:target :browser
:output-dir "public/js"
:asset-path "/js"
:modules {:main {:entries [my-clojurescript-app.core]}}}}}
Run Shadow CLJS:
shadow-cljs watch app
This command compiles your ClojureScript code and watches for changes, providing live reloading.
Both Figwheel Main and Shadow CLJS offer live reloading and efficient builds, but they have distinct features:
Let’s create a simple ClojureScript component using Reagent. Open the src/my_clojurescript_app/core.cljs
file and add the following code:
(ns my-clojurescript-app.core
(:require [reagent.core :as r]))
(defn hello-world []
[:div
[:h1 "Hello, ClojureScript!"]
[:p "This is a simple Reagent component."]])
(defn mount-root []
(r/render [hello-world]
(.getElementById js/document "app")))
(defn init []
(mount-root))
This code defines a simple Reagent component that displays a greeting message. The init
function mounts the component to the DOM element with the ID app
.
Experiment with the code by modifying the hello-world
function to display different messages or add new HTML elements. Observe how Figwheel Main or Shadow CLJS automatically reloads the changes in your browser.
Below is a diagram illustrating the ClojureScript development workflow using Figwheel Main or Shadow CLJS:
graph TD; A[Edit ClojureScript Code] --> B[Figwheel Main / Shadow CLJS]; B --> C[Compile to JavaScript]; C --> D[Live Reload in Browser]; D --> A;
Diagram Description: This flowchart represents the iterative development process in ClojureScript, where code edits are compiled and live-reloaded in the browser.
For more information on ClojureScript and its ecosystem, consider exploring the following resources:
Create a New Component: Extend the example by creating a new Reagent component that displays a list of items. Use a vector to represent the list and map over it to generate HTML elements.
Integrate a JavaScript Library: Use Shadow CLJS to integrate a popular JavaScript library, such as Lodash or Moment.js, into your ClojureScript project. Create a component that utilizes the library’s functionality.
Explore Module Splitting: With Shadow CLJS, experiment with module splitting by creating multiple entry points in your application. Observe how this affects the build process and application performance.
Now that we’ve set up the ClojureScript environment, you’re ready to build interactive web applications with the power of Clojure. Let’s continue to explore how these tools can enhance your development workflow.