Get Started with ClojureScript with Leiningen Templates

When I was about to get started with ClojureScript, I was discouraged by the fact that I apparently had to figure out so much before getting a trivial project up and running.

Eventually I learned, built and showed a minimal application running with just Leiningen and Ring, and a little bit of jQuery in ClojureScript.

Some time later Kyle Cordes showed me cljs-template. It’s a Leiningen template created by Chris Granger, also known as the guy behind Noir and Light Table. That was quite fun. All you need to get a project up and running is:

lein new cljs-template my-project
cd my-project
lein run

That’s it, you’re now running a Noir application with ClojureScript in client (jQuery included). You can start hacking at the CLJS source and see changes in browser immediately.

I soon discovered that it was a few months old, using Clojure 1.3, dated build of ClojureScript and pretty much everything. Eventually (thanks to Kyle and Raynes) I got push access to the project and updated everything, so it should be in even better shape now.

I am not sure where cljs-template is going though, with Noir itself going away. I also found one bit missing: That template is awesome to get up and running and show off a demo, but you would still need to do some manual plumbing to make such a project work for a real application (with leiningen hooks on compilation etc.).

That’s why I created another template: cljs-kickoff. Like my first steps, it’s really minimal: just Ring, lein-cljsbuild and ClojureScript. Fewer files, fewer dependencies, very easy to grasp.

To kick it off, just run:

lein new cljs-kickoff my-project
cd my-project
lein run

It will compile the ClojureScript file included in the project and start Ring server with it.

In another shell, you can run:

lein cljsbuild auto

This will start lein-cljsbuild in the auto-compile mode. Whenever the CLJS source changes, it will be automatically recompiled and the running application will pick it up after reload.

Compared to cljs-template, this template is much smaller and only uses very basic, popular and mature pieces (just Ring and CLJS). It also has all the “real” Leiningen hooks in place: CLJS compilation is included in lein run, lein jar and lein uberjar.

I hope it all makes someone’s life easier by making the first step on CLJS path as easy as possible. Happy hacking!

9 thoughts on “Get Started with ClojureScript with Leiningen Templates

  1. Thank you very much for that! This is a convenient and lean setup.

    I’ve slightly modified render-app function in server.clj file to use a static app.html file, instead of generating it:

    (defn render-app []
    (response/redirect “/app.html”))

  2. Tim – I’m glad you like it, thank you!

    I wanted to show (or provide “hooks” for) both ways: generated content and static files. You can do everything in Clojure, e.g. with enlive/hiccup/stencil, mix and match with static (e.g. static CSS/JS, generated html), or you can write only the server code in Clojure and serve all content from static – like a single-page JS-heavy webapp. Whatever works best for you, hopefully the template doing both saves you 5 minutes of wondering where to put something. :-)

  3. Oh, man. It saved me much more than five minutes! I’ve opened two terminals as you suggested: one to run a Ring server, the second to auto compile sources.

    After that I edit HTML in HTML editor, and edit client.cljs in a text editor. Every time I save cljs-file, lein re-compiles it automatically. It’s perfect.

    I can not express enough what a relieve it is to concentrate on a task at hand, rather than spend countless hours configuring all these layers of software running on top of each other.

  4. Buck – that sounds very interesting. I’ll take a look.

    I also thought about templates like “CLJS with jQuery”, “CLJS with Compojure” etc. In a way it’s a shame we have to do this. I wish we could say “gimme CLJS, Compojure and Heroku” without creating a special template for each possible combination. I know why it’s technically challenging (if possible), but one can dream. :-)

  5. Thanks for the template, works perfectly.

    As a JS dev trying to get into clojurescript, it is very dauting to set up and realize the workflow, but I’m getting bit by bit.

  6. Thanks! In between the thousands of outdated instructions out there on the web, these finally got me up and running.

  7. I am searching a ready-made lein template to create a Clojurescript app. This really fills my need. Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *

Spam protection by WP Captcha-Free