I'm releasing clojurejs – a Clojure library for translating a Clojure subset language to Javascript. The code is on github reachable at the following link:

http://github.com/kriyative/clojurejs

clojurejs is something I've been working on for a few weeks as part of a larger web app in Clojure. The code's a bit crufty (reflects my incremental discovery of the inconsistencies in Javascript), but functional and I wanted put something out there for people to check out. I welcome bug reports and feedback. It's been useful for my specific needs, and I'd be happy if it's even marginally useful to others.

I realize there are a number of other efforts to compile/translate Clojure (or other Lisp subset) to Javascript, but nothing quite fit my requirements, which prompted me to build clojurejs. Some useful aspects of clojurejs are:

  • Consistent scoping in let and loop/recur forms
  • Macros with defmacro
  • Implicit return from all forms
  • loop/recur translates to Javascript for loops
  • Translates Clojure vectors, strings, keywords, symbols and maps to Javascript equivalents
  • dot form access to methods and properties

Here's an example from the README:

(js
 (defn join [arr delim]
   (loop [str (get arr 0)
          i 1]
     (if (< i (length arr))
       (recur (+ str delim (get arr i))
              (+ i 1))
       str))))

translates to the following Javascript:

join = function(arr, delim) {
    return (function () {
        for (var str = arr[0],i = 1; true;) {
            if ((i < arr.length)) {
                str = (str + delim + arr[i]);
                i = (i + 1);
                continue;
            } else {
                return str;
            } break;
        }
    })();
}

Documentation is regrettably scarce, but there are more details in the README file, boot.cljs, and the unit tests. I hope to add more documentation and examples in the next few weeks. Stay tuned.