                              (fn [p]
                                (vec (distinct
                                      (concat p '[figwheel.repl.preload figwheel.core])))))
-                     #_(not (contains? opts :aot-cache))
-                     #_(assoc :aot-cache true)))
-        convey   (into [:output-dir] cljs.repl/known-repl-opts)
+                     (not (contains? opts :aot-cache))
+                     (assoc :aot-cache false)))
+        convey   (into [:output-dir :output-to] cljs.repl/known-repl-opts)
         cfg      (update cfg :options merge (select-keys opts convey))
         cfg      (update cfg :options dissoc :watch)
+
         source   (when (and (= :none (:optimizations opts :none)) main-ns)
                    (:uri (bapi/ns->location main-ns)))
         cenv     (cljs.env/default-compiler-env)]
+
     (cljs.env/with-compiler-env cenv
-      [opts options-meta]
-      #_(if-let [path (:watch opts)]
+      (if-let [path (:watch opts)]
         (do
           (bapi/build (if (coll? path)
                         (apply bapi/inputs path)

                   (cond-> opts
                     (not (:output-to opts))
                     (assoc :output-to default-output-to)
                     (= :advanced (:optimizations opts))
                     (dissoc :browser-repl)
                     (not (:output-dir opts))
                     (assoc :output-dir default-output-dir)
                     figwheel-mode?

                 (dissoc opts :watch)
                 cenv))
        (bapi/build source opts cenv))
      (when repl?
        (#'cli/repl-opt repl-env args cfg))
      (when (or serve? figwheel-mode?)
        ;; what to do when the repl isn't a figwheel repl here?
        ;; in the case where it isn't the repl we will need to start the server
        ;; directly, so perhaps start the server directly regardless

        ;; use repl to start server and setup environment for figwheel to operate in
        (let [re-opts (merge (:repl-env-options cfg)
                             (select-keys opts [:output-dir :output-to]))
              renv (apply repl-env (mapcat identity re-opts))]
          (binding [cljs.repl/*repl-env* renv]
            (cljs.repl/-setup renv (:options cfg))
            ;; TODO is this better than a direct call?
            ;; I don't think so
            (when figwheel-mode?
              (cljs.repl/evaluate-form renv
                                       "<cljs repl>"
                                       (assoc (ana/empty-env)
                                              :ns (ana/get-namespace ana/*cljs-ns*))
                                       ;; todo allow opts to be added here
                                       (ana-api/forms-seq (StringReader. "(figwheel.core/start-from-repl)"))))
            (when-let [server @(:server repl-env)]
              (.join server))))))))

(def server (atom nil))

(def ^:dynamic *config*)

;; TODO figwheel.core start when not --repl
(defn -main [& args]
  (let [[pre post] (split-with (complement #{"-re" "--repl-env"}) args)
        args (if (empty? post) (concat ["-re" "figwheel" ] args) args)]
    (with-redefs [cljs.cli/default-compile default-compile]
      (apply cljs.main/-main args))))

(def args
  (concat ["-co" "{:aot-cache false :asset-path \"out\"}" "-e" "(figwheel.core/start-from-repl)"]
          (string/split "-w src -d target/public/out -o target/public/out/mainer.js -c exproj.core -r" #"\s")))

#_(apply -main args)
