serverless-cljs-plugin icon indicating copy to clipboard operation
serverless-cljs-plugin copied to clipboard

How To Use REPL?

Open JimLynchCodes opened this issue 8 years ago • 7 comments

Hi, I scaffold a new project with lein new serverless-cljs ok-then and cd into the directory with project.clj.

I then add one function to my core.clj like this:

(ns ok-then.core
  (:require [cljs-lambda.macros :refer-macros [defgateway]]))

(defgateway echo [event ctx]
  {:status  200
   :headers {:content-type (-> event :headers :content-type)}
   :body    (event :body)})

(defn ok []
  (println "ok"))

How can I call this function from the REPL?

JimLynchCodes avatar Nov 24 '17 17:11 JimLynchCodes

(require 'ok-then.core)
;; => No such namespace: ok-then.core, could not locate ok_then/core.cljs, ok_then/core.cljc, or JavaScript source providing "ok-then.core"

(in-ns 'ok-then.core)
;; => ok-then.core=>
(ok)
;; => WARNING: Use of undeclared Var ok-then.core/ok_ at line 1 
;;    Cannot read property 'call' of undefined

JimLynchCodes avatar Nov 24 '17 17:11 JimLynchCodes

@JimTheMan this plugin doesn't affect how your code is compiled or executed or anything like that - so do whatever you normally do to get a REPL going. The template doesn't include a dependency on the cljsbuild plugin - if that's what you're used to doing, maybe add that - or else you can use the cljs compiler API.

moea avatar Nov 24 '17 17:11 moea

Thanks but this is what I normally do to get the REPL going, but it is not working- hence why I opened this issue. Can you please post how you are doing it?

JimLynchCodes avatar Nov 24 '17 18:11 JimLynchCodes

Ah, when I cd into src directory and remove the cljs-lambda.macros then I am able to access my functions. However, I'm still unsure about other dependencies.

JimLynchCodes avatar Nov 24 '17 18:11 JimLynchCodes

The problem is that I need to be in the src directory in order to access my namespaces, but it does not see any dependencies installed through something like lein deps.

JimLynchCodes avatar Nov 24 '17 19:11 JimLynchCodes

@JimTheMan I'm not sure what you mean about "being in the src directory" - the REPL doesn't know what directory you're in, beyond the directory it was started in. I successfully did lein new serverless-cljs ok-then, started a REPL (M-x cider-jack-in-clojurescript in emacs), and added a var called ok - so I think whatever issue you are seeing is not related to this project / template.

Note that my ~/.lein/profiles.clj file looks like so:

{:user
 {:repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}
  :dependencies [[com.cemerick/piggieback "0.2.2"]]
  :plugins
  [[cider/cider-nrepl "0.15.1"]]}}

But that stuff is going to be dependent on your setup, and is just general configuration. Can you point me to another cljs project you can successfully start a REPL against? How are you starting it?

moea avatar Nov 24 '17 22:11 moea

I have also struggled with this. In the end I followed https://github.com/bhauman/lein-figwheel/wiki/Node.js-development-with-figwheel#nodejs-standalone-application-development-with-figwheel with some changes to keep it separate from the lambda function itself (as I only use it locally):

To my project.clj I have added

;...
:profiles {:dev {:dependencies [[org.clojure/tools.nrepl "0.2.12"]
                                  [com.cemerick/piggieback "0.2.2"]
                                  [figwheel-sidecar "0.5.14"]]
                   :cljsbuild
                                 {:builds
                                  {:dev
                                   {:source-paths ["src"]
                                    :figwheel true
                                    :compiler
                                                  {:main node-main
                                                   :output-to     "target/rbot-lambda/node-main-with-figwheel.js"
                                                   :output-dir    "target/rbot-lambda"
                                                   :target        :nodejs
                                                   :language-in   :ecmascript5
                                                   :optimizations :none
                                                   :source-map true
                                                   :pretty-print true}}}}
                   :source-paths ["dev"]
                   :repl-options {:init-ns user
                                  :nrepl-middleware
                                           [cemerick.piggieback/wrap-cljs-repl]
                                  }}}

and I have created dev/node_main.cljs with the same content as the ./server_src/figwheel4node_server/core.cljs in the guide, only with the namespace changed (figwheel4node-server.core -> node-main).

and then I run lein with-profile dev, repl and inside this Clojure REPL I start first figwheel, then the cljs REPL:

(require '[figwheel-sidecar.repl-api :as ra])
(ra/start-figwheel!)
(ra/cljs-repl)

the last thing i need to do is to start Node:

node target/rbot-lambda/node-main-with-figwheel.js

holyjak avatar Jan 19 '18 14:01 holyjak