Skip to content

Instantly share code, notes, and snippets.

@pyrmont
Last active January 27, 2022 05:27
Show Gist options
  • Save pyrmont/73d10c74d14f26772fd276c38ee3490d to your computer and use it in GitHub Desktop.
Save pyrmont/73d10c74d14f26772fd276c38ee3490d to your computer and use it in GitHub Desktop.
Figwheel setup with Conjure
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.597"}}
:paths ["src" "resources"]
:aliases {:fig {:extra-deps
{com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}
com.bhauman/figwheel-main {:mvn/version "0.2.4"}}
:extra-paths ["target" "test"]}
:build {:main-opts ["-m" "figwheel.main" "-b" "dev" "-r"]}
:min {:main-opts ["-m" "figwheel.main" "-O" "advanced" "-bo" "dev"]}
:release {:main-opts ["-m" "figwheel.main" "-bo" "release"]}
:remote {:main-opts ["-m" "figwheel.main" "-b" "remote" "-r"]}
:nrepl {:extra-deps
{nrepl/nrepl {:mvn/version "0.7.0"}
cider/piggieback {:mvn/version "0.4.2"}
cider/cider-nrepl {:mvn/version "0.24.0"}}
:main-opts ["-m" "figwheel.nrepl" "-b" "remote"]}}}
(ns figwheel.nrepl
(:require [clojure.java.io :as io]
[clojure.string :as string]
[figwheel.main.api :as fig]
[nrepl.server :as server]))
(def port-file ".nrepl-port")
(def nrepl-port 7888)
(defonce nrepl-server (atom nil))
(defn nrepl-handler []
(require 'cider.nrepl)
(ns-resolve 'cider.nrepl 'cider-nrepl-handler))
(defn start-nrepl-server! []
(reset! nrepl-server
(server/start-server :port nrepl-port
:handler (nrepl-handler)))
(println "CIDER nREPL server started on port" nrepl-port)
(spit port-file nrepl-port))
(defn stop-nrepl-server! []
(when (not (nil? @nrepl-server))
(server/stop-server @nrepl-server)
(println "CIDER nREPL server on port" nrepl-port "stopped")
(reset! nrepl-server nil)
(io/delete-file port-file true)))
(defn parse-command-args [args]
(if (nil? args)
{}
(->> args
(reduce (fn [as a]
(if (string/starts-with? a "-")
(conj as a true)
(conj (pop as) a)))
[])
(apply hash-map))))
(defn -main [& args]
(let [switches (parse-command-args args)
build (switches "-b")]
(start-nrepl-server!)
(-> (new java.io.File port-file) (.deleteOnExit))
(fig/start {:rebel-readline false :mode :serve} build)
(fig/cljs-repl build)))
@pyrmont
Copy link
Author

pyrmont commented May 2, 2020

A couple of notes:

  1. The nrepl.clj file should be placed in src/figwheel/nrepl.clj.

  2. To connect from Conjure:

    :ConjurePiggieback (figwheel.main.api/repl-env "<build-name>")
    

The figwheel.nrepl/-main function disables rebel-readline at line 53 because I can't get rebel-readline to exit cleanly when called via the figwheel.main.api namespace. This in turn causes the nREPL port file to not be deleted. If anyone knows how to fix that, please comment!

@thiru
Copy link

thiru commented May 9, 2020

I had the same problem with Rebel-readline not exiting correctly. What worked for me was calling (System/exit 0) after starting rebel. E.g. here's what it roughly looks like in my main:

(nrepl/start!)
(fig/start "dev")
(fig/cljs-repl "dev")
(rebel/-main) ; blocking call
(System/exit 0)

@pyrmont
Copy link
Author

pyrmont commented May 27, 2020

@thiru Sorry to have taken so long to respond. Are you able to share more of the file you're running to set everything up? I wasn't able to get things to work but I suspect I didn't have all the pieces set up the way you do.

@thiru
Copy link

thiru commented Jun 4, 2020

Hey there, sorry for the late response as well. I didn't have a public repo with what I outlined. I just opened up a private repo that doesn't use Conjure yet but it has the rebel-readline bit, which is the problem anyway:

https://github.com/thiru/fileworthy2/blob/master/dev/user.clj

Note if you remove the (System/exit 0) call it hangs.

@EdwardIII
Copy link

EdwardIII commented Aug 9, 2021

Hey @thiru that file seems to have been deleted?

@thiru
Copy link

thiru commented Aug 9, 2021

Oops, I'm not sure why I put a link to a private repo! Maybe this repo will help @EdwardIII?

https://github.com/thiru/repl-base

@EdwardIII
Copy link

@thiru Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment