Skip to content

Commit fc8dfa4

Browse files
committed
CLJS-1019: REPL source map caching support
1 parent b9d6a75 commit fc8dfa4

File tree

3 files changed

+67
-62
lines changed

3 files changed

+67
-62
lines changed

src/clj/cljs/analyzer.clj

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,14 +2091,6 @@
20912091
core-cache
20922092
(io/file (str (util/to-target-file output-dir ns-info "cljs") ".cache.edn")))))
20932093

2094-
(defn last-modified [src]
2095-
(cond
2096-
(instance? File src) (.lastModified ^File src)
2097-
(instance? URL src) (.getLastModified (.openConnection ^URL src))
2098-
:else
2099-
(throw
2100-
(IllegalArgumentException. (str "Cannot get last modified for " src)))))
2101-
21022094
(defn requires-analysis?
21032095
([src] (requires-analysis? src "out"))
21042096
([src output-dir]
@@ -2118,7 +2110,7 @@
21182110
(let [out-src (util/to-target-file output-dir (parse-ns src))]
21192111
(if (not (.exists out-src))
21202112
true
2121-
(if (> (last-modified src) (last-modified cache))
2113+
(if (> (util/last-modified src) (util/last-modified cache))
21222114
true
21232115
(let [version' (util/compiled-by-version cache)
21242116
version (util/clojurescript-version)]

src/clj/cljs/repl.clj

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,14 @@
205205
[f]
206206
(let [smf (io/file (str f ".map"))]
207207
(when (.exists smf)
208-
(sm/decode (json/read-str (slurp smf) :key-fn keyword)))))
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])))))
209216

210217
(defn ^File js-src->cljs-src
211218
"Map a JavaScript output file back to the original ClojureScript source
@@ -261,58 +268,56 @@
261268
from the classpath."
262269
([stacktrace] (mapped-stacktrace stacktrace nil))
263270
([stacktrace opts]
264-
(let [read-source-map' (memoize read-source-map)
265-
ns-info' (memoize ns-info)]
266-
(vec
267-
(let [with-calls
268-
(for [{:keys [function file line column] :as frame} stacktrace]
269-
;; need to convert file, a relative URL style path, to host-specific file
270-
(let [no-source-file? (if-not file
271-
true
272-
(.startsWith file "<"))
273-
rfile (when-not no-source-file?
274-
(io/file (URL. (.toURL (io/file (util/output-directory opts))) file)))
275-
[sm {:keys [ns source-file] :as ns-info}]
276-
(when-not no-source-file?
277-
((juxt read-source-map' ns-info') rfile))
278-
[line' column' call] (if ns-info
279-
(mapped-line-column-call sm line column)
280-
[line column])
281-
name' (when (and ns-info function)
282-
function)
283-
file' (if no-source-file?
284-
file
285-
(string/replace
286-
(.getCanonicalFile
287-
(if ns-info
288-
source-file
289-
(io/file rfile)))
290-
(str (System/getProperty "user.dir") File/separator) ""))
291-
url (or (and ns-info (io/resource (util/ns->relpath ns)))
292-
(and file (io/resource file)))]
293-
(merge
294-
{:function name'
295-
:call call
296-
:file (if no-source-file?
297-
(str "NO_SOURCE_FILE"
298-
(when file
299-
(str " " file)))
300-
(io/file file'))
301-
:line line'
302-
:column column'}
303-
(when url
304-
{:url url}))))]
305-
;; take each non-nil :call and optionally merge it into :function one-level up
306-
;; to avoid replacing with local symbols, we only replace munged name if we can munge call symbol back to it
307-
(map #(merge-with (fn [munged-fn-name unmunged-call-name]
308-
(if (= munged-fn-name (string/replace (cljs.compiler/munge unmunged-call-name) "." "$"))
309-
unmunged-call-name
310-
munged-fn-name)) %1 %2)
311-
(map #(dissoc % :call) with-calls)
312-
(concat (rest (map #(if (:call %)
313-
(hash-map :function (:call %))
314-
{})
315-
with-calls)) [{}])))))))
271+
(vec
272+
(let [with-calls
273+
(for [{:keys [function file line column] :as frame} stacktrace]
274+
;; need to convert file, a relative URL style path, to host-specific file
275+
(let [no-source-file? (if-not file
276+
true
277+
(.startsWith file "<"))
278+
rfile (when-not no-source-file?
279+
(io/file (URL. (.toURL (io/file (util/output-directory opts))) file)))
280+
[sm {:keys [ns source-file] :as ns-info}]
281+
(when-not no-source-file?
282+
((juxt read-source-map ns-info) rfile))
283+
[line' column' call] (if ns-info
284+
(mapped-line-column-call sm line column)
285+
[line column])
286+
name' (when (and ns-info function)
287+
function)
288+
file' (if no-source-file?
289+
file
290+
(string/replace
291+
(.getCanonicalFile
292+
(if ns-info
293+
source-file
294+
(io/file rfile)))
295+
(str (System/getProperty "user.dir") File/separator) ""))
296+
url (or (and ns-info (io/resource (util/ns->relpath ns)))
297+
(and file (io/resource file)))]
298+
(merge
299+
{:function name'
300+
:call call
301+
:file (if no-source-file?
302+
(str "NO_SOURCE_FILE"
303+
(when file
304+
(str " " file)))
305+
(io/file file'))
306+
:line line'
307+
:column column'}
308+
(when url
309+
{:url url}))))]
310+
;; take each non-nil :call and optionally merge it into :function one-level up
311+
;; to avoid replacing with local symbols, we only replace munged name if we can munge call symbol back to it
312+
(map #(merge-with (fn [munged-fn-name unmunged-call-name]
313+
(if (= munged-fn-name (string/replace (cljs.compiler/munge unmunged-call-name) "." "$"))
314+
unmunged-call-name
315+
munged-fn-name)) %1 %2)
316+
(map #(dissoc % :call) with-calls)
317+
(concat (rest (map #(if (:call %)
318+
(hash-map :function (:call %))
319+
{})
320+
with-calls)) [{}]))))))
316321

317322
(defn print-mapped-stacktrace
318323
"Given a vector representing the canonicalized JavaScript stacktrace

src/clj/cljs/util.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@
113113
(filename x)
114114
(last (string/split (path x) #"\/"))))
115115

116+
(defn last-modified [src]
117+
(cond
118+
(file? src) (.lastModified ^File src)
119+
(url? src) (.getLastModified (.openConnection ^URL src))
120+
:else
121+
(throw
122+
(IllegalArgumentException. (str "Cannot get last modified for " src)))))
123+
116124
(defn topo-sort
117125
([x get-deps]
118126
(topo-sort x 0 (atom (sorted-map)) (memoize get-deps)))

0 commit comments

Comments
 (0)