Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Comprehensive enhancement to the build API #263

Merged
merged 4 commits into from
Sep 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add callback to lumo.build.api/build (#265)
This change was made necessary by the fact that the bootstrap compiler is
asynchronous by default. Therefore, it does not make sense to call
lumo.build.api/build without a callback.

The patch does not break old code, so it still contains the arity without cb,
in which case it hooks a default callback that just returns the input.
  • Loading branch information
arichiardi authored and anmonteiro committed Sep 16, 2017
commit 027fe9c487a8cbb026616633819e2f7f5fb17ce0
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Get arglists from the runtime environment ([#248](https://github.com/anmonteiro/lumo/issues/248)).
- Honor `*main-cli-fn*` ([#238](https://github.com/anmonteiro/lumo/issues/238)).
- Completions for JS namespaces ([#254](https://github.com/anmonteiro/lumo/issues/254)).
- Comprehensive enhancement to the Lumo build API ([#263](https://github.com/anmonteiro/lumo/pull/263)).
The Lumo build API has been enhanced significantly and now includes JS module
processing, preloads, and is mostly at feature parity with the JVM ClojureScript
build API.

### Changes

Expand Down
16 changes: 11 additions & 5 deletions src/cljs/bundled/lumo/build/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@
(defn build
"Given a source which can be compiled, produce runnable JavaScript."
([source opts]
(build source opts
(env/default-compiler-env
(closure/add-externs-sources opts))))
([source opts compiler-env]
(build source opts nil (fn [& args] args)))
([source opts compiler-env-or-cb]
(if (goog/isFunction compiler-env-or-cb)
(build source opts nil compiler-env-or-cb)
(build source opts compiler-env-or-cb (fn [& args] args))))
([source opts compiler-env cb]
(doseq [[unknown-opt suggested-opt] (util/unknown-opts (set (keys opts)) closure/known-opts)]
(when suggested-opt
(println (str "WARNING: Unknown compiler option '" unknown-opt "'. Did you mean '" suggested-opt "'?"))))
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
(closure/build source opts compiler-env))))
(closure/build source opts
(if (some? compiler-env)
compiler-env
(env/default-compiler-env
(closure/add-externs-sources opts))) cb))))

(defn inputs
"Given a list of directories and files, return a compilable object that may
Expand Down
45 changes: 23 additions & 22 deletions src/cljs/bundled/lumo/closure.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@
;; TODO: enable this, figure out how to get default externs
all-sources
#_(cond->
(if use-only-custom-externs
all-sources
(into all-sources (CommandLineRunner/getDefaultExterns)))
infer-externs
(conj (js-source-file nil
(io/file (util/output-directory opts) "inferred_externs.js")))))))
(if use-only-custom-externs
all-sources
(into all-sources (CommandLineRunner/getDefaultExterns)))
infer-externs
(conj (js-source-file nil
(io/file (util/output-directory opts) "inferred_externs.js")))))))

(defn make-closure-compiler []
(js/$$LUMO_GLOBALS.getGoogleClosureCompiler))
Expand Down Expand Up @@ -416,16 +416,22 @@
(merge
(javascript-file
(:foreign m)
(when-let [f (:file m)]
(when-let [f (or (:file m) (:url m))]
f)
(when-let [sf (:source-file m)]
sf)
(:provides m)
(:requires m)
(:lines m)
(:source-map m))
(when-let [source-file (:source-file m)]
{:source-file source-file})
(when-let [out-file (:out-file m)]
{:out-file out-file})
(when (:closure-lib m)
{:closure-lib true})
(when-let [ns (:ns m)]
{:ns ns})
(when (:macros-ns m)
{:macros-ns true})))

Expand Down Expand Up @@ -980,13 +986,6 @@

(defmulti javascript-name type)

(defmethod javascript-name :default [url]
(if (or (util/resource? url)
(util/bundled-resource? url)
(util/jar-resource? url))
(.-src url)
(throw (ex-info "should never happen :)" {}))))

;; TODO: this can probably be improved to account for the :url path
(defmethod javascript-name js/String [s]
(if-let [name (first (deps/-provides s))] name "cljs/user.js"))
Expand Down Expand Up @@ -1254,30 +1253,32 @@
sources (if (= :whitespace (:optimizations opts))
(cons "var CLOSURE_NO_DEPS = true;" sources)
sources)
inputs (map #(js-source-file (javascript-name %) %) sources)
inputs (doall
(map
(fn [source]
(let [source (cond-> source
(and (not (record? source)) (map? source))
map->javascript-file)]
(js-source-file (javascript-name source) source)))
sources))
_ (gobj/extend compiler-options #js {:jsCode (into-array inputs)
:externs (into-array
(map (fn [e]
#js {:src e})
(concat (:externs opts) externs)))
:externs (into-array externs)
:defines (clj->js (.-defines compiler-options))})
result (util/measure (:compiler-stats opts)
"Optimizing with Google Closure Compiler"
(closure-compiler compiler-options))]
;; FIXME: Java GClosure -> JS GClosure
(if (optimize-success? result)
;; compiler.getSourceMap().reset()
(let [source (.-compiledCode result)]
(when-let [name (:source-map opts)]
(let [name' (str name ".closure")
;; sw (StringWriter.)
;; TODO: figure out what this is doing
;; sm-json-str (do
;; (.appendTo (.getSourceMap closure-compiler) sw name')
;; (.toString sw))
]
#_(when (true? (:closure-source-map opts))
(fs.writeFileSync name' sm-json-str))
(spit (io/file name') sm-json-str))
#_(emit-optimized-source-map
(json/read-str sm-json-str :key-fn keyword)
sources name
Expand Down
2 changes: 1 addition & 1 deletion src/test/lumo/lumo/build_api_tests.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
(let [out (path/join (test/tmp-dir) "cljs-1537-test-out")
root "src/test/cljs_build"]
(test/delete-out-files out)
(closure/build
(build/build
(build/inputs
(path/join root "circular_deps" "a.cljs")
(path/join root "circular_deps" "b.cljs"))
Expand Down