Skip to content

Optional Self hosting

David Nolen edited this page Aug 2, 2015 · 15 revisions

This page documents advice for people wanting to build ClojureScript projects that want or need to leverage cljs.js/eval-str and other self-hosted functionality.

WARNING: If you are building a web application where the final size of the deployed artifact matters significantly you should not be leveraging any of the functionality outlined on this page.

Production Builds

You can leverage :optimization :simple with builds that include the cljs.js namespace. Other recommended settings:

:pretty-print false
:optimize-constants true
:static-fns true

:optimize-constants is significant for code size, all keys and symbols in your source will be compiled into a single lookup table.

By default for ease of use cljs.js/empty-state will dump the analysis cache for cljs.core directly into the cljs.js namespace. This doubles the size of the final file. You can disable this with setting :dump-core false in your compiler build options.

This means you will need to load the analysis for core yourself. It's easy to dump the core analysis cache:

(require '[clojure.java.io :as io]
         '[cognitect.transit :as transit])
(import [java.io ByteArrayOutputStream])

(def out-path
  "../../assets/js/cljs/core.cljs.cache.aot.json")
(def out (ByteArrayOutputStream. 1000000))
(def writer (transit/writer out :json))

(def cache
  (read-string
    (slurp (io/resource "cljs/core.cljs.cache.aot.edn"))))

(transit/write writer cache)

(spit (io/file out-path) (.toString out))

for example that might look something like this:

(def st (cljs.js/empty-state))

;; path to Transit encoded analysis cache
(def cache-url "/assets/js/cljs/core.cljs.cache.aot.json")

(defn main []
  (http/get cache-url
    (fn [json]
      (let [rdr   (transit/reader :json)
            cache (transit/read rdr json)]
        (cljs.js/load-analysis-cache! st 'cljs.core cache)
        ;; ...
        ))))
Clone this wiki locally