mfikes/ambly

Adding custom cljs functions to repl via source file

zwang opened this issue · 9 comments

zwang commented

I am trying to figure out how to add custom cljs functions into the repl after it loads via source file. I searched around the wiki, issues lis and sample project, but I could not figure it out since I am still new to how clojurescript project is set up.

Can somebody point me a direction? Any help appreciated.

@zwang Can you provide an example illustrating what you are trying to accomplish?

zwang commented

@mfikes For example, I have some functions/methods in javascript context I can use to query the state of current js context and js objects in the context, I want to create a clojurescript function to call these js functions/methods so that I don't need to call these functions again and again. Instead of calling from reply everytime

(js/m3 (js/m2 (js/m1 someObject)))

I want to be able to have a function defined in a source file, i.e. cljslib.core.cljs

(defn cljsM1 [obj] 
     (js/m3 (js/m2 (js/m1 obj)))
)

So when I ran script/repl, the cljsM1 method is available to use for me on js objects in current JSContext (the js/m1, js/m3, js/m3 methods all exists in current JSContext)

This sounds a bit like :repl-requires. I wonder if that option exists, but is simply not documented here

https://github.com/clojure/clojurescript/wiki/REPL-Options

Here is the relevant bit:

https://github.com/clojure/clojurescript/blob/6dc027b961e7bfb642caf3114283ce9073dfd667/src/main/clojure/cljs/repl.cljc#L775

@zwang Here is an example. Let's say you want to always have CSV printing available via https://github.com/testdouble/clojurescript.csv

Modify your project.clj file to specify [testdouble/clojurescript.csv "0.2.0"]

Start up a Clojure REPL and then:

(require
  '[cljs.repl :as repl]
  '[ambly.core :as ambly])

Then evaluate:

(repl/repl (ambly/repl-env)
  :repl-requires
  '[[cljs.repl :refer-macros [source doc find-doc apropos dir pst]]
   [cljs.pprint :refer [pprint] :refer-macros [pp]]
   [testdouble.cljs.csv :as csv]])

All of the existing requires are preserved above and the CSV dep is added to the mix.

Then after connecting, csv will be aliased into cljs.user and you can do things like evaluate (csv/write-csv [[1 2 3] [4 5 6]]).

zwang commented

@mfikes thank you for the help. I tried the example, however, it errors out with following information

WARNING: No such namespace: csv, could not locate csv.cljs at line 1 <cljs repl>
WARNING: Use of undeclared Var csv/write-csv at line 1 <cljs repl>
ReferenceError: Can't find variable: csv global code (NO_SOURCE_FILE)

I also tried the other options in script/repl like below shows:

$COMMAND trampoline run -m clojure.main -e \
"(require '[cljs.repl :as repl])
(require '[ambly.core :as ambly])
(repl/repl (ambly/repl-env)
    :analyze-path \"src\"
    :watch \"src\"
    :repl-requires '[[pla.core :as pla]]
)"

So in above code. I have a core.cljs file under pla folder which is under src folder which is under root folder of ambly project. After the repl starts. I notice there is a pla folder created in the Ambly-xxxxxxxx volume and it contains the core.cljs as well as the compiled js file and source map file. However, in the repl I still could not use the function defined in the core.cljs folder nor the javascript function in the js file.

It seems the output js files is for the app to consume but does not get loaded to the connected JavaScriptCore Context automatically.

Any hints?

Thank you again for you help.

@zwang I can repro what you are seeing if I use an older version of ClojureScript (specifically the one specified in the Clojure/project.clj of Ambly), but If I try a newer version of ClojureScript, I can't.

zwang commented

I see. Let me try with newer version of clojurescript. Thank you for the tip. :)

zwang commented

@mfikes After I update the version of clojurescript to latest 1.7.189. It works flawlessly now. Thank you. Great post too, appreciated. 👍