 (defn load-all-js-files
   "Returns a chanel with one collection of loaded filenames on it."
   [files]
-  (async/into [] (async/filter< identity (async/merge (mapv reload-js-file files)))))
+  (let [out (chan)]
+    (go-loop [[f & fs] files]
+      (if-not (nil? f)
+        (do (put! out (<! (reload-js-file f)))
+            (recur fs))
+        (close! out)))
+    (async/into [] out))
+  ;; the following is a parallel load which is facter but non-deterministic
+  #_(async/into [] (async/filter< identity (async/merge (mapv reload-js-file files)))))
+
 
 (defn add-request-url [{:keys [url-rewriter] :as opts} {:keys [file] :as file-msg}]
-  (assoc file-msg :request-url
-         (if url-rewriter
-           (url-rewriter file)
-           (resolve-url file-msg))))
+  (let [resolved-path (resolve-url file-msg)]
+    (assoc file-msg :request-url
+           (if url-rewriter
+             (url-rewriter resolved-path)
+             resolved-path))))
 
 (defn add-request-urls [opts files]
   (map (partial add-request-url opts) files))


(defn reload-file [{:keys [request-url] :as file-msg} callback]
  (dev-assert (string? request-url)
              (map? file-msg)
              (not (nil? callback)))
  (utils/debug-prn (str "FigWheel: Attempting to load " request-url))
  (reload-file* request-url
                (fn [success?]

  (go
    (before-jsload files)
    ;; evaluate the eval bodies first
    ;; for now this is only for updating dependencies
    ;; we are not handling removals
    ;; TODO handle removals
    (let [eval-bodies (filter #(:eval-body %) files)]
      (when (not-empty eval-bodies)
        (doseq [eval-body-file eval-bodies]
          (eval-body eval-body-file))
        (invalidate-dependency-cache!)))

    (let [filtered-files (filter #(and (:namespace %)
                                       (not (:eval-body %)))
                                 files)
          all-files  (expand-files-to-include-deps filtered-files)
          files'  (add-request-urls opts all-files)
          res'    (<! (load-all-js-files files'))
          res     (filter :loaded-file res')
          files-not-loaded  (filter #(not (:loaded-file %)) res')]
