Created
November 20, 2015 16:40
-
-
Save jamesnvc/6bd6dcc0aa938ed45a10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns blog.precompile | |
(:require [me.raynes.fs :as fs] | |
[cljs.closure :as cljsc] | |
[clojure.java.io :as io]) | |
(:import [javax.script ScriptEngineManager ScriptException])) | |
(defn eval-js | |
[js] | |
(let [engine (.getEngineByName (ScriptEngineManager. ) "nashorn")] | |
(.eval engine js))) | |
(defn precompile-sources | |
[] | |
(let [output-dir "out/lib-precompile"] | |
(when (fs/directory? output-dir) | |
(fs/delete-dir output-dir)) | |
(fs/mkdirs output-dir) | |
(str | |
(cljsc/build | |
'[(ns blog.precompiled | |
(:require [om.core :as om :include-macros] | |
[om.dom :as dom :include-macros] | |
cljs.reader | |
[blog.page :as b])) | |
(defn ^:export precompile | |
[app-state-str] | |
(dom/render-to-str | |
(let [app-state (cljs.reader/read-string app-state-str)] | |
(om/build | |
(case (:page-type app-state) | |
:list b/list-page-view | |
:static b/static-page-view | |
:contact-list b/contact-list-page-view | |
:item b/item-page-view | |
:feed b/feed-page-view | |
:home b/home-page-view) | |
app-state))))] | |
{:optimizations :advanced | |
:elide-asserts true | |
:preamble ["patch_react.js" "patch_misc.js" "react/react.min.js"] | |
:externs ["react/externs/react.js"] | |
:output-dir output-dir | |
:output-to "resources/precompiled.js" | |
:pretty-print false})))) | |
(defn precompile-page | |
[data] | |
(let [output-dir "out/precompile"] | |
(when (fs/directory? output-dir) | |
(fs/delete-dir output-dir)) | |
(fs/mkdirs output-dir) | |
(when-not (io/resource "precompiled.js") | |
(precompile-sources)) | |
(eval-js | |
(str (slurp (io/resource "precompiled.js")) | |
"\nblog.precompiled.precompile(" (pr-str (pr-str data)) ");")))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment