 
 (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")

                  ;; we need to get the server host:port args
                  (serve {:repl-env repl-env
                          :repl-options repl-options
                          :eval-str (when fw-mode? (start-figwheel-code config))
                          :join? (get b-cfg ::join-server? true)}))))))))))

(defn 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

                   (merge (build-once-opt cfg build-name)
                          {:args args})))

(declare default-output-dir default-output-to)

(defn repl-main-opt [repl-env-fn args cfg]
  (let [target-on-classpath?
        (when-let [target-dir
                   (try (:target-dir (read-string (slurp "figwheel-main.edn")) "target")
                        (catch Throwable t
                          nil))]
          (fw-util/dir-on-classpath? target-dir))
        temp-dir (when-not target-on-classpath?
                   (let [tempf (java.io.File/createTempFile "figwheel" "repl")]
                     (.delete tempf)
                     (.mkdirs tempf)
                     (.deleteOnExit (io/file tempf))
                     (fw-util/add-classpath! (.toURL (.toURI tempf)))
                     tempf))]
    (default-compile
     repl-env-fn
     (-> cfg
         (cond->
             temp-dir (assoc-in [:options :output-dir] (default-output-dir temp-dir))
             temp-dir (assoc-in [:options :output-to]  (default-output-to temp-dir))
             temp-dir (assoc-in [:options :asset-path]  "cljs-out"))
         (assoc :args args)
         (update :options (fn [opt] (merge {:main 'figwheel.repl.preload} opt)))
         (assoc-in [:repl-env-options :open-url]
                   "http://[[server-hostname]]:[[server-port]]/?figwheel-server-force-default-index=true")
         ;; TODO :default-index-body should be a function that takes the build options as an arg
         (assoc-in [:repl-env-options :default-index-body] default-main-repl-index-body)
         (assoc-in [::config :mode] :repl)
         (assoc-in [::build] {:id "figwheel-default-repl-build"})))))

(declare serve update-config)

