我在使用Figwheel和Node时,注意到时间戳源映射存在一个bug。当设置了{{:source-map-timestamp}}编译器标志时,{{sourceMappingURL}}被设置为{{source.js.map?timestamp}}。
这在使用浏览器的环境中运行正常,但在Node(从文件系统中加载文件时)中会失效。似乎一个简单的解决方案是检查{{:target}}是否是{{:node}},然后输出如{{source.js.timestamp.map}}这样的内容,并直接将其作为{{sourceMappingURL}}的值使用。
以下是我对本地{{cljs.compiler/emit-source-map}}所做的更改,允许在Node中使用时间戳时解析源映射
{code:title=emit-source-map}
(defn emit-source-map [src dest sm-data opts]
(let [timestamp (System/currentTimeMillis)
filename (str (.getPath ^File dest)
(when (and
(true? (:source-map-timestamp opts))
(= (:target opts) :nodejs))
(str "." timestamp))
".map")
sm-file (io/file filename)]
(if-let [smap (:source-map-asset-path opts)]
(emits "\n//# sourceMappingURL=" smap
(string/replace (util/path sm-file)
(str (util/path (io/file (:output-dir opts))))
"")
(if (and (true? (:source-map-timestamp opts))
(not= (:target opts) :nodejs))
(str
(if-not (string/index-of smap "?") "?" "&")
"rel=" timestamp)
n
(emits "\n//# sourceMappingURL="
(:source-map-url opts) (or (.getName sm-file))
(if (and (true? (:source-map-timestamp opts))
(not= (:target opts) :nodejs))
(str "?rel=" timestamp)
(sm/encode {(url-path src) (:source-map sm-data)}
{:lines (+ (:gen-line sm-data) 2)
:file (url-path dest)
:source-map-path (:source-map-path opts)
:source-map-timestamp (:source-map-timestamp opts)
:source-map-pretty-print (:source-map-pretty-print opts)
:relpaths {(util/path src)
(util/ns->relpath (first (:provides opts)) (:ext opts))}}))))