Server-side decoding of JavaScript sourcemaps

The Closure Compiler actually has a full suite of tools for dealing with Source Maps. Unfortunately they are pretty much not documented at all so it takes a little bit of digging around.

It is however pretty easy to use once you figure out how.

(ns demo.sm-lookup
  (:require [clojure.java.io :as io])
  (:import [com.google.debugging.sourcemap SourceMapConsumerV3]))

(defn lookup [map-name line column]
  (let [sm-consumer (SourceMapConsumerV3.)
        sm-file (io/file map-name)]
    (.parse sm-consumer (slurp sm-file))
    (when-let [mapping (.getMappingForLine sm-consumer line column)]
      {:original (.getOriginalFile mapping)
       :line (.getLineNumber mapping)
       :column (.getColumnPosition mapping)}
      )))

(demo.sm-lookup/lookup "path/to/your/source.js.map" 80 1)
=> {:original "cljs/core.cljs", :line 1099, :column 1}

There is also some code in cljs.stacktrace and cljs.source-map but I don’t know anything about those.

3 Likes