Skip to content

Instantly share code, notes, and snippets.

@ds604
Forked from kohyama/README.md
Created January 20, 2018 01:36
Show Gist options
  • Save ds604/2bc6a244221bc9a2a9192f58d0c8c615 to your computer and use it in GitHub Desktop.
Save ds604/2bc6a244221bc9a2a9192f58d0c8c615 to your computer and use it in GitHub Desktop.
A minimum setting to use browser REPL of ClojureScript

A minimum setting to use browser REPL of ClojureScript

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

1. Prepare files

Copy project.clj, repl-test.cljs and brepl-test.html from this gist or git clone this gist and move or copy repl-test.cljs under src directory.

$ git clone https://gist.github.com/6183122.git
$ cd 6183122/
$ mkdir src
$ mv repl-test.cljs src/

2. Compile

Compile repl-test.cljs into brepl-test.js.

$ lein cljsbuild once

Note that a JavaScript file whose name starts with repl raises an error like

Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:9000/repl-test.js".

at step 4.

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>

This repl also works as a lightweight web server serving the project directory as the document root.

4. Open the HTML file

Open brepl-test.html on a web browser via the repl web server.

http://localhost:9000/brepl-test.html

Note that openning as a local file with file: doesn't work.
Also note that a HTML file whose name starts with repl raises an error like

No 'xpc' param provided to child iframe.

Of course, it's O.K. to serve HTML and JavaScript files with whatever web server you like. In the case, HTML and JavaScript file whose name starts with repl isn't inhibitted. Refer how to use browser REPL of ClojureScript with compojure.

5. Use it

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

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

<html>
<body>
<script type="text/javascript" src="brepl-test.js"></script>
</body>
</html>
(defproject repl-test "0.1.0-SNAPSHOT"
:plugins [[lein-cljsbuild "0.3.2"]]
:cljsbuild {
:builds [{
:source-paths ["src"]
:compiler {
:output-to "brepl-test.js"}}]})
(ns repl-test
(:require [clojure.browser.repl :as repl]))
(repl/connect "http://localhost:9000/repl")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment