Skip to content

Commit

Permalink
Fix module compilation when modules aren't required
Browse files Browse the repository at this point in the history
  • Loading branch information
r0man authored and dnolen committed Dec 1, 2015
1 parent f73c426 commit 8d50cd8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/main/clojure/cljs/closure.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
[cljs.env :as env]
[cljs.js-deps :as deps]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as string]
[clojure.data.json :as json])
(:import [java.io File BufferedInputStream StringWriter]
Expand Down Expand Up @@ -725,13 +726,25 @@
(find-cljs-dependencies ["cljs.core" "clojure.string"])
)

(defn- module-entries
"Return the module entries of `compile-opts` as a set."
[compile-opts]
(->> compile-opts :modules vals
(map :entries)
(remove nil?)
(apply concat)
(set)))

(defn add-dependency-sources
"Given list of IJavaScript objects, produce a new sequence of IJavaScript objects
of all dependencies of inputs."
[inputs]
(let [inputs (set inputs)
requires (set (mapcat deps/-requires inputs))]
(into inputs (find-cljs-dependencies requires))))
([inputs]
(add-dependency-sources inputs nil))
([inputs compile-opts]
(let [inputs (set inputs)
requires (set (mapcat deps/-requires inputs))
module-entries (module-entries compile-opts)]
(into inputs (find-cljs-dependencies (set/union requires module-entries))))))

(defn check-unprovided
[inputs]
Expand Down Expand Up @@ -1850,7 +1863,7 @@
(assoc all-opts :output-file (:output-to all-opts))
all-opts)
js-sources (-> (-find-sources source all-opts)
add-dependency-sources
(add-dependency-sources compile-opts)
deps/dependency-order
(compile-sources compiler-stats compile-opts)
(add-js-sources all-opts)
Expand Down
4 changes: 4 additions & 0 deletions src/test/cljs/module_test/main.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns module-test.main)

(defn ^:export main []
(println "Loading modules A and B ..."))
4 changes: 4 additions & 0 deletions src/test/cljs/module_test/modules/a.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns module-test.modules.a)

(defn ^:export main []
(println "Module A loaded."))
4 changes: 4 additions & 0 deletions src/test/cljs/module_test/modules/b.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns module-test.modules.b)

(defn ^:export main []
(println "Module B loaded."))
25 changes: 25 additions & 0 deletions src/test/clojure/cljs/build_api_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,28 @@
(build srcs opts)
(is (not (every? #(zero? (.length %)) [common-tmp app-tmp]))
"The files are not empty after compilation")))

(deftest cljs-1500-test-modules
(let [module-main (io/file "out/module-main.js")
module-a (io/file "out/module-a.js")
module-b (io/file "out/module-b.js")]
(.delete module-main)
(.delete module-a)
(.delete module-b)
(build
(inputs "src/test/cljs")
{:main "module-test.main"
:optimizations :advanced
:verbose true
:modules
{:cljs-base
{:output-to (str module-main)}
:module-a
{:output-to (str module-a)
:entries #{'module-test.modules.a}}
:module-b
{:output-to (str module-b)
:entries #{'module-test.modules.b}}}})
(is (re-find #"Loading modules A and B" (slurp module-main)))
(is (re-find #"Module A loaded" (slurp module-a)))
(is (re-find #"Module B loaded" (slurp module-b)))))

0 comments on commit 8d50cd8

Please sign in to comment.