|
108 | 108 | (doall
|
109 | 109 | (map #(io/resource %)
|
110 | 110 | (filter #(do
|
111 |
| - (and |
| 111 | + (and |
112 | 112 | (.startsWith % lib-path)
|
113 | 113 | (.endsWith % ".js")))
|
114 | 114 | (jar-entry-names jar-path)))))
|
|
121 | 121 | (map to-url (filter #(.endsWith (.getName %) ".js") (file-seq (io/file path)))))))
|
122 | 122 |
|
123 | 123 |
|
124 |
| -(defn find-js-classpath [path] |
| 124 | +(defn find-js-classpath |
125 | 125 | "finds all js files on the classpath matching the path provided"
|
| 126 | + [path] |
126 | 127 | (let [process-entry #(if (.endsWith % ".jar")
|
127 | 128 | (find-js-jar % path)
|
128 | 129 | (find-js-fs (str % "/" path)))
|
|
150 | 151 | Options may contain an :externs key with a list of file paths to
|
151 | 152 | load. The :use-only-custom-externs flag may be used to indicate that
|
152 | 153 | the default externs should be excluded."
|
153 |
| - [{:keys [externs use-only-custom-externs target]}] |
154 |
| - (letfn [(filter-js [paths] |
155 |
| - (for [p paths u (find-js-resources p)] u)) |
156 |
| - (add-target [ext] |
157 |
| - (if (= :nodejs target) |
158 |
| - (cons (io/resource "cljs/nodejs_externs.js") |
159 |
| - (or ext [])) |
160 |
| - ext)) |
161 |
| - (load-js [ext] |
162 |
| - (map #(js-source-file (.getFile %) (slurp %)) ext))] |
163 |
| - (let [js-sources (-> externs filter-js add-target load-js)] |
| 154 | + [{:keys [externs use-only-custom-externs target ups-externs]}] |
| 155 | + (let [filter-cp-js (fn [paths] |
| 156 | + (for [p paths u (find-js-classpath p)] u)) |
| 157 | + filter-js (fn [paths] |
| 158 | + (for [p paths u (find-js-resources p)] u)) |
| 159 | + add-target (fn [ext] |
| 160 | + (if (= :nodejs target) |
| 161 | + (cons (io/resource "cljs/nodejs_externs.js") |
| 162 | + (or ext [])) |
| 163 | + ext)) |
| 164 | + load-js (fn [ext] |
| 165 | + (map #(js-source-file (.getFile %) (slurp %)) ext))] |
| 166 | + (let [js-sources (-> externs filter-js add-target load-js) |
| 167 | + ups-sources (-> ups-externs filter-cp-js add-target load-js) |
| 168 | + all-sources (concat js-sources ups-sources)] |
| 169 | + (println all-sources) |
164 | 170 | (if use-only-custom-externs
|
165 |
| - js-sources |
166 |
| - (into js-sources (CommandLineRunner/getDefaultExterns)))))) |
| 171 | + all-sources |
| 172 | + (into all-sources (CommandLineRunner/getDefaultExterns)))))) |
167 | 173 |
|
168 | 174 | (defn ^com.google.javascript.jscomp.Compiler make-closure-compiler []
|
169 | 175 | (let [compiler (com.google.javascript.jscomp.Compiler.)]
|
|
437 | 443 | "Given a library spec (a map containing the keys :file
|
438 | 444 | and :provides), returns a map containing :provides, :requires, :file
|
439 | 445 | and :url"
|
440 |
| - [lib-spec] |
441 |
| - (merge lib-spec {:foreign true |
442 |
| - :url (find-url (:file lib-spec))})) |
| 446 | + ([lib-spec] (load-foreign-library* lib-spec false)) |
| 447 | + ([lib-spec cp-only?] |
| 448 | + (let [find-func (if cp-only? io/resource find-url)] |
| 449 | + (merge lib-spec {:foreign true |
| 450 | + :url (find-func (:file lib-spec))})))) |
443 | 451 |
|
444 | 452 | (def load-foreign-library (memoize load-foreign-library*))
|
445 | 453 |
|
446 | 454 | (defn load-library*
|
447 | 455 | "Given a path to a JavaScript library, which is a directory
|
448 | 456 | containing Javascript files, return a list of maps
|
449 | 457 | containing :provides, :requires, :file and :url."
|
450 |
| - [path] |
451 |
| - (letfn [(graph-node [u] |
452 |
| - (-> (io/reader u) |
453 |
| - line-seq |
454 |
| - parse-js-ns |
455 |
| - (assoc :url u)))] |
| 458 | + ([path] (load-library* path false)) |
| 459 | + ([path cp-only?] |
| 460 | + (let [find-func (if cp-only? find-js-classpath find-js-resources) |
| 461 | + graph-node (fn [u] |
| 462 | + (-> (io/reader u) |
| 463 | + line-seq |
| 464 | + parse-js-ns |
| 465 | + (assoc :url u)))] |
456 | 466 | (let [js-sources (find-js-resources path)]
|
457 |
| - (filter #(seq (:provides %)) (map graph-node js-sources))))) |
| 467 | + (filter #(seq (:provides %)) (map graph-node js-sources)))))) |
458 | 468 |
|
459 | 469 | (def load-library (memoize load-library*))
|
460 | 470 |
|
461 |
| -(defn library-dependencies [{:keys [libs foreign-libs]}] |
| 471 | +(defn library-dependencies [{libs :libs foreign-libs :foreign-libs |
| 472 | + ups-libs :ups-libs ups-flibs :ups-foreign-libs}] |
462 | 473 | (concat
|
| 474 | + (mapcat #(load-library % true) ups-libs) ;upstream deps |
463 | 475 | (mapcat load-library libs)
|
| 476 | + (mapcat #(load-foreign-library % true) ups-flibs) ;upstream deps |
464 | 477 | (map load-foreign-library foreign-libs)))
|
465 | 478 |
|
466 | 479 | (comment
|
|
816 | 829 | "goog.provide('test');\ngoog.require('cljs.core');\nalert('hello');\n")
|
817 | 830 | )
|
818 | 831 |
|
| 832 | + |
| 833 | +(defn get-upstream-deps* |
| 834 | + "returns a merged map containing all upstream dependencies defined by libraries on the classpath" |
| 835 | + [] |
| 836 | + (let [classloader (. (Thread/currentThread) (getContextClassLoader)) |
| 837 | + upstream-deps (map #(read-string (slurp %)) (enumeration-seq (. classloader (findResources "deps.cljs"))))] |
| 838 | + (apply merge-with concat upstream-deps))) |
| 839 | + |
| 840 | +(def get-upstream-deps (memoize get-upstream-deps*)) |
| 841 | + |
819 | 842 | (defn add-header [{:keys [hashbang target]} js]
|
820 | 843 | (if (= :nodejs target)
|
821 | 844 | (str "#!" (or hashbang "/usr/bin/nodejs") "\n" js)
|
|
827 | 850 | (let [opts (if (= :nodejs (:target opts))
|
828 | 851 | (merge {:optimizations :simple} opts)
|
829 | 852 | opts)
|
830 |
| - compiled (-compile source opts) |
| 853 | + ups-deps (get-upstream-deps) |
| 854 | + all-opts (assoc opts |
| 855 | + :ups-libs (:libs ups-deps) |
| 856 | + :ups-foreign-libs (:foreign-libs ups-deps) |
| 857 | + :ups-externs (:externs ups-deps)) |
| 858 | + compiled (-compile source all-opts) |
831 | 859 | compiled (concat
|
832 | 860 | (if (coll? compiled) compiled [compiled])
|
833 |
| - (when (= :nodejs (:target opts)) |
834 |
| - [(-compile (io/resource "cljs/nodejscli.cljs") opts)])) |
| 861 | + (when (= :nodejs (:target all-opts)) |
| 862 | + [(-compile (io/resource "cljs/nodejscli.cljs") all-opts)])) |
835 | 863 | js-sources (if (coll? compiled)
|
836 |
| - (apply add-dependencies opts compiled) |
837 |
| - (add-dependencies opts compiled))] |
838 |
| - (if (:optimizations opts) |
| 864 | + (apply add-dependencies all-opts compiled) |
| 865 | + (add-dependencies all-opts compiled))] |
| 866 | + (println "Compiler Options:" (pr-str all-opts)) |
| 867 | + (if (:optimizations all-opts) |
839 | 868 | (->> js-sources
|
840 |
| - (apply optimize opts) |
841 |
| - (add-header opts) |
842 |
| - (output-one-file opts)) |
843 |
| - (apply output-unoptimized opts js-sources)))) |
| 869 | + (apply optimize all-opts) |
| 870 | + (add-header all-opts) |
| 871 | + (output-one-file all-opts)) |
| 872 | + (apply output-unoptimized all-opts js-sources)))) |
844 | 873 |
|
845 | 874 | (comment
|
846 | 875 |
|
|
0 commit comments