Skip to content

Commit

Permalink
Use :foreign-libs :file-min if optimizations simple
Browse files Browse the repository at this point in the history
Fixes #555
  • Loading branch information
mfikes committed Nov 27, 2017
1 parent 85f87ec commit 609298d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. This change
- Update build to allow alternate `xxd -i` implementation ([#549](https://github.com/mfikes/planck/issues/549))
- Update doc site to reflect that `cljs.core/*command-line-args*` is populated.
- Update doc site Dependencies Foreign Libs CLJSJS section to use `boot`
- Use `:foreign-libs` `:file-min` if optimizations `simple` ([#555](https://github.com/mfikes/planck/issues/555))

### Fixed
- Single-dash command line args not passed to script ([#550](https://github.com/mfikes/planck/issues/550))
Expand Down
37 changes: 26 additions & 11 deletions planck-cljs/src/planck/repl.cljs
Expand Up @@ -932,14 +932,21 @@

(declare goog-dep-source)

(defn- load-minified-libs?
[opts]
(= :simple (:optimizations opts)))

;; TODO: we could be smarter and only load the libs that we haven't already loaded
(defn- load-js-lib
[name cb]
(let [sources (mapcat (fn [{:keys [file requires]}]
(concat (->> requires
(filter #(string/starts-with? % "goog."))
(map (comp goog-dep-source symbol)))
[(first (js/PLANCK_LOAD file))]))
[name opts cb]
(let [sources (mapcat (fn [{:keys [file file-min requires]}]
(let [file (or (and (load-minified-libs? opts)
file-min)
file)]
(concat (->> requires
(filter #(string/starts-with? % "goog."))
(map (comp goog-dep-source symbol)))
[(first (js/PLANCK_LOAD file))])))
(deps/js-libs-to-load name))]
(cb {:lang :js
:source (string/join "\n" sources)})
Expand Down Expand Up @@ -996,16 +1003,24 @@

; file here is an alternate parameter denoting a filesystem path
(defn- load
[{:keys [name macros path file] :as full} cb]
[{:keys [name macros path file] :as full} opts cb]
(cond
file (load-file file cb)
(skip-load? full) (cb {:lang :js
:source ""})
(re-matches #"^goog/.*" path) (load-goog name cb)
(deps/js-lib? name) (load-js-lib name cb)
(deps/js-lib? name) (load-js-lib name opts cb)
(= name 'cljs.nodejs) (load-cljs-nodejs name path cb)
:else (load-other name path macros cb)))

(defn- load-opts
[]
(select-keys @app-env [:optimizations]))

(defn- load-fn
[m cb]
(load m (load-opts) cb))

(declare skip-cljsjs-eval-error)

(defn- handle-error
Expand All @@ -1032,7 +1047,7 @@
[main-ns & args]
(let [main-args (js->clj args)
opts (make-base-eval-opts)]
(binding [cljs/*load-fn* load
(binding [cljs/*load-fn* load-fn
cljs/*eval-fn* (get-eval-fn)]
(cljs/eval st
`(~'require (quote ~(symbol main-ns)))
Expand Down Expand Up @@ -1416,7 +1431,7 @@
(defn- process-execute-path
[file opts]
(binding [theme (assoc theme :err-font (:verbose-font theme))]
(load {:file file}
(load-fn {:file file}
(fn [{:keys [lang source source-url cache]}]
(if source
(case lang
Expand Down Expand Up @@ -1843,7 +1858,7 @@
[[source-type source-value] {:keys [expression?] :as opts}]
(binding [ana/*cljs-ns* @current-ns
*ns* (create-ns @current-ns)
cljs/*load-fn* load
cljs/*load-fn* load-fn
cljs/*eval-fn* (get-eval-fn)
r/*data-readers* tags/*cljs-data-readers*]
(if-not (= "text" source-type)
Expand Down
2 changes: 1 addition & 1 deletion site/src/dependencies.md
Expand Up @@ -69,7 +69,7 @@ It is possible to use foreign libraries with Planck.

> “Foreign” libraries are implemented in a language that is not ClojureScript. (In other words, JavaScript!)
Planck will honor any `deps.cljs` files on the classpath (including those embedded in a JAR file). A `deps.cljs` file will have a [`:foreign-libs`](https://clojurescript.org/reference/compiler-options#foreign-libs) specification for foreign dependencies, essentially indicating the synthetic namespace, the JavaScript file that needs to be loaded, and an indication of any other dependencies that need to be loaded.
Planck will honor any `deps.cljs` files on the classpath (including those embedded in a JAR file). A `deps.cljs` file will have a [`:foreign-libs`](https://clojurescript.org/reference/compiler-options#foreign-libs) specification for foreign dependencies, essentially indicating the synthetic namespace, the JavaScript file that needs to be loaded, and an indication of any other dependencies that need to be loaded for each foreign lib. If specified for a given foreign lib, Planck will load `:file-min` in preference to `:file` if Planck is launched with `simple` optimizations (via `-O simple` or `--optimizations simple`).

> While `deps.cljs` files are usually bundled in JAR files in order to convey upstream foreign lib dependencies, you can also put a `deps.cljs` file directly on Planck's classpath in order to specify `:foreign-libs`. (This is useful since Planck doesn't provide a command line argument mechanism for specifying foreign libs.)
Expand Down
7 changes: 6 additions & 1 deletion site/src/performance.md
Expand Up @@ -83,8 +83,9 @@ Even with `:static-fns` enabled, unknown higher-order functions will be called u

An illustrative example is the code emitted for `(defn f [g x] (g x))`. With `:static-fns` disabled, `g.call(null,x)` is emitted for the function body. With `:static-fns` enabled, the emitted code will test determining if `g` is associated with a single-arity static dispatch implementation, and if so, call it, otherwise falling back to `g.call(null,x)`. But, with `:fn-invoke-direct`, the fallback branch will instead involve a direct call `g(x)`.

### Closure Optimizations
### Optimizations

#### Closure
You can specify the Closure compiler level to be applied to source loaded from namespaces by using `-O` or `-​-optimizations`. The allowed values are `none`, `whitespace`, and `simple`. (Planck doesn't support whole-program optimization, so `advanced` is not an option.)

Consider this example:
Expand All @@ -101,6 +102,10 @@ Furthermore, if you have caching enabled (the `-K` option above), then code is c

While enabling caching is not required, using optimizations and caching together makes sense, given that Closure optimization can take a bit of time to apply.

#### Foreign Libs

If optimizations is set to `simple`, Planck will use `:file-min` in preference to `:file` when loading foreign lib dependencies. (See the Dependencies section of this guide for more information on loading foreign lib dependencies.)

### Removing Asserts

ClojureScript allows you to embed runtime assertions into your code. Here is an example of triggering an assert at the Planck REPL:
Expand Down

0 comments on commit 609298d

Please sign in to comment.