Thomas Skardal

Hello, computer.

Sharing code between Clojure and ClojureScript

February 6, 2015

As preparation for my next step on BattleSnake I’ve created a sample application on GitHub that demonstrates shared code between Clojure and ClojureScript. The intention is to run the same game logic both server side and client side.

This is achieved by using the leiningen plugin cljx which enables you to produce both variants by using annotations.

This isn’t a very useful example, but it illustrates the necessary configuration needed to use a shared codebase.

(ns example.shared)

(defn hello []
  "Hello there!")

(defn log []
  #+clj (println "hello from cljx")
  #+cljs (js/console.log "hello from cljx"))

The functions defined above can be called from both Clojure and ClojureScript :-)