Permalink
Browse files

CLJS-723: add :preamble option to compiler

Per this thread:

https://groups.google.com/forum/#!searchin/clojurescript/preamble/clojurescript/rUIlLfcYZvE/Yskfh4znL_0J

1) :preamble 's value will be a vector of paths
2) the compiled output is prepended with the contents of the files at those paths
3) the generated source map points to the correct/adjusted line numbers

Additionally, when compiling for the :nodejs target the preamble
contents will default to the hashbang we currently write in that
situation.

Not entirely confident the source map-fu is the right way to do this,
but seems to work.
  • Loading branch information...
1 parent 454bcb7 commit 136bf46c656265a93dd15c40925f11edb34bd127 @travis travis committed with swannodette Dec 11, 2013
Showing with 19 additions and 7 deletions.
  1. +17 −6 src/clj/cljs/closure.clj
  2. +2 −1 src/clj/cljs/source_map.clj
View
@@ -702,6 +702,16 @@
required-cljs
inputs)))))
+(defn preamble-from-paths [paths]
+ (str (apply str (map #(slurp (io/file %)) paths)) "\n"))
+
+(defn make-preamble [{:keys [target preamble hashbang]}]
+ (if preamble
+ (preamble-from-paths preamble)
+ (if (= :nodejs target)
+ (str "#!" (or hashbang "/usr/bin/env node") "\n")
+ "")))
+
(comment
;; add dependencies to literal js
(add-dependencies {} "goog.provide('test.app');\ngoog.require('cljs.core');")
@@ -762,7 +772,9 @@
(cons "var CLOSURE_NO_DEPS = true;" sources)
sources)
^List inputs (map #(js-source-file (javascript-name %) %) sources)
- result ^Result (.compile closure-compiler externs inputs compiler-options)]
+ result ^Result (.compile closure-compiler externs inputs compiler-options)
+ preamble (make-preamble opts)
+ preamble-line-count (- (count (.split #"\r?\n" preamble -1)) 1)]
(if (.success result)
;; compiler.getSourceMap().reset()
(let [source (.toSource closure-compiler)]
@@ -805,7 +817,8 @@
merged)))
(spit (io/file name)
(sm/encode merged
- {:lines (+ (:lineCount sm-json) 2)
+ {:preamble-line-count preamble-line-count
+ :lines (+ (:lineCount sm-json) preamble-line-count 2)
:file (:file sm-json)
:output-dir (output-directory opts)
:source-map-path (:source-map-path opts)
@@ -1004,10 +1017,8 @@
(def get-upstream-deps (memoize get-upstream-deps*))
-(defn add-header [{:keys [hashbang target]} js]
- (if (= :nodejs target)
- (str "#!" (or hashbang "/usr/bin/env node") "\n" js)
- js))
+(defn add-header [opts js]
+ (str (make-preamble opts) js))
(defn add-wrapper [{:keys [output-wrapper] :as opts} js]
(if output-wrapper
@@ -181,6 +181,7 @@
(let [lines (atom [[]])
names->idx (atom {})
name-idx (atom 0)
+ preamble-lines (take (or (:preamble-line-count opts) 0) (repeat []))
info->segv
(fn [info source-idx line col]
(let [segv [(:gcol info) source-idx line col]]
@@ -221,7 +222,7 @@
#(last (string/split % #"/")))]
(map f paths)))
"lineCount" (:lines opts)
- "mappings" (->> (lines->segs @lines)
+ "mappings" (->> (lines->segs (concat preamble-lines @lines))
(map #(string/join "," %))
(string/join ";"))
"names" (into []

0 comments on commit 136bf46

Please sign in to comment.