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

Commit

Permalink
synchronous build API
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Sep 24, 2017
1 parent 966e6e3 commit 80192d1
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 409 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
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.
One noteworthy feature is the possibility of passing a callback to
`lumo.build.api/build`: Lumo compilation is intrinsically asynchronous,
therefore now it is possible to pass a callback.

### Changes

Expand Down
20 changes: 6 additions & 14 deletions packages/lumo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,15 @@ can basically just replace the namespace with `lumo.build.api`:
(lumo.build.api/build "src" {:output-to "out/main.js"})
```

The Lumo compiler is completely asynchronous whereas the ClojureScript compiler
is sync. This means that you can pass a callback in:
The following example also shows how to use multiple source folders.

```clojure
(require '[lumo.build.api :as api])

(api/build
(api/inputs "src1" "src2") ;; variadic
{:output-to "out/main.js"}
(fn [result]
(if-let [err (:error result)]
(println (.-stack err))
(do-something-else result))))
```
(require '[lumo.build.api :as b])

The above example also shows how to use multiple source folders. The compiler
returns a map containing an `:error` key if something went wrong.
(b/build
(b/inputs "src1" "src2") ;; variadic
{:output-to "out/main.js"})
```

## Building

Expand Down
36 changes: 17 additions & 19 deletions src/cljs/bundled/lumo/build/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,20 @@
(defn build
"Given a source which can be compiled, produce runnable JavaScript."
([source opts]
(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]
(build source opts
(if-not (nil? env/*compiler*)
env/*compiler*
(env/default-compiler-env
;; need to dissoc :foreign-libs since we won't know what overriding
;; foreign libspecs are referring to until after add-implicit-options
;; - David
(closure/add-externs-sources (dissoc opts :foreign-libs))))))
([source opts compiler-env]
(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
(if (some? compiler-env)
compiler-env
(env/default-compiler-env
(closure/add-externs-sources opts))) cb))))
(closure/build source opts compiler-env))))

(defn inputs
"Given a list of directories and files, return a compilable object that may
Expand All @@ -62,13 +61,12 @@
(-paths [_]
xs)
closure/Compilable
(-compile [_ opts cb]
(letfn [(compile-input [x cb]
(closure/-compile x opts
(fn [compiled]
(cb (if (sequential? compiled)
compiled
[compiled])))))]
(closure/mapcat-sync compile-input xs :error cb)))
(-compile [_ opts]
(letfn [(compile-input [x]
(let [compiled (closure/-compile x opts)]
(if (sequential? compiled)
compiled
[compiled])))]
(mapcat compile-input xs)))
(-find-sources [_ opts]
(mapcat #(closure/-find-sources % opts) xs))))
Loading

0 comments on commit 80192d1

Please sign in to comment.