Skip to content

Commit

Permalink
Self-host: munge file name when appending source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
arichiardi authored and dnolen committed Feb 18, 2018
1 parent 06e591d commit c6961d5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main/cljs/cljs/js.cljs
Expand Up @@ -157,9 +157,11 @@
(defn- append-source-map
[state name source sb sm-data {:keys [output-dir asset-path source-map-timestamp] :as opts}]
(let [t (.valueOf (js/Date.))
smn (if name
(string/replace (munge (str name)) "." "/")
mn (if name
(munge (str name))
(str "cljs-" t))
smn (cond-> mn
name (string/replace "." "/"))
ts (.valueOf (js/Date.))
out (or output-dir asset-path)
src (cond-> (str smn ".cljs")
Expand All @@ -173,7 +175,7 @@
:file file :sources-content [source]})]
(when (:verbose opts) (debug-prn json))
(swap! state assoc-in
[:source-maps name] (sm/invert-reverse-map (:source-map sm-data)))
[:source-maps (symbol mn)] (sm/invert-reverse-map (:source-map sm-data)))
(.append sb
(str "\n//# sourceURL=" file
"\n//# sourceMappingURL=data:application/json;base64,"
Expand Down Expand Up @@ -969,7 +971,7 @@
the ClojureScript source
name (symbol or string)
optional, the name of the source
optional, the name of the source - used as key in :source-maps
opts (map)
compilation options.
Expand Down Expand Up @@ -1134,7 +1136,7 @@
the ClojureScript source
name (symbol or string)
optional, the name of the source
optional, the name of the source - used as key in :source-maps
opts (map)
compilation options.
Expand Down
85 changes: 85 additions & 0 deletions src/test/self/self_host/test.cljs
Expand Up @@ -12,6 +12,7 @@
[cljs.js :as cljs]
[cljs.analyzer :as ana]
[clojure.string :as string]
[cljs.stacktrace :as st]
[cljs.nodejs :as nodejs]))

(set! (.-user js/cljs) #js {})
Expand Down Expand Up @@ -1360,6 +1361,90 @@
(is (= 4 value))
(inc! l))))))))

(deftest test-mapping-stacktrace
(async done
(let [l (latch 1 done)]
(testing "it should correctly map from canonical representation (smoke test)."
(let [st (cljs/empty-state)]
(cljs/eval-str st
"(ns cljs.user (:require foo.bar :reload))"
'cljs.user
{:source-map true
:ns 'cljs.user
:target :nodejs
:eval node-eval
:load (fn [{:keys [name]} cb]
(cb (when (= name 'foo.bar)
{:lang :clj
:source "(ns foo.bar)\n(defn broken-first [] (ffirst 0))"
:file "foo/bar.cljs"})))}
(fn [{:keys [ns value error file]}]
(let [sms (:source-maps @st)]
(is (= [{:function "broken-first"
:file "foo/bar.cljs"
:line 2
:column 7}]
(st/mapped-stacktrace
[{:file "foo/bar.js"
:function "broken-first"
:line 2
:column 0}]
sms)))))))))))

(deftest test-mapping-stacktrace-with-underscore
(async done
(let [l (latch 1 done)]
(testing "it should correctly map when file names contain underscore"
(let [st (cljs/empty-state)]
(cljs/eval-str st
"(ns cljs.user (:require foo.with-underscore :reload))"
'cljs.user
{:source-map true
:ns 'cljs.user
:target :nodejs
:eval node-eval
:load (fn [{:keys [name]} cb]
(cb (when (= name 'foo.with-underscore)
{:lang :clj
:source "(ns foo.with-underscore)\n(defn broken-first [] (ffirst 0))"
:file "foo/with_underscore.cljs"})))}
(fn [{:keys [ns value error file]}]
(let [sms (:source-maps @st)]
(is (= [{:function "broken-first"
:file "foo/with_underscore.cljs"
:line 2
:column 7}]
(st/mapped-stacktrace
[{:file "foo/with_underscore.js"
:function "broken-first"
:line 2
:column 0}]
sms)))))))))))

(deftest test-append-source-map-with-nil-name
(async done
(let [
l (latch 1 done)]
(testing "it should correctly use cljs-{js/Date as number} when name to cljs.js/eval-str is nil"
(let [st (cljs/empty-state)]
(cljs/eval-str st
"(ns cljs.user (:require foo.bar :reload))"
nil
{:source-map true
:ns 'cljs.user
:target :nodejs
:eval node-eval
:load (fn [{:keys [name]} cb]
(cb (when (= name 'foo.bar)
{:lang :clj
:source "(ns foo.bar)"
:file "foo/bar.cljs"})))}
(fn [{:keys [ns value error file]}]
(let [cljs-timestamp? #(let [[c t] (string/split % "-")]
(and (= "cljs" c) (not (js/isNaN (js/parseInt t)))))
sms (:source-maps @st)]
(is (some cljs-timestamp? (keys sms)))))))))))

(defn -main [& args]
(run-tests))

Expand Down

0 comments on commit c6961d5

Please sign in to comment.