Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New repl-env options: inject-html and inject-scripts #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/clj/cemerick/austin.clj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ function."
(format ";console.error('Austin ClojureScript REPL session %s does not exist. Maybe you have a stale ClojureScript REPL environment in `cemerick.austin.repls/browser-repl-env`?');"
session-id)))

(defn- repl-client-code [session-id]
(-> (get @sessions session-id) :client-code))

(defn- include-js [url]
(format "<script type=\"text/javascript\" src=\"%s\"></script>" url))

(defn- send-repl-client-page
[^HttpExchange ex session-id]
(let [url (format "http://%s/%s/repl"
Expand All @@ -160,7 +166,7 @@ function."
"<script type=\"text/javascript\">
clojure.browser.repl.client.start(" (pr-str url) ");
</script>"
"</body></html>"))))
"</body></html>"))))

(defn- send-repl-index
[ex session-id]
Expand All @@ -175,8 +181,21 @@ function."
"<script type=\"text/javascript\">
clojure.browser.repl.connect(" (pr-str url) ");
</script>"
(repl-client-code session-id)
"</body></html>"))))

(defn- file-or-resource
[path]
(cond
(.exists (io/file path))
path

(re-find #"^\./" path)
(io/resource (subs path 2))

:else
(io/resource path)))

(defn- send-static
[ex session-id path]
(let [opts (get @sessions session-id)
Expand All @@ -185,8 +204,9 @@ function."
(not= "/favicon.ico" path))
(let [path (if (= "/" path) "/index.html" path)]
(if-let [local-path (seq (for [x (if (string? st-dir) [st-dir] st-dir)
:when (.exists (io/file (str x path)))]
(str x path)))]
:let [found (file-or-resource (str x path))]
:when found]
found))]
(send-response ex 200 (slurp (first local-path)) :content-type
(condp #(.endsWith %2 %1) path
".html" "text/html"
Expand Down Expand Up @@ -375,6 +395,8 @@ function."
preloaded-libs: List of namespaces that should not be sent from the REPL server
to the browser. This may be required if the browser is already
loading code and reloading it would cause a problem.
inject-html: Additional HTML code to be included in REPL page (default \"\")
inject-scripts List of additional javascript files to be loaded from repl page.
optimizations: The level of optimization to use when compiling the client
end of the REPL. Defaults to :simple.
host: The host URL on which austin will run the clojurescript repl.
Expand All @@ -392,6 +414,8 @@ function."
:serve-static true
:static-dir ["." "out/"]
:preloaded-libs []
:inject-html ""
:inject-scripts []
:src "src/"
:host "localhost"
:source-map true
Expand All @@ -414,7 +438,9 @@ function."
:loaded-libs preloaded-libs
:client-js (future (create-client-js-file
opts
(io/file (:working-dir opts) "client.js")))))
(io/file (:working-dir opts) "client.js")))
:client-code (str (apply str (map include-js (:inject-scripts opts)))
(:inject-html opts))))
(println (str "Browser-REPL ready @ " (:entry-url opts)))
opts)))

Expand Down