Skip to content

Commit 90c0985

Browse files
committed
enhance -co -ro to take EDN files or resources. Can compose and
merge left to right via `:`. change cljs.closure/build so that source can truly be nil, if no sources ever supplied (no src, no main), then assume :modules is defined. Make the necessary changes required to make this work.
1 parent 853260d commit 90c0985

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed

src/main/clojure/cljs/cli.clj

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,56 @@ classpath. Classpath-relative paths have prefix of @ or @/")
172172
(let [target (if (= "node" target) "nodejs" target)]
173173
(assoc-in cfg [:options :target] (keyword target))))
174174

175+
(defn missing-file [x]
176+
(throw
177+
(ex-info
178+
(str "File " x " does not exist")
179+
{:cljs.main/error :invalid-arg})))
180+
181+
(defn missing-resource [x]
182+
(throw
183+
(ex-info
184+
(str "Resource "
185+
(if (string/starts-with? x "@/")
186+
(subs x 2)
187+
(subs x 1))
188+
" does not exist")
189+
{:cljs.main/error :invalid-arg})))
190+
191+
(defn read-edn-opts [str]
192+
(letfn [(read-rsrc [rsrc-str orig-str]
193+
(if-let [rsrc (io/resource rsrc-str)]
194+
(edn/read-string (slurp rsrc))
195+
(missing-resource orig-str)))]
196+
(cond
197+
(string/starts-with? str "@/") (read-rsrc (subs str 2) str)
198+
(string/starts-with? str "@") (read-rsrc (subs str 1) str)
199+
:else
200+
(let [f (io/file str)]
201+
(if (.exists f)
202+
(edn/read-string (slurp f))
203+
(missing-file str))))))
204+
205+
(defn load-edn-opts [str]
206+
(reduce merge {} (map read-edn-opts (string/split str #":"))))
207+
175208
(defn- repl-env-opts-opt
176209
[cfg ropts]
177-
(update cfg :repl-env-options merge (edn/read-string ropts)))
210+
(let [ropts (string/trim ropts)
211+
edn (if (string/starts-with? ropts "{")
212+
(edn/read-string ropts)
213+
(load-edn-opts ropts))]
214+
(println edn)
215+
(update cfg :repl-env-options merge edn)))
178216

179217
(defn- compile-opts-opt
180218
[cfg copts]
181-
(update cfg :options merge (edn/read-string copts)))
182-
219+
(let [copts (string/trim copts)
220+
edn (if (string/starts-with? copts "{")
221+
(edn/read-string copts)
222+
(load-edn-opts copts))]
223+
(println edn)
224+
(update cfg :options merge edn)))
183225

184226
(defn- init-opt
185227
[cfg file]
@@ -192,19 +234,9 @@ classpath. Classpath-relative paths have prefix of @ or @/")
192234
(let [f (io/file file)]
193235
(if (.exists f)
194236
f
195-
(throw
196-
(ex-info
197-
(str "File " file " does not exist")
198-
{:cljs.main/error :invalid-arg})))))]
237+
(missing-file file))))]
199238
(when-not file'
200-
(throw
201-
(ex-info
202-
(str "Resource "
203-
(if (string/starts-with? file "@/")
204-
(subs file 2)
205-
(subs file 1))
206-
" does not exist")
207-
{:cljs.main/error :invalid-arg})))
239+
(missing-resource file))
208240
(update-in cfg [:inits]
209241
(fnil conj [])
210242
{:type :init-script
@@ -320,18 +352,12 @@ present"
320352
(string/starts-with? script "@/")
321353
(if-let [rsrc (io/resource (subs script 2))]
322354
(repl/load-stream renv (util/get-name rsrc) rsrc)
323-
(throw
324-
(ex-info
325-
(str "Resource script " (subs script 2) " does not exist")
326-
{:cljs.main/error :invalid-arg})))
355+
(missing-resource script))
327356

328357
(string/starts-with? script "@")
329358
(if-let [rsrc (io/resource (subs script 1))]
330359
(repl/load-stream renv (util/get-name rsrc) rsrc)
331-
(throw
332-
(ex-info
333-
(str "Resource script " (subs script 1) " does not exist")
334-
{:cljs.main/error :invalid-arg})))
360+
(missing-resource script))
335361

336362
(string/starts-with? script "-")
337363
(throw
@@ -404,7 +430,7 @@ present"
404430
(defn default-compile
405431
[repl-env {:keys [ns args options] :as cfg}]
406432
(let [env-opts (repl/repl-options (repl-env))
407-
main-ns (symbol ns)
433+
main-ns (when ns (symbol ns))
408434
coptsf (when-let [od (:output-dir options)]
409435
(io/file od "cljsc_opts.edn"))
410436
opts (as->
@@ -416,7 +442,8 @@ present"
416442
(not (:target options))
417443
(conj :browser-repl)))
418444
options
419-
{:main main-ns}) opts
445+
(when main-ns
446+
{:main main-ns})) opts
420447
(cond-> opts
421448
(not (:output-to opts))
422449
(assoc :output-to
@@ -429,7 +456,7 @@ present"
429456
(assoc :aot-cache true)))
430457
convey (into [:output-dir] repl/known-repl-opts)
431458
cfg (update cfg :options merge (select-keys opts convey))
432-
source (when (= :none (:optimizations opts :none))
459+
source (when (and (= :none (:optimizations opts :none)) main-ns)
433460
(:uri (build/ns->location main-ns)))
434461
repl? (boolean (#{"-r" "--repl"} (first args)))
435462
serve? (boolean (#{"-s" "--serve"} (first args)))

src/main/clojure/cljs/closure.clj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@
658658
(-compile [this opts] (compile-form-seq this))
659659
(-find-sources [this opts]
660660
[(ana/parse-ns this opts)])
661+
662+
clojure.lang.IPersistentSet
663+
(-compile [this opts]
664+
(doall (map (comp #(-compile % opts) util/ns->source) this)))
665+
(-find-sources [this opts]
666+
(into [] (mapcat #(-find-sources % opts)) this))
661667
)
662668

663669
(comment
@@ -2777,7 +2783,9 @@
27772783
;; reset :js-module-index so that ana/parse-ns called by -find-sources
27782784
;; can find the missing JS modules
27792785
js-sources (env/with-compiler-env (dissoc @compiler-env :js-module-index)
2780-
(-> (-find-sources source opts)
2786+
(-> (if source
2787+
(-find-sources source opts)
2788+
(-find-sources (reduce into #{} (map (comp :entries val) (:modules opts))) opts))
27812789
(add-dependency-sources compile-opts)))
27822790
opts (handle-js-modules opts js-sources compiler-env)
27832791
js-sources (-> js-sources

0 commit comments

Comments
 (0)