Permalink
Browse files

Create a Nashorn engine to load JavaScript on the server.

Also add a cljsbuild target that’s compatible with Nashorn.
1 parent 1c67cd8 commit 80660eea4fdd9d3c21ca89a6b049f67a0dfc9871 @pupeno pupeno committed with pupeno Sep 8, 2015
Showing with 46 additions and 28 deletions.
  1. +22 −12 project.clj
  2. +24 −16 src/clj/projectx/handler.clj
  3. 0 src/cljc/projectx/.keep
View
@@ -40,12 +40,15 @@
:minify-assets {:assets {"resources/public/css/site.min.css" "resources/public/css/site.css"}}
- :cljsbuild {:builds {:app {:source-paths ["src/cljs" "src/cljc"]
- :compiler {:output-to "resources/public/js/app.js"
- :output-dir "resources/public/js/out"
- :asset-path "js/out"
- :optimizations :none
- :pretty-print true}}}}
+ :cljsbuild {:builds {:app {:source-paths ["src/cljs" "src/cljc"]
+ :compiler {:output-to "resources/public/js/app.js"
+ :output-dir "resources/public/js/app"
+ :asset-path "js/app"}}
+ :server-side {:source-paths ["src/cljs" "src/cljc"]
+ :compiler {:output-to "resources/public/js/server-side.js"
+ :output-dir "resources/public/js/server-side"
+ :asset-path "js/server-side"
+ :optimizations :whitespace}}}}
:profiles {:dev {:repl-options {:init-ns projectx.repl}
@@ -70,15 +73,22 @@
:env {:dev true}
- :cljsbuild {:builds {:app {:source-paths ["env/dev/cljs"]
- :compiler {:main "projectx.dev"
- :source-map true}}}}}
+ :cljsbuild {:builds {:app {:source-paths ["env/dev/cljs"]
+ :compiler {:optimizations :none
+ :source-map true
+ :pretty-print true
+ :main "projectx.dev"}}
+ :server-side {:compiler {:optimizations :whitespace
+ :source-map "resources/public/js/server-side.js.map"
+ :pretty-print true}}}}}
:uberjar {:hooks [leiningen.cljsbuild minify-assets.plugin/hooks]
:env {:production true}
:aot :all
:omit-source true
:cljsbuild {:jar true
- :builds {:app {:source-paths ["env/prod/cljs"]
- :compiler {:optimizations :advanced
- :pretty-print false}}}}}})
+ :builds {:app {:source-paths ["env/prod/cljs"]
+ :compiler {:optimizations :advanced
+ :pretty-print false}}
+ :server-side {:compiler {:optimizations :advanced
+ :pretty-print false}}}}}})
@@ -1,28 +1,36 @@
(ns projectx.handler
- (:require [compojure.core :refer [GET defroutes]]
+ (:require [clojure.java.io :as io]
+ [compojure.core :refer [GET defroutes]]
[compojure.route :refer [not-found resources]]
[ring.middleware.defaults :refer [site-defaults wrap-defaults]]
[hiccup.core :refer [html]]
[hiccup.page :refer [include-js include-css]]
[prone.middleware :refer [wrap-exceptions]]
[ring.middleware.reload :refer [wrap-reload]]
- [environ.core :refer [env]]))
+ [environ.core :refer [env]])
+ (:import [javax.script ScriptEngineManager
+ Invocable]))
(defn render-app [path]
- (html
- [:html
- [:head
- [:meta {:charset "utf-8"}]
- [:meta {:name "viewport"
- :content "width=device-width, initial-scale=1"}]
- (include-css (if (env :dev) "css/site.css" "css/site.min.css"))]
- [:body
- [:div#app
- [:h3 "ClojureScript has not been compiled!"]
- [:p "please run "
- [:b "lein figwheel"]
- " in order to start the compiler"]]
- (include-js "js/app.js")]]))
+ (let [js-engine (doto (.getEngineByName (ScriptEngineManager.) "nashorn")
+ (.eval "var global = this") ; React requires either "window" or "global" to be defined.
+ (.eval (-> "public/js/server-side.js" ; TODO: load the console polyfill, so that calling console.log is safe.
+ io/resource
+ io/reader)))]
+ (html
+ [:html
+ [:head
+ [:meta {:charset "utf-8"}]
+ [:meta {:name "viewport"
+ :content "width=device-width, initial-scale=1"}]
+ (include-css (if (env :dev) "css/site.css" "css/site.min.css"))]
+ [:body
+ [:div#app
+ [:h3 "ClojureScript has not been compiled!"]
+ [:p "please run "
+ [:b "lein figwheel"]
+ " in order to start the compiler"]]
+ (include-js "js/app.js")]])))
(defn- path [request]
(str (:uri request) ; Build the path the same way ring.util.request/request-url does it: https://github.com/ring-clojure/ring/blob/1.4.0/ring-core/src/ring/util/request.clj#L5
No changes.

0 comments on commit 80660ee

Please sign in to comment.