Skip to content

Instantly share code, notes, and snippets.

@kohyama
Last active December 20, 2015 20:38
Show Gist options
  • Save kohyama/6191281 to your computer and use it in GitHub Desktop.
Save kohyama/6191281 to your computer and use it in GitHub Desktop.
How to use browser REPL of ClojureScript with compojure

How to use browser REPL of ClojureScript with compojure

You can do this without compojure. Refer [A minimum setting to use browser REPL of ClojureScript] (https://gist.github.com/kohyama/6183122).

Assumed that you have set leiningen up and can use it.

1. Prepare files

Copy project.clj, repl-test.cljs, minimum_httpd.clj and repl-test.html from this gist or git clone this gist. Move or copy repl-test.cljs under src-cljs directory, minimum_httpd.clj under src and repl-test.html under resources/public

$ git clone https://gist.github.com/6191281.git
$ cd 6191281/
$ mkdir -p src src-cljs resources/public
$ mv repl-test.cljs src-cljs/
$ mv minimum_httpd.clj src/
$ mv repl-test.html resources/public/

2. Compile

Compile repl-test.cljs into resources/public/repl-test.js.

$ lein cljsbuild once

3. Run a ClojureScript REPL

$ lein trampoline cljsbuild repl-listen
Running ClojureScript REPL, listening on port 9000.
"Type: " :cljs/quit " to quit"
ClojureScript:cljs.user>

4. Start httpd and open the HTML file

Do below on another terminal

$ lein ring server

This opens http://localhost:3000/ with a new window or tab of your default web browser automatically and it fails with 404 because we didn't define the response for /.

Open resources/public/repl-test.html on a web browser via the httpd

http://localhost:3000/repl-test.html

It's enough to add repl-test.html to the address bar of the opened browser.

5. Use the REPL

ClojureScript:cljs.user> (js/alert "Hello world from CLJS REPL!")

If the web browser pops an alert window up, it works.

(ns minimum-httpd
(:use [compojure.core])
(:require [compojure.route :as route]))
(defroutes app
(route/resources "/"))
(defproject repl-test "0.1.0-SNAPSHOT"
:dependencies [[compojure "1.1.5"]]
:plugins [[lein-ring "0.8.6"]
[lein-cljsbuild "0.3.2"]]
:ring {:handler minimum-httpd/app}
:cljsbuild {
:builds [{
:source-paths ["src-cljs"]
:compiler {
:output-to "resources/public/repl-test.js"
:optimizations :whitespace
:pretty-print true}}]})
(ns repl-test
(:require [clojure.browser.repl :as repl]))
(repl/connect "http://localhost:9000/repl")
<html>
<body>
<script type="text/javascript" src="repl-test.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment