 
 (defn build [{:keys [watch-dirs mode ::build] :as config} options cenv]
   (let [source (when (and (= :none (:optimizations options :none)) (:main options))
-                 (:uri (bapi/ns->location (symbol (:main options)))))]
+                 (:uri (bapi/ns->location (symbol (:main options)))))
+        id (:id (::build *config*) "dev")]
     ;; TODO should probably try obtain a watch path from :main here
     ;; if watch-dirs is empty
     (if-let [paths (and (not= mode :build-once) (not-empty watch-dirs))]
       (do
-        (build-cljs (:id build "dev") (apply bapi/inputs paths) options cenv)
-        (watch-build (:id build "dev")
-                     paths options cenv (select-keys config [:reload-clj-files])))
+        (build-cljs id (apply bapi/inputs paths) options cenv)
+        (watch-build id paths options cenv (select-keys config [:reload-clj-files])))
       (cond
         source
-        (build-cljs (:id build "dev") source options cenv)
+        (build-cljs id source options cenv)
         ;; TODO need :compile-paths config param
         (not-empty watch-dirs)
-        (build-cljs (:id build "dev") (apply bapi/inputs watch-dirs) options cenv)))))
+        (build-cljs id (apply bapi/inputs watch-dirs) options cenv)))))
 
 (defn log-server-start [repl-env]
   (let [host (get-in repl-env [:ring-server-options :host] "localhost")

                                               false
                                               ansip/*use-color*)]
                   (ansip/format-str (log/format-ex except-data))))
        (flush)))))

;; TODO this needs to work in nrepl as well

                  ;; we need to get the server host:port args
                  (serve {:repl-env repl-env
                          :repl-options repl-options
                          :join? (get b-cfg ::join-server? true)}))))))))))

(defn start
  ([] (start {}))
  ([{:keys [figwheel-options build join-server?]}]
   (let [[build-id build-options] (if (map? build)
                                    [(:id build) (:options build)]
                                    [build])
         build-id (name (or build-id "dev"))
         options  (or (and (not build-options)
                           (get-build build-id))
                      build-options
                      {})
         cfg
         (cond-> {:options options
                  ::join-server? (if (true? join-server?) true false)}
           figwheel-options (assoc ::start-figwheel-options figwheel-options)
           build-id    (assoc ::build {:id  build-id
                                       :config (meta options)})
           (not (get figwheel-options :mode))
           (assoc-in [::config :mode] :repl))]
     (default-compile cljs.repl.figwheel/repl-env cfg))))

;; ----------------------------------------------------------------------------
;; REPL api
