Basti's Scratchpad on the Internet

Leiningen, ClojureScript and Browser REPL

Note to self, make sure ClojureScript is on classpath,

(defn cljs-home [path]
  (if-let [home (get (System/getenv) "CLOJURESCRIPT_HOME")]
    (str home path)
    (throw (Exception. "You must set the $CLOJURESCRIPT_HOME variable!"))))

(defproject foo "1.0.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.3.0"]
		 [noir "1.2.2-SNAPSHOT"]]
  :extra-classpath-dirs ~(map cljs-home ["/lib/*" "/src/clj" "/src/cljs"]))

Compile and move browser repl,

(ns foo
  (:require [clojure.browser.repl :as repl]))

(repl/connect "http://localhost:9000/repl")
mkdir source/resources/public/
cljsc source/resources/foo.cljs > source/resources/public/foo.js
mv out/ source/resources/public/

Setup noir to serve browser repl,

(ns foo.core
  (:use [noir.core]
	[hiccup.core])
  (:require [noir.server :as server]
	    [cljs.repl :as repl]
	    [cljs.repl.browser :as browser]))

(defpage "/" []
  (html
   [:html
    [:head]
    [:body
     [:script {:type "text/javascript" :src "out/goog/base.js"}]
     [:script {:type "text/javascript" :src "foo.js"}]
     [:script {:type "text/javascript"} "goog.require('foo');"]]]))

;;"Type: " :cljs/quit " to quit"
(defn foo-repl []
  (repl/repl (browser/repl-env)))

(defn server []
  (server/start 8080))

Start server, repl and navigate to http://127.0.0.1:8080/, you should be good to go.

(server)
(foo-repl)
Other posts
comments powered by Disqus