Skip to content

Commit dd0401b

Browse files
committed
Switch to Figwheel REPL
Having trouble getting the built-in browser REPL to pick everything up correctly. This figwheel REPL seems to work pretty well, though it relies on you having a preload to do the figwheel connection.
1 parent 48e763e commit dd0401b

File tree

5 files changed

+116
-37
lines changed

5 files changed

+116
-37
lines changed

build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ dependencies {
3434
compileOnly("nrepl:nrepl:0.9.0")
3535
compileOnly("cider:piggieback:0.5.3")
3636
devImplementation("cider:piggieback:0.5.3")
37+
38+
compileOnly("com.bhauman:figwheel-repl:0.2.18")
39+
devImplementation("com.bhauman:figwheel-repl:0.2.18")
40+
41+
compileOnly("ring:ring-jetty-adapter:1.9.5")
42+
devImplementation("ring:ring-jetty-adapter:1.9.5")
43+
compileOnly("org.eclipse.jetty.websocket:websocket-server:9.4.7.v20180619")
44+
devImplementation("org.eclipse.jetty.websocket:websocket-server:9.4.7.v20180619")
3745
}
3846

3947
tasks.withType<Test>() {
Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
(ns dev.clojurephant.tooling.api
22
(:require [dev.clojurephant.tooling.core :as core]
3+
[clojure.edn :as edn]
34
[clojure.string :as string]))
45

56
(defonce db (atom {}))
67

7-
(defn connect [dir]
8-
(swap! db update :con
9-
(fn [existing-con]
10-
(if existing-con
11-
(throw (ex-info "Must close existing connection" {}))
12-
(core/connect dir)))))
13-
14-
(defn close []
15-
(swap! db update :con
16-
(fn [existing-con]
17-
(when existing-con
18-
(.close existing-con)
19-
nil))))
20-
21-
(defn model []
8+
(defn connect! [project-dir]
9+
(swap! db (fn [{:keys [dir connection] :as current-db}]
10+
(cond
11+
(nil? connection)
12+
(merge current-db
13+
{:dir project-dir
14+
:connection (core/connect project-dir)})
15+
16+
(= dir project-dir)
17+
current-db
18+
19+
:else
20+
(throw (ex-info "Must close existing connection" {:dir dir})))))
21+
nil)
22+
23+
(defn close! []
24+
(swap! db (fn [{:keys [connection]}]
25+
(when connection
26+
(.close connection))
27+
nil))
28+
nil)
29+
30+
(defn reload-model! []
2231
(swap! db (fn [current-db]
23-
(if-let [con (:con current-db)]
32+
(if-let [con (:connection current-db)]
2433
(let [m (core/wait (core/clojurephant-model con))]
2534
(if (= :success (:result m))
26-
(assoc current-db :model (read-string (.getEdn (:value m))))
35+
(assoc current-db :model (edn/read-string (.getEdn (:value m))))
2736
(throw (ex-info "Failed to get Clojurephant model"
2837
(select-keys m [:error])))))
2938
(throw (ex-info "Cannot get model until connect" {}))))))
3039

31-
(defn reroot-cljs-build [build new-dir]
40+
(defn repl-output-dir []
41+
(-> @db :model :repl :dir))
42+
43+
(defn ^:private reroot-cljs-build [build new-dir]
3244
(let [old-dir (:output-dir build)
3345
replacer (fn [dir]
3446
(cond
@@ -40,13 +52,13 @@
4052
(update-in [:output-to] replacer)
4153
(update-in [:source-map] replacer))))
4254

43-
(defn repl-cljs-builds []
55+
(defn cljs-all-build-opts []
4456
(let [m (:model @db)
45-
repl-dir (-> m :repl :dir)
57+
repl-dir (repl-output-dir)
4658
builds (:clojurescript m)]
4759
(into {} (map (fn [[id build]]
4860
[id (reroot-cljs-build build repl-dir)]))
4961
builds)))
5062

51-
(defn repl-cljs-build [id]
52-
(get (repl-cljs-builds) id))
63+
(defn cljs-build-opts [id]
64+
(get (cljs-all-build-opts) id))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(ns dev.clojurephant.tooling.cljs
2+
(:require [dev.clojurephant.tooling.api :as api]
3+
[cljs.analyzer.api :as ana]
4+
[cljs.build.api :as build]
5+
[cljs.closure :as closure]
6+
[cljs.repl :as repl]))
7+
8+
(def registry (atom {}))
9+
10+
(defn build-opts [id]
11+
(api/cljs-build-opts id))
12+
13+
(defn compiler-env! [id]
14+
(let [opts (build-opts id)]
15+
(-> registry
16+
(swap! update-in [id :compiler-env]
17+
(fn [env] (or env (ana/empty-state opts))))
18+
(get-in [id :compiler-env]))))
19+
20+
(defn build! [id]
21+
(let [opts (build-opts id)
22+
compiler-env (compiler-env! id)]
23+
(build/build nil opts compiler-env)))
24+
25+
(defn watch! [id]
26+
(let [opts (build-opts id)
27+
compiler-env (compiler-env! id)
28+
stop (promise)]
29+
(swap! registry assoc-in [id :watch-stop] stop)
30+
(build/watch nil opts compiler-env stop)))
31+
32+
(defn stop-watch! [id]
33+
(swap! registry update-in [id :watch-stop]
34+
(fn [stop] (when stop (deliver stop true))))
35+
nil)
36+
37+
(defn clean! [id]
38+
(let [opts (build-opts id)]
39+
(stop-watch! id)
40+
(swap! registry dissoc id)
41+
;; TODO delete output-dir and output-to
42+
))
43+
44+
(defn repl-env!
45+
([id]
46+
(get-in registry [id :repl-env]))
47+
([id env]
48+
(swap! registry update-in [id :repl-env]
49+
(fn [old-env]
50+
(when old-env
51+
(repl/tear-down old-env))
52+
env))
53+
env))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(ns dev.clojurephant.tooling.figwheel-repl
2+
(:require [dev.clojurephant.tooling.api :as api]
3+
[dev.clojurephant.tooling.cljs :as cljs]
4+
[cider.piggieback :as piggie]
5+
[figwheel.repl :as repl]))
6+
7+
(defn repl-env [id]
8+
(let [opts (cljs/build-opts id)
9+
env (repl/repl-env* opts)]
10+
(cljs/repl-env! id env)))
11+
12+
(defn cljs-repl [id]
13+
(piggie/cljs-repl (repl-env id)))
14+
15+
(defn start
16+
([] (start :dev))
17+
([id]
18+
(api/connect! ".")
19+
(api/reload-model!)
20+
(cljs/build! id)
21+
(cljs-repl id)))

src/main/clojure/dev/clojurephant/tooling/repl/browser.clj

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)