metasoarous / datsys

(+ clj cljs datomic datascript re-frame-esque-frp)
Eclipse Public License 1.0
231 stars 25 forks source link

Make it so figwheel is called from a stuartsierra component and only one terminal needs to be open #22

Closed bamarco closed 8 years ago

bamarco commented 8 years ago

@metasoarous in app.cljs When using a figwheel server component, start.cljs is not compiled and available for goog.require to call when the ring-handler is called because figwheel hasn't run yet. Instead, I made changes to the project.clj file so that there is an on-figwheel-reload function. Where do you want me to put the function. Is it okay in app.cljs or do you want it in its own file?

It also seems like main won't need to be called in the reload function, but I'm not sure so I have it commented out. My current implementation looks like:

(defonce system
  (do
    (log/info "Creating and starting system")
    (component/start (new-system))))

(defn ^:export main []
  (log/info "Running main function")
  (when-let [root (.getElementById js/document "app")]
    (r/render-component [views/main (:app system)] root)))

(defn on-figwheel-reload []
  (prn "------ Figwheel Has Reloaded ------")
  (do
    ;;(main)
    (dat.view/dispatch! (:app system) [:figwheel/reload nil])
    )
  )

(main)

I'm also not sure if you want me to go ahead and combine the index.html back to one file?

metasoarous commented 8 years ago

Thanks so much for working on this!

I'd rather keep as much figwheel (and other dev) logic away from app as possible. But of course, we need to inject figwheel in the system somewhere. I'd say do your best to come up with a nice separation of things, but at the end of the day just do what works.

I'm not sure what the problem is with start.cljs and goog.require; Can you clarify for me please? I think at the very least, we'd like to make it so a prod build leaves out the figwheel system component.

bamarco commented 8 years ago

@metasoarous

Yeah no problem, I'm having fun.

So if you load figwheel this way in dev/user.clj:

(ns user
  (:require
      ;; other requires...
      [dat.sys.figwheel-server :as fserver]))

(def system nil)

(defn init
  ([config-overrides]
   (alter-var-root #'system (fn [_] (assoc (system/create-system config-overrides)
                                      :http-server (component/using (fserver/new-figwheel-server) [:datomic :config :ring-routes])))))
  ([] (init {})))

Then you have a system that only loads figwheel during development, but if you try using fw/watch-and-reload you can't hardcode in the figwheel server port like start.cljs currently does because it comes from the configuration during runtime. That's okay because figwheel allows for a project.clj config parameter with a callback function :figwheel {:on-jsload "dat.sys.client.app/on-figwheel-reload"}. When I tried using dat.sys.client.start/on-figwheel-reload it couldn't find it, but now that I've explained it to you I think I could get start.cljs to work if i put the dat/sys/client/start.cljs folders in the dev folder.

bamarco commented 8 years ago

@metasoarous Okay so I figured out the problem. Figwheel doesn't do profile lein merging. I have the project.clj configured in a way that has the figwheel stuff in development only. So the current need for separate index files is no longer necessary. So the question is: Do you foresee other reasons for index.html file divergence or do you want them merged back into one?

metasoarous commented 8 years ago

Wonderful! I'd love not to have two index.html files. That's something I've been wanting to accomplish anyway. I would've avoided that myself had I written from scratch, but I inherited that setup from Rente. So yeah, merge away.

metasoarous commented 8 years ago

Hey @bamarco How do things look on this to merge? Is it running for you and are you satisfied with things?

bamarco commented 8 years ago

@metasoarous Yeah everything seems to be in order. I think it's good to go.