Skip to content

Commit cddad10

Browse files
committed
Merge branch 'cljs-2615'
2 parents a095d30 + b9b1a27 commit cddad10

3 files changed

Lines changed: 55 additions & 109 deletions

File tree

src/main/clojure/cljs/build/api.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
(closure/source-for-namespace ns compiler-env)))
133133

134134
(defn add-dependencies
135-
"Given one or more IJavaScript objects in dependency order, produce
135+
"DEPRECATED: Given one or more IJavaScript objects in dependency order, produce
136136
a new sequence of IJavaScript objects which includes the input list
137137
plus all dependencies in dependency order."
138138
[opts & ijss]

src/main/clojure/cljs/closure.clj

Lines changed: 39 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,6 @@
77
; You must not remove this notice, or any other, from this software.
88

99
(ns cljs.closure
10-
"Compile ClojureScript to JavaScript with optimizations from Google
11-
Closure Compiler producing runnable JavaScript.
12-
13-
The Closure Compiler (compiler.jar) must be on the classpath.
14-
15-
Use the 'build' function for end-to-end compilation.
16-
17-
build = find-sources -> add-dependencies -> compile -> optimize -> output
18-
19-
Two protocols are defined: IJavaScript and Compilable. The
20-
Compilable protocol is satisfied by something which can return one
21-
or more IJavaScripts.
22-
23-
With IJavaScript objects in hand, calling add-dependencies will
24-
produce a sequence of IJavaScript objects which includes all
25-
required dependencies from the Closure library and ClojureScript,
26-
in dependency order. This function replaces the closurebuilder
27-
tool.
28-
29-
The optimize function converts one or more IJavaScripts into a
30-
single string of JavaScript source code using the Closure Compiler
31-
API.
32-
33-
The produced output is either a single string of optimized
34-
JavaScript or a deps file for use during development.
35-
"
3610
(:refer-clojure :exclude [compile])
3711
(:require [cljs.util :as util :refer [distinct-by]]
3812
[cljs.core :as cljsm]
@@ -546,6 +520,11 @@
546520
[compilable opts]
547521
(-compile compilable opts))
548522

523+
(defn find-sources
524+
"Given a Compilable, find sources and return a sequence of IJavaScript."
525+
[compilable opts]
526+
(-find-sources compilable opts))
527+
549528
(defn compile-file
550529
"Compile a single cljs file. If no output-file is specified, returns
551530
a string of compiled JavaScript. With an output-file option, the
@@ -724,7 +703,7 @@
724703
(js-dependencies {:libs ["closure/library/third_party/closure"]} ["goog.dom.query"])
725704
)
726705

727-
(defn- add-core-macros-if-cljs-js
706+
(defn add-core-macros-if-cljs-js
728707
"If a compiled entity is the cljs.js namespace, explicitly
729708
add the cljs.core macros namespace dependency to it."
730709
[compiled]
@@ -856,10 +835,8 @@
856835
(let [url (deps/to-url (constants-filename opts))]
857836
(javascript-file nil url [(str ana/constants-ns-sym)] ["cljs.core"])))
858837

859-
;; Internally only REPLs use this. We do expose it in cljs.build.api - David
860-
861838
(defn add-dependencies
862-
"Given one or more IJavaScript objects in dependency order, produce
839+
"DEPRECATED: Given one or more IJavaScript objects in dependency order, produce
863840
a new sequence of IJavaScript objects which includes the input list
864841
plus all dependencies in dependency order."
865842
[opts & inputs]
@@ -1065,30 +1042,6 @@
10651042
(str "#!" (or hashbang "/usr/bin/env node") "\n"))
10661043
(when preamble (preamble-from-paths preamble))))
10671044

1068-
(comment
1069-
;; add dependencies to literal js
1070-
(add-dependencies {} "goog.provide('test.app');\ngoog.require('cljs.core');")
1071-
(add-dependencies {} "goog.provide('test.app');\ngoog.require('goog.array');")
1072-
(add-dependencies {} (str "goog.provide('test.app');\n"
1073-
"goog.require('goog.array');\n"
1074-
"goog.require('clojure.set');"))
1075-
;; add dependencies with external lib
1076-
(add-dependencies {:libs ["closure/library/third_party/closure"]}
1077-
(str "goog.provide('test.app');\n"
1078-
"goog.require('goog.array');\n"
1079-
"goog.require('goog.dom.query');"))
1080-
;; add dependencies with foreign lib
1081-
(add-dependencies {:foreign-libs [{:file "samples/hello/src/hello/core.cljs"
1082-
:provides ["example.lib"]}]}
1083-
(str "goog.provide('test.app');\n"
1084-
"goog.require('example.lib');\n"))
1085-
;; add dependencies to a JavaScriptFile record
1086-
(add-dependencies {} (javascript-file false
1087-
(deps/to-url "samples/hello/src/hello/core.cljs")
1088-
["hello.core"]
1089-
["goog.array"]))
1090-
)
1091-
10921045
;; Optimize
10931046
;; ========
10941047

@@ -1438,11 +1391,6 @@
14381391

14391392
;; optimize a ClojureScript form
14401393
(optimize {:optimizations :simple} (-compile '(def x 3) {}))
1441-
1442-
;; optimize a project
1443-
(println (->> (-compile "samples/hello/src" {})
1444-
(apply add-dependencies {})
1445-
(apply optimize {:optimizations :simple :pretty-print true})))
14461394
)
14471395

14481396
;; Output
@@ -1972,24 +1920,6 @@
19721920

19731921
:else (output-deps-file opts disk-sources))))
19741922

1975-
(comment
1976-
1977-
;; output unoptimized alone
1978-
(output-unoptimized {} "goog.provide('test');\ngoog.require('cljs.core');\nalert('hello');\n")
1979-
;; output unoptimized with all dependencies
1980-
(apply output-unoptimized {}
1981-
(add-dependencies {}
1982-
"goog.provide('test');\ngoog.require('cljs.core');\nalert('hello');\n"))
1983-
;; output unoptimized with external library
1984-
(apply output-unoptimized {}
1985-
(add-dependencies {:libs ["closure/library/third_party/closure"]}
1986-
"goog.provide('test');\ngoog.require('cljs.core');\ngoog.require('goog.dom.query');\n"))
1987-
;; output unoptimized and write deps file to 'out/test.js'
1988-
(output-unoptimized {:output-to "out/test.js"}
1989-
"goog.provide('test');\ngoog.require('cljs.core');\nalert('hello');\n")
1990-
)
1991-
1992-
19931923
(defn get-upstream-deps*
19941924
"returns a merged map containing all upstream dependencies defined
19951925
by libraries on the classpath."
@@ -2706,15 +2636,17 @@
27062636
(:foreign-libs opts)))
27072637
(process-js-modules opts)
27082638
(:options @compiler-env))]
2709-
(swap! compiler-env (fn [cenv]
2710-
(-> cenv
2711-
;; we need to also track the whole top level - this is to support
2712-
;; cljs.analyze/analyze-deps, particularly in REPL contexts - David
2713-
(merge {:js-dependency-index (deps/js-dependency-index opts)})
2714-
(update-in [:node-module-index] (fnil into #{})
2715-
(if (= target :nodejs)
2716-
(map str node-required)
2717-
(map str (keys top-level)))))))
2639+
(swap! compiler-env
2640+
(fn [cenv]
2641+
(-> cenv
2642+
;; we need to also track the whole top level - this is to support
2643+
;; cljs.analyze/analyze-deps, particularly in REPL contexts - David
2644+
(merge {:js-dependency-index (deps/js-dependency-index opts)})
2645+
(update-in [:options] merge opts)
2646+
(update-in [:node-module-index] (fnil into #{})
2647+
(if (= target :nodejs)
2648+
(map str node-required)
2649+
(map str (keys top-level)))))))
27182650
opts))
27192651

27202652
(defn output-bootstrap [{:keys [target] :as opts}]
@@ -2726,6 +2658,27 @@
27262658
(util/mkdirs outfile)
27272659
(spit outfile (slurp (io/resource (str "cljs/bootstrap_" target-str ".js")))))))
27282660

2661+
(defn compile-inputs
2662+
"Compile inputs and all of their transitive dependencies including JS modules,
2663+
libs, and foreign libs. Duplicates the pipeline of build."
2664+
[inputs opts]
2665+
(env/ensure
2666+
(let [sources (-> inputs (add-dependency-sources opts))
2667+
opts (handle-js-modules opts sources env/*compiler*)
2668+
sources (-> (remove (comp #{:seed} :type) sources)
2669+
deps/dependency-order
2670+
(compile-sources false opts)
2671+
(#(map add-core-macros-if-cljs-js %))
2672+
(add-js-sources opts) deps/dependency-order
2673+
(->> (map #(source-on-disk opts %)) doall))]
2674+
sources)))
2675+
2676+
(defn compile-ns
2677+
"Compiles a namespace and all of its transitive dependencies.
2678+
See compile-inputs."
2679+
[ns opts]
2680+
(compile-inputs (find-sources ns opts) opts))
2681+
27292682
(defn build
27302683
"Given a source which can be compiled, produce runnable JavaScript."
27312684
([source opts]
@@ -2818,7 +2771,6 @@
28182771
(-> (-find-sources source opts)
28192772
(add-dependency-sources compile-opts)))
28202773
opts (handle-js-modules opts js-sources compiler-env)
2821-
_ (swap! env/*compiler* update-in [:options] merge opts)
28222774
js-sources (-> js-sources
28232775
deps/dependency-order
28242776
(compile-sources compiler-stats compile-opts)

src/main/clojure/cljs/repl.cljc

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,28 +196,19 @@
196196
only once."
197197
([repl-env ns] (load-namespace repl-env ns nil))
198198
([repl-env ns opts]
199-
(let [ns (if (and (seq? ns)
200-
(= (first ns) 'quote))
201-
(second ns)
202-
ns)
203-
;; TODO: add pre-condition to source-on-disk, the
204-
;; source must supply at least :url - David
205-
sources (binding [ana/*analyze-deps* false]
206-
(cljsc/add-dependencies
207-
(merge (env->opts repl-env) opts)
208-
{:requires [(name ns)] :type :seed}))
209-
deps (->> sources
210-
(remove (comp #{["goog"]} :provides))
211-
(remove (comp #{:seed} :type))
212-
(map #(select-keys % [:provides :url])))]
213-
(cljsc/handle-js-modules opts sources env/*compiler*)
199+
(let [ns (if (and (seq? ns) (= (first ns) 'quote)) (second ns) ns)
200+
;; We need to use a seed because many things (npm deps etc.) cannot be
201+
;; *directly* compiled, they must be a part of some ClojureScript ns
202+
;; form - thus we fabricate a seed
203+
sources (->> (cljsc/compile-inputs
204+
[{:requires [(name ns)] :type :seed}]
205+
(merge (env->opts repl-env) opts))
206+
(remove (comp #{["goog"]} :provides)))]
214207
(if (:output-dir opts)
215208
;; REPLs that read from :output-dir just need to add deps,
216209
;; environment will handle actual loading - David
217210
(let [sb (StringBuffer.)]
218-
(doseq [source (->> sources
219-
(remove (comp #{:seed} :type))
220-
(map #(cljsc/source-on-disk opts %)))]
211+
(doseq [source sources]
221212
(when (:repl-verbose opts)
222213
(println "Loading:" (:provides source)))
223214
;; Need to get :requires and :provides from compiled source
@@ -233,7 +224,7 @@
233224
(println (.toString sb)))
234225
(-evaluate repl-env "<cljs repl>" 1 (.toString sb)))
235226
;; REPLs that stream must manually load each dep - David
236-
(doseq [{:keys [url provides]} deps]
227+
(doseq [{:keys [url provides]} sources]
237228
(-load repl-env provides url))))))
238229

239230
(defn- load-dependencies
@@ -502,8 +493,11 @@
502493
ast (->ast form)
503494
ast (if-not (#{:ns :ns*} (:op ast))
504495
ast
505-
(let [ijs (ana/parse-ns [form])] ;; if ns form need to check for js modules - David
506-
(cljsc/handle-js-modules opts [ijs] env/*compiler*)
496+
(let [ijs (ana/parse-ns [form])]
497+
(cljsc/handle-js-modules opts
498+
(deps/dependency-order
499+
(cljsc/add-dependency-sources [ijs] opts))
500+
env/*compiler*)
507501
(binding [ana/*check-alias-dupes* false]
508502
(ana/no-warn (->ast form))))) ;; need new AST after we know what the modules are - David
509503
wrap-js

0 commit comments

Comments
 (0)