Skip to content

Commit 7c71115

Browse files
committed
add new main opt for starting simple web server
1 parent 298b126 commit 7c71115

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/main/clojure/cljs/cli.clj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@ present"
404404
((::compile (repl/-repl-options (repl-env)) default-compile)
405405
repl-env (merge cfg {:args args :ns ns})))
406406

407+
(defn- serve-opt
408+
[_ [_ address-port & args] {:keys [options] :as cfg}]
409+
(let [[host port] (if address-port
410+
(string/split address-port #":")
411+
["localhost" 9000])]
412+
(require 'cljs.repl.browser)
413+
((ns-resolve 'cljs.repl.browser 'serve)
414+
{:host host
415+
:port (if port
416+
(cond-> port (string? port) Integer/parseInt)
417+
9000)
418+
:output-dir (:output-dir options "out")})))
419+
407420
(defn get-options [commands k]
408421
(if (= :all k)
409422
(into (get-options commands :main) (get-options commands :init))
@@ -493,6 +506,9 @@ present"
493506
:arg "ns"
494507
:doc (str "Compile a namespace. If -r / --repl present after "
495508
"namespace will launch a REPL after the compile completes")}
509+
["-s" "--serve"] {:fn serve-opt
510+
:arg "host:port"
511+
:doc (str "Start a simple web server to serve the current directory")}
496512
[nil] {:fn null-opt}
497513
["-h" "--help" "-?"] {:fn help-opt
498514
:doc "Print this help message and exit"}}}))

src/main/clojure/cljs/repl/browser.clj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@
275275
[repl-env provides url]
276276
(browser-eval (slurp url)))
277277

278+
(defn serve [{:keys [host port output-dir] :as opts}]
279+
(println "Serving HTTP on" host "port" port)
280+
(binding [ordering (agent {:expecting nil :fns {}})
281+
es (Executors/newFixedThreadPool 16)
282+
server/state (atom {:socket nil :connection nil :promised-conn nil})]
283+
(server/start
284+
(merge opts
285+
{:static-dir (cond-> ["." "out/"] output-dir (conj output-dir))}))))
286+
278287
;; =============================================================================
279288
;; BrowserEnv
280289

@@ -360,12 +369,11 @@
360369
:launch-browser true
361370
:working-dir (->> [".repl" (util/clojurescript-version)]
362371
(remove empty?) (string/join "-"))
363-
:serve-static true
364372
:static-dir (cond-> ["." "out/"] output-dir (conj output-dir))
365373
:preloaded-libs []
366374
:src "src/"
367375
:browser-state (atom {:return-value-fn nil
368-
:client-js nil})
376+
:client-js nil})
369377
:ordering (agent {:expecting nil :fns {}})
370378
:es (Executors/newFixedThreadPool 16)
371379
:server-state
@@ -386,8 +394,6 @@
386394
working-dir: The directory where the compiled REPL client JavaScript will
387395
be stored. Defaults to \".repl\" with a ClojureScript version
388396
suffix, eg. \".repl-0.0-2138\".
389-
serve-static: Should the REPL server attempt to serve static content?
390-
Defaults to true.
391397
static-dir: List of directories to search for static content. Defaults to
392398
[\".\" \"out/\"].
393399
src: The source directory containing user-defined cljs files. Used to

0 commit comments

Comments
 (0)