
cljs.repl.browser.clj Maven / Gradle / Ivy
;; Copyright (c) Rich Hickey. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns cljs.repl.browser
(:refer-clojure :exclude [loaded-libs])
(:require [clojure.java.io :as io]
[clojure.java.browse :as browse]
[clojure.string :as string]
[clojure.edn :as edn]
[clojure.data.json :as json]
[cljs.util :as util]
[cljs.env :as env]
[cljs.closure :as cljsc]
[cljs.repl :as repl]
[cljs.cli :as cli]
[cljs.repl.server :as server]
[cljs.stacktrace :as st]
[cljs.analyzer :as ana]
[cljs.build.api :as build])
(:import [java.util.concurrent Executors]))
(def ^:dynamic browser-state nil)
(def ^:dynamic ordering nil)
(def ^:dynamic es nil)
(def ext->mime-type
{".html" "text/html"
".css" "text/css"
".jpg" "image/jpeg"
".png" "image/png"
".gif" "image/gif"
".svg" "image/svg+xml"
".js" "text/javascript"
".json" "application/json"
".clj" "text/x-clojure"
".cljs" "text/x-clojure"
".cljc" "text/x-clojure"
".edn" "text/x-clojure"
".map" "application/json"})
(def mime-type->encoding
{"text/html" "UTF-8"
"text/css" "UTF-8"
"image/jpeg" "ISO-8859-1"
"image/png" "ISO-8859-1"
"image/gif" "ISO-8859-1"
"image/svg+xml" "UTF-8"
"text/javascript" "UTF-8"
"text/x-clojure" "UTF-8"
"application/json" "UTF-8"})
(defn- set-return-value-fn
"Save the return value function which will be called when the next
return value is received."
[f]
(swap! browser-state (fn [old] (assoc old :return-value-fn f))))
(defn send-for-eval
"Given a form and a return value function, send the form to the
browser for evaluation. The return value function will be called
when the return value is received."
([form return-value-fn]
(send-for-eval @(server/connection) form return-value-fn))
([conn form return-value-fn]
(set-return-value-fn return-value-fn)
(server/send-and-close conn 200 form "text/javascript")))
(defn- return-value
"Called by the server when a return value is received."
[val]
(when-let [f (:return-value-fn @browser-state)]
(f val)))
(defn repl-client-js []
(slurp (:client-js @browser-state)))
(defn send-repl-client-page
[request conn opts]
(server/send-and-close conn 200
(str "
"
""
"")
"text/html"))
(defn default-index [output-to]
(str
""
""
""
""
""
""
""
""
""
""
"
"
"Welcome to the ClojureScript browser REPL.
"
"This page hosts your REPL and application evaluation environment. "
"Validate the connection by typing (js/alert \"Hello CLJS!\")
in the REPL.
"
"To provide your own custom page, place an index.html
file in "
"the REPL launch directory, starting with this template:
"
""
"<!DOCTYPE html>\n"
"<html>\n"
" <head>\n"
" <meta charset=\"UTF-8\">\n"
" </head>\n"
" <body>\n"
" <script src=\"" output-to "\" type=\"text/javascript\"></script>\n"
" </body>\n"
"</html>\n"
"
"
"