Skip to content

Commit

Permalink
Fix #467: No :output-to default needed when :modules option provided
Browse files Browse the repository at this point in the history
Output files can either be specified through the upper level :output-to
option or through the :modules option. If the :modules option is used,
there will be multiple output files.
  • Loading branch information
mneise committed Jul 23, 2017
1 parent 0d085f9 commit 0291b12
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
11 changes: 7 additions & 4 deletions plugin/src/leiningen/cljsbuild/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
:crossovers []})

(defn- default-compiler-options [target-path]
{:output-to (in-target-path target-path "main.js")
:externs []
{:externs []
:libs []})

(defn- default-build-options [target-path]
Expand Down Expand Up @@ -109,8 +108,12 @@
(deep-merge a b)
b))

(defn- set-default-build-options [target-path options]
(deep-merge (default-build-options target-path) options))
(defn set-default-build-options [target-path options]
(let [options (deep-merge (default-build-options target-path) options)]
(if (or (get-in options [:compiler :output-to])
(get-in options [:compiler :modules]))
options
(assoc-in options [:compiler :output-to] (in-target-path target-path "main.js")))))

(defn- set-default-output-dirs [target-path options]
(let [output-dir-key [:compiler :output-dir]
Expand Down
17 changes: 16 additions & 1 deletion plugin/test/leiningen/test/cljsbuild/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,19 @@
(def config-out (set-compiler-global-dirs config-in))

(fact
(extract-options {:cljsbuild config-in}) => config-out)
(extract-options {:cljsbuild config-in}) => config-out)

(def modules-config {:compiler {:externs []
:libs []
:modules {:front {:output-to "lib/assets/cljs/cljs-front.js"
:entries #{"hb.front.core"}}
:extranet {:output-to "lib/assets/cljs/cljs-extranet.js"
:entries #{"hb.core"}}}}
:assert true
:incremental true
:jar false
:notify-command nil
:source-paths ["src-cljs"]})

(fact "don't set :output-to option when :modules option is provided"
(set-default-build-options target-path modules-config) => modules-config)
43 changes: 23 additions & 20 deletions support/src/cljsbuild/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,34 @@
(pst+ e))))
(println (colorizer message)))

(defn ns-from-file [f]
(try
(when (.exists f)
(with-open [rdr (io/reader f)]
(-> (java.io.PushbackReader. rdr)
read
second)))
;; better exception here eh?
(catch java.lang.RuntimeException e
nil)))
(defn get-output-files [compiler-options]
(if-let [output-file (:output-to compiler-options)]
[output-file]
(into [] (map :output-to (->> compiler-options :modules vals)))))

(defn- compile-cljs [cljs-paths compiler-options notify-command incremental? assert? watching?]
(let [output-file (:output-to compiler-options)
output-file-dir (fs/parent output-file)]
(println (str "Compiling \"" output-file "\" from " (pr-str cljs-paths) "..."))
(let [output-files (get-output-files compiler-options)
output-files-parent (map fs/parent output-files)]
(println (str "Compiling " (pr-str output-files) " from " (pr-str cljs-paths) "..."))
(flush)
(when (not incremental?)
(fs/delete-dir (:output-dir compiler-options)))
(when output-file-dir
(fs/mkdirs output-file-dir))
(doseq [output-file-parent output-files-parent]
(when output-file-parent
(fs/mkdirs output-file-parent)))
(let [started-at (System/currentTimeMillis)]
(try
(binding [*assert* assert?]
(bapi/build (apply bapi/inputs cljs-paths) compiler-options))
(fs/touch output-file started-at)
(doseq [output-file output-files]
(fs/touch output-file started-at))
(notify-cljs
notify-command
(str "Successfully compiled \"" output-file "\" in " (elapsed started-at) ".") green)
(str "Successfully compiled " (pr-str output-files) " in " (elapsed started-at) ".") green)
(catch Throwable e
(notify-cljs
notify-command
(str "Compiling \"" output-file "\" failed.") red)
(str "Compiling " (pr-str output-files) " failed.") red)
(if watching?
(pst+ e)
(throw e)))))))
Expand All @@ -88,6 +84,13 @@
[]
dependency-mtimes))

(defn get-oldest-mtime [output-files]
(apply min (map (fn [output-file]
(if (fs/exists? output-file)
(fs/mod-time output-file)
0))
output-files)))

(defn- drop-extension [path]
(let [i (.lastIndexOf path ".")]
(if (pos? i)
Expand Down Expand Up @@ -128,9 +131,9 @@
assert? last-dependency-mtimes watching?]
(let [compiler-options (merge {:output-wrapper (= :advanced (:optimizations compiler-options))}
compiler-options)
output-file (:output-to compiler-options)
output-files (get-output-files compiler-options)
lib-paths (:libs compiler-options)
output-mtime (if (fs/exists? output-file) (fs/mod-time output-file) 0)
output-mtime (get-oldest-mtime output-files)
macro-files (map :absolute crossover-macro-paths)
macro-classpath-files (into {} (map vector macro-files (map :classpath crossover-macro-paths)))
clj-files (mapcat (fn [cljs-path]
Expand Down
22 changes: 22 additions & 0 deletions support/test/cljsbuild/test/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
(def crossover-macro-paths [{:absolute crossover-macro-absolute
:classpath crossover-macro-classpath}])
(def output-to "output-to")
(def output-to-2 "output-to-2")
(def compiler-options
{:output-to output-to
:output-dir "output-dir"
Expand All @@ -38,6 +39,7 @@
(def assert? false)
(def incremental? true)
(def mtime 1234)
(def mtime-2 5678)

; TODO: We really need more tests here, particularly for the crossover/clojure reloading stuff.

Expand Down Expand Up @@ -88,3 +90,23 @@
(as-checker #(and (instance? cljs.closure.Compilable %) (instance? cljs.closure.Inputs %)))
compiler-options-with-defaults)
=> nil :times 1))

(fact "returns oldest modified time"
(get-oldest-mtime [cljs-file-a cljs-file-b]) => mtime
(provided
(fs/exists? cljs-file-a) => true
(fs/mod-time cljs-file-a) => mtime
(fs/exists? cljs-file-b) => true
(fs/mod-time cljs-file-b) => mtime-2))

(fact "should get output file provided through output-to option"
(get-output-files {:output-to output-to}) => [output-to])

(fact "should get output files provided through modules option"
(get-output-files {:modules {:front {:output-to output-to
:entries #{"hb.front.core"}}
:extranet {:output-to output-to-2
:entries #{"hb.core"}}}}) => [output-to output-to-2])

(fact "should return no output files when output-to and modules options not provided"
(get-output-files {}) => [])

0 comments on commit 0291b12

Please sign in to comment.