Fork me on GitHub
#shadow-cljs
<
2019-08-13
>
Saikyun08:08:23

is there a way to start a shadow-cljs repl on the browser-side in an existing js-project?

Saikyun08:08:02

what I wish to do is something akin to:

let wish = require('shadow-cljs.wishful-repl');
wish.startRepl();
and then be able to connect to it using e.g. cider-mode or inf-clojure-mode, and eval cljs-code in the context of the browser page 🙂 I hope I'm making sense

thheller08:08:41

that is possible but what is the point?

thheller08:08:48

since you used require I'm going to guess that something like webpack is used

thheller08:08:08

which means all code is isolated and not accessible like CLJS code would be

thheller08:08:48

so you basically only get access to the DOM?

Saikyun08:08:52

oh, I guess that makes sense. I was hoping to do something akin to what you can do in jvm/clr, where you can start clojure and poke at the classes etc

thheller08:08:06

yeah thats not really possible in JS

Saikyun08:08:14

I figured I could do that by "exposing" variables by putting references under window

Saikyun08:08:36

e.g. window.reactRoot = ... then in the repl js/window.reactRoot

thheller08:08:37

yeah but you are much better of poking at those variables using the browser devtools

Saikyun08:08:38

or similar 🙂

Saikyun08:08:26

I generally feel very comfortable playing around with stuff using the repl, but maybe I should get more used to the devtools

Saikyun08:08:46

thanks for the input anyway 🙂

Saikyun08:08:06

also thanks for a great tool, shadow-cljs is what has made me appreciate cljs 🙂

thheller08:08:09

I don't want to stand in the way of your ideas so you could just make a regular :browser build and load that normally alongside your JS project

thheller08:08:21

BUT that will eventually cause issues with conflicts and so on

thheller08:08:32

so be very careful and expect bugs 😛

Saikyun08:08:04

ah okay, that is good to know. and thanks for the warning, haha.

Saikyun08:08:38

one of the things I had in mind is when Sean Cornfield used clojure to generate tests for java-classes/functions by speccing them. I really like spec/generation, so I figured that could be one way to do it 🙂

Saikyun08:08:08

but I'm not sure how feasible it is to do it "from the outside", as you're saying, the js-world might be too walled off 😕

Saikyun08:08:46

do you think it'd be more feasible to do it the other way around, create a shadow-cljs browser-project, and require all js-libs through cljs? or would they be equally opaque?

thheller09:08:31

equally opaque

thheller09:08:50

unlike JVM/Java the JS doesn't have a namespacing system

thheller09:08:00

so you can't really look up a class by name for example

thheller09:08:17

also there is no built in reflection/introspection like the JVM has

thheller09:08:45

you are basically just looking at arbitrary JS objects (if you can even get them)

Saikyun09:08:21

ah, I see. maybe it'd make more sense to just use a testing library built for js then

thheller09:08:07

IMHO the CLJS repl is fairly limited when it comes to inspecting pure JS objects

thheller09:08:16

much better to do that via the browser console/devtools

Saikyun10:08:22

all right @thheller. I've had some success using appliedscience/js-interop 🙂

Olical13:08:13

you might reaaaaly like this too https://github.com/mfikes/cljs-bean

Saikyun13:08:47

thanks @olical. what does this give me that js-interop doesn't? 🙂

Olical13:08:53

Oh, I'm not sure exactly, that's also a good library. But if you want an easy way to print JS objects as Clojure data structures cljs-bean should help a lot. I think it does some magic around the conversion so you can use regular CLJS data functions then convert it back to JS objects without sacrificing performance. Depends on your usecase I guess.

Saikyun13:08:44

that does sound interesting 🙂 js-interop comes with its own get-in etc

Olical13:08:43

Yep, so you can either go down the route of "leave it as JS and use functions that are aware of that fact" or the other route of "wrap it in a thin layer that lets you use all other ClojureScript functions you'd use with normal data". Honestly depends on your preference. You might find cljs-interop faster for all I know.

Olical13:08:11

I've used it before and enjoyed it, I do like the idea of only having to learn like 1-3 functions though, then I get to use all of the cljs.core functions.

Olical13:08:36

Up to you really! Just another option which might suit you better or worse.

Saikyun13:08:12

yeah, I really appreciate the suggestion 🙂 was just curious about the differences. being able to use "standard" functions sure sounds nice

Olical14:08:03

If you've got something that works, might as well stick with it. If I was starting fresh and had to do a lot of JS interop I'd be tempted by both. Just have to try it out I guess.

Saikyun14:08:37

I'm currently just dabbling with cljs, so I'm not really into any big project 😄 so trying different things out sounds fine

Olical14:08:13

Experimenting with functions and tools at the REPL is one of my favourite things about the ecosystem, playing with stuff is good 👍

tianshu18:08:22

Is my problem relative to shadow-cljs? My chrome format cljs data like this, but I want the following way

tianshu18:08:28

Sorry, I think I miss the cljs-devtools dependencies.

nate19:08:08

Hello, does anyone know if the loader code works with re-frame events ? https://shadow-cljs.github.io/docs/UsersGuide.html#_loading_code_dynamically

nate19:08:53

It doesn't seem to work to load a module dynamically and then try to dispatch a re-frame event defined in that module (for my case, calling re-frame/dispatch in the fn-to-call-on-load callback)

thheller19:08:18

@nisehl I need some more information to comment. depends on how your code is structured and so on

nate21:08:46

Hey, actually after investigating, I was passing (rf/dispatch ...) instead of a fn (eg. #(rf/dispatch ...) to the loader as a callback. My bad, all good. I confirm you can load modules that contain re-frame event definitions (eg. (rf/reg-event-fx...) dynamically