ryoakg
7/20/2017 - 2:46 AM

browser cljs nrepl

browser cljs nrepl

;;; https://cljs.github.io/api/syntax/

(js/console.log "a")
(js/console.log \a)
(js/console.log #"abc")

(js/console.log 1)
(js/console.log 1.1)
;; (js/console.log 1/2)
(js/console.log NaN)
(js/console.log Infinity)
(js/console.log -Infinity)

(js/console.log :a)
(js/console.log ::a)
(js/console.log 'a)

(js/console.log true)
(js/console.log false)

(js/console.log nil)

(js/console.log [1])
(js/console.log '(1))
(js/console.log #{1})
(js/console.log {:a 1})
(js/console.log #:: {:a 1})
(js/console.log #::cljs.user {:a 1})
#queue [1 2 3]

#inst "2000-01-10"
#inst "2000-01-10T00:00:00"
#inst "2000-01-10T00:00:00Z"
#inst "2000-01-10T00:00:00-09:00"
#inst "2000-01-10T00:00:00+09:00"

(js/console.log #js [1])
(js/console.log (clj->js [1]))
(js/console.log #js {:a 1})
(js/console.log (clj->js {:a 1}))

(js/console.log (js->clj #js [1]))
(js/console.log (js->clj #js {:a 1}))

Math/PI
js/Math.PI

;;; NullPointerException !!!
;; (require 'cljsjs.moment)
;; (.locale js/moment "fi")
;; (js/moment.locale "fi")
(set-env! :dependencies '[;; https://mvnrepository.com/artifact/com.cemerick/piggieback
                          [com.cemerick/piggieback "0.2.2"]
                          ;; https://mvnrepository.com/artifact/org.clojure/tools.nrepl
                          [org.clojure/tools.nrepl "0.2.13"]
                          ;; https://mvnrepository.com/artifact/org.clojure/clojurescript
                          [org.clojure/clojurescript "1.9.671"]

                          ;; https://clojars.org/weasel
                          [weasel "0.7.0"]

                          ;; using cljsjs
                          ;; https://clojars.org/cljsjs/moment
                          [cljsjs/moment "2.17.1-1"]
                          ])
(require '[clojure.java.io :as io])

;;; put cljs
(def cljs-file (io/file "src/repl.cljs"))
(def cljs-code
  '[(ns my.nrepl
      (:require [weasel.repl]))
    (enable-console-print!)
    (when-not (weasel.repl/alive?)
      (weasel.repl/connect "ws://localhost:9001"))])

(io/make-parents cljs-file)
(->> cljs-code
     (map (fn [code]
            (spit cljs-file code :append true)
            (spit cljs-file \newline :append true)))
     dorun)

;;; compile from cljs to js
(require 'cljs.build.api)
(cljs.build.api/build "src" {:output-to "out/main.js"
                             :main      'my.nrepl})

;;; put index.html
(merge-env! :dependencies '[[hiccup "1.0.5"]])
(require 'hiccup.core)
(->> [:html
      [:body
       [:script {:type "text/javascript" :src "out/goog/base.js"}]
       [:script {:type "text/javascript" :src "out/main.js"}]]]
     hiccup.core/html
     (spit "index.html"))

;;; open html
(require 'clojure.java.browse)
(-> (io/file (System/getProperty "user.dir") "index.html")
    io/as-url
    clojure.java.browse/browse-url)

;;; wait for connecting from the browser to dispatch cljs-code-IO
(require 'weasel.repl.websocket 'cemerick.piggieback)
(cemerick.piggieback/cljs-repl (weasel.repl.websocket/repl-env :port 9001))

;;; reload the browser to run cljs nREPL