Certainly! ClojureScript is a variant of the Clojure language that compiles to JavaScript. It provides functional programming and immutable data structures and is designed to run on browsers and other JavaScript environments. Here we'll cover the basics of setting up a ClojureScript environment and writing a simple web application.
Setup
First, we need to set up the development environment for ClojureScript:
- Install Java: ClojureScript runs on the Java Virtual Machine (JVM), so you need to have Java installed to use it.
- Install Leiningen: This is a build tool for Clojure that also supports ClojureScript. You can download it from Leiningen's website.
Project Setup
To create a new ClojureScript project:
- Run
lein new mies myapp in your terminal to create a new ClojureScript project using the Mies template. Replace myapp with whatever you want to name your project.
- Navigate to your new project directory (
cd myapp).
Structure of a ClojureScript File
ClojureScript files use the .cljs extension. Here is what a simple ClojureScript file looks like:
(ns my-app.core) ;; Define the namespace
;; Define a function
(defn greet [name]
(str "Hello, " name "!"))
;; Call the function and print the output
(println (greet "World"))
Compiling ClojureScript
To compile your ClojureScript code to JavaScript, run:
lein cljsbuild once
This will generate JavaScript files in the out directory (or another target directory specified in the project.clj file).
Working with the Browser
To do something useful, like manipulating the DOM in a browser, use the goog.dom and goog.events namespaces:
(ns my-app.core
(:require [goog.dom :as dom]
[goog.events :as events]))
(defn add-greeting []
(let [body (dom/getElement "body")
greeting (dom/createDom "h1" nil "Hello, ClojureScript World!")]
(dom/appendChild body greeting)))
(defn main []
(add-greeting))
;; Run `main` once the DOM is fully loaded.
(events/listen (dom/getElement "window") "load" main)
Interacting with JavaScript
ClojureScript can interact with existing JavaScript libraries. You will typically use the js/ prefix to access global JavaScript objects.
(ns my-app.core)
;; Accessing the JavaScript alert function
(js/alert "This is a JavaScript alert from ClojureScript!")
Integrating with HTML
To hook up your ClojureScript with an HTML file, simply include the compiled JavaScript in your HTML:
<!DOCTYPE html>
<html>
<head>
<title>ClojureScript App</title>
</head>
<body>
<script src="path/to/compiled-clojurescript.js"></script>
</body>
</html>
Project Configuration (project.clj)
ClojureScript project configuration is done in a file named project.clj. Here's a simple example:
(defproject my-app "0.1.0"
:dependencies [[org.clojure/clojure "1.10.3"]
[org.clojure/clojurescript "1.10.773"]]
:plugins [[lein-cljsbuild "1.1.7"]]
:cljsbuild {
:builds [{:source-paths ["src"]
:compiler {
:main my-app.core
:output-to "out/main.js"
:output-dir "out"
:optimizations :advanced}}]})
In this file, you can specify dependencies, plugins, and compilation options.
Next Steps
- Learn more about ClojureScript: Read the ClojureScript documentation, explore tutorials, and go through resources like books and online courses.
- Explore Libraries: There are many libraries available for ClojureScript, such as Reagent (for React integration) or re-frame (for managing app state and effects).
- Experiment and Practice: Build small projects, contribute to open source, and get practical experience.
- Get to know the tooling: Familiarize yourself with REPLs, editors, and other tools that support ClojureScript development.
Conclusion
This tutorial just scratches the surface of what you can do with ClojureScript. ClojureScript is a powerful language that benefits from the robustness of the Clojure ecosystem and the flexibility of the JavaScript environment. Remember that the key to getting proficient in ClojureScript is practice and experimentation. Have fun building!