Skip to content

Commit 72c9dfd

Browse files
Thomas Hellerswannodette
authored andcommitted
split sm/encode into 2 functions so JSON generation is optional
1 parent d2d0316 commit 72c9dfd

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

src/main/clojure/cljs/source_map.clj

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,9 @@
214214
.toURI)]
215215
(str (.relativize source-map-parent-juri unrelativized-juri))))))
216216

217-
(defn encode
217+
(defn encode*
218218
"Take an internal source map representation represented as nested
219-
sorted maps of file, line, column and return a source map v3 JSON
220-
string."
219+
sorted maps of file, line, column and return a v3 representation."
221220
[m opts]
222221
(let [lines (atom [[]])
223222
names->idx (atom {})
@@ -252,35 +251,43 @@
252251
(doseq [[line cols] lines]
253252
(doseq [[col infos] cols]
254253
(encode-cols infos source-idx line col))))
255-
(let [source-map-file-contents
256-
(cond-> {"version" 3
257-
"file" (:file opts)
258-
"sources" (into []
259-
(let [paths (keys m)
260-
f (comp
261-
(if (true? (:source-map-timestamp opts))
262-
#(str % "?rel=" (System/currentTimeMillis))
263-
identity)
264-
(if (or (:output-dir opts)
265-
(:source-map-path opts))
266-
#(relativize-path % opts)
267-
#(last (string/split % #"/"))))]
268-
(map f paths)))
269-
"lineCount" (:lines opts)
270-
"mappings" (->> (lines->segs (concat preamble-lines @lines))
271-
(map #(string/join "," %))
272-
(string/join ";"))
273-
"names" (into []
274-
(map (set/map-invert @names->idx)
275-
(range (count @names->idx))))}
276-
(:sources-content opts)
277-
(assoc "sourcesContent" (:sources-content opts)))]
278-
(if (true? (:source-map-pretty-print opts))
279-
(with-out-str
280-
(json/pprint
281-
source-map-file-contents
282-
:escape-slash false))
283-
(json/write-str source-map-file-contents)))))
254+
255+
(cond-> {"version" 3
256+
"file" (:file opts)
257+
"sources" (into []
258+
(let [paths (keys m)
259+
f (comp
260+
(if (true? (:source-map-timestamp opts))
261+
#(str % "?rel=" (System/currentTimeMillis))
262+
identity)
263+
(if (or (:output-dir opts)
264+
(:source-map-path opts))
265+
#(relativize-path % opts)
266+
#(last (string/split % #"/"))))]
267+
(map f paths)))
268+
"lineCount" (:lines opts)
269+
"mappings" (->> (lines->segs (concat preamble-lines @lines))
270+
(map #(string/join "," %))
271+
(string/join ";"))
272+
"names" (into []
273+
(map (set/map-invert @names->idx)
274+
(range (count @names->idx))))}
275+
276+
(:sources-content opts)
277+
(assoc "sourcesContent" (:sources-content opts)))))
278+
279+
(defn encode
280+
"Take an internal source map representation represented as nested
281+
sorted maps of file, line, column and return a source map v3 JSON
282+
string."
283+
[m opts]
284+
(let [source-map-file-contents (encode* m opts)]
285+
(if (true? (:source-map-pretty-print opts))
286+
(with-out-str
287+
(json/pprint
288+
source-map-file-contents
289+
:escape-slash false))
290+
(json/write-str source-map-file-contents))))
284291

285292
;; -----------------------------------------------------------------------------
286293
;; Merging

0 commit comments

Comments
 (0)