Skip to content

Commit 1dc5b89

Browse files
committed
if source map enabled in REPL attempt to read AOTed source map
on another thread when starting up to eliminate lag on the first user error
1 parent fc8dfa4 commit 1dc5b89

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/clj/cljs/repl.clj

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,6 @@
200200
(doseq [ns (distinct requires)]
201201
(load-namespace repl-env ns opts))))
202202

203-
(defn read-source-map
204-
"Return the source map for the JavaScript source file."
205-
[f]
206-
(let [smf (io/file (str f ".map"))]
207-
(when (.exists smf)
208-
(as-> @env/*compiler* compiler-env
209-
(let [t (util/last-modified smf)]
210-
(if (> t (get-in compiler-env [::source-maps f :last-modified] 0))
211-
(swap! env/*compiler* assoc-in [::source-maps f]
212-
{:last-modified t
213-
:source-map (sm/decode (json/read-str (slurp smf) :key-fn keyword))})
214-
compiler-env))
215-
(get-in compiler-env [::source-maps f :source-map])))))
216-
217203
(defn ^File js-src->cljs-src
218204
"Map a JavaScript output file back to the original ClojureScript source
219205
file."
@@ -223,6 +209,22 @@
223209
name (.getName f)]
224210
(io/file dir (string/replace name ".js" ".cljs"))))
225211

212+
(defn read-source-map
213+
"Return the source map for the JavaScript source file."
214+
[f]
215+
(when-let [smf (util/file-or-resource (str f ".map"))]
216+
(let [ns (if (= f "cljs/core.aot.js")
217+
'cljs.core
218+
(:ns (ana/parse-ns (js-src->cljs-src f))))]
219+
(as-> @env/*compiler* compiler-env
220+
(let [t (util/last-modified smf)]
221+
(if (> t (get-in compiler-env [::source-maps ns :last-modified] 0))
222+
(swap! env/*compiler* assoc-in [::source-maps ns]
223+
{:last-modified t
224+
:source-map (sm/decode (json/read-str (slurp smf) :key-fn keyword))})
225+
compiler-env))
226+
(get-in compiler-env [::source-maps ns :source-map])))))
227+
226228
(defn ns-info
227229
"Given a path to a js source file return the ns info for the corresponding
228230
ClojureScript file if it exists."
@@ -734,6 +736,8 @@
734736
:source-map-inline source-map-inline})))
735737
done? (atom false)]
736738
(env/with-compiler-env (or compiler-env (env/default-compiler-env opts))
739+
(when (:source-map opts)
740+
(.start (Thread. (bound-fn [] (read-source-map "cljs/core.aot.js")))))
737741
(binding [*err* (if bind-err
738742
(cond-> *out*
739743
(not (instance? PrintWriter *out*)) (PrintWriter.))

src/clj/cljs/util.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@
121121
(throw
122122
(IllegalArgumentException. (str "Cannot get last modified for " src)))))
123123

124+
(defn file-or-resource [s]
125+
(or (and (.exists (io/file s)) (io/file s))
126+
(io/resource s)))
127+
124128
(defn topo-sort
125129
([x get-deps]
126130
(topo-sort x 0 (atom (sorted-map)) (memoize get-deps)))

0 commit comments

Comments
 (0)