Fork me on GitHub
#shadow-cljs
<
2023-01-06
>
Quentin Le Guennec09:01:58

Hello. I need to have a config.edn that will work both for backend and for frontend compile constants. What’s the ideal way to achieve this with shadow-cljs?

thheller09:01:50

define "compile constants" and why they have to be in a config.edn?

Quentin Le Guennec09:01:40

For example: • wether the app is compiled in debug mode or not • the links the app will point too (different across dev/prod environments) They need to be in a config.edn because both the backend and frontend need to access them, and different environment have different config files.

thheller10:01:12

ok, but for the backend these are not compile constants

thheller10:01:15

they are runtime configuration

thheller10:01:02

arguably for the frontend they are also runtime configuration

thheller10:01:26

whether its compiled in debug mode or not I'd say is a different problem

thheller10:01:45

what backend to talk to I personally configure at runtime not compile time

thheller10:01:12

and usually that is something my backend sends to my frontend, usually directly with the initial HTML

Quentin Le Guennec10:01:08

Yeah you’re right, for the backend it’s runtime configuration

Quentin Le Guennec10:01:57

I guess I could send it to the frontend, yes

thheller10:01:50

I mean you can also all make these compile time constants if you really wanted

Quentin Le Guennec10:01:57

but it’d be more practical having it directly compiled into the js file imo.

thheller10:01:05

for that you just need to build your own solution. since there is nothing provided out of the box for that

thheller10:01:17

I mean if they have to be in config.edn.

Quentin Le Guennec10:01:43

I guess I could have shadow cljs to read the config.edn file and then generate closure defines according to it

thheller10:01:59

are your options otherwise

thheller10:01:54

I'd say its easier and more flexible by other means. for example not having the recompile the entire CLJS code if your backend url changes. or using the same frontend build for prod and testing

thheller10:01:17

IMHO runtime configuration should not be compile contstants

beders11:01:28

Happy new Year! Patreon is being funny:

2
Tomáš Baránek15:01:07

Hi, I’m a beginner in shadow-cljs and when it comes to familiarity with clojure and clojurescript, basically too. So please be patient :) I have successfully launched a small one-page interactive application on shadow-cljs/reagent/firebase as a sort of proof-of-concept. So I am not completely stupid. But now I’m stuck. I’m trying to figure out how to get the server (localhost for now) to simply take the address URI (e.g. the “a” part from localhost:8600/a) that user entered into the browser + just dump it to my console (e.g. directly via handler function). The next step will be that I will generate additional URIs (subaddresses) based on the app’s interaction with the user etc. For now console reaction would be great. But I just get 200 OK response when entering localhost:8600/a into browser (or when contacting server via GET API) – still without any reaction from handler function. The app is barebones, it compiles, the server runs, the page renders and is displayed. But when I specify e.g. localhost:8600/a, I am unable to get the server to get the handler react. I run it using shadow-cljs watch app. Here is my shadow-cljs.edn setup:

;; shadow-cljs configuration

{:source-paths
 ["src"]

 :dependencies [[binaryage/devtools "1.0.6"]
                [reagent "0.8.0-alpha2"]
                [ring "1.9.6"]]

 :nrepl        {:port 8777}

 :builds
 
 {:app {:target :browser
        :output-dir "public/js/compiled"
        :asset-path "/js/compiled"
        
        :modules
        {:main
         {:entries [when.core]}}

        :devtools
        ;; before live-reloading any code call this function
        {:before-load when.core/stop
         ;; after live-reloading finishes call this function
         :after-load when.core/start
         ;; serve the public directory over http at port 8700
         :http-root    "public"
         :http-port    8600
         :ring-handler when.helpers/handler
         :preloads     [devtools.preload]}}}}
What I am doing wrong here? Thank you very much!

thheller15:01:26

shadow-cljs is a CLJS development tool. ring is CLJ server side, which I always recommend doing without shadow-cljs

thheller15:01:05

since you won't be using shadow-cljs in production either. it is best to just setup your own CLJ server. the CLJS setup doesn't change

thheller16:01:27

if you really want to use shadow-cljs for this then you need to use the correct http config. https://shadow-cljs.github.io/docs/UsersGuide.html#dev-http

thheller16:01:02

so just :dev-http {8600 {:root "public" :handler when.helpers/handler}} at the top level. not inside the build config, and removing the http related things from there

Tomáš Baránek16:01:48

Oh, now I understand. Thank you very much, Thomas and thank you for your great tool.

Tomáš Baránek17:01:21

I updated the .edn to

;; shadow-cljs configuration

{:source-paths
 ["src"]

 :dependencies [[binaryage/devtools "1.0.6"]
                [reagent "0.8.0-alpha2"]]

 :nrepl        {:port 8777}

 :dev-http {8600
            {:root "public"
             :handler when.helpers/handler}}

 :builds

 {:app {:target :browser
        :output-dir "public/js/compiled"
        :asset-path "/js/compiled"

        :modules
        {:main
         {:entries [when.core]}}

        :devtools
        {:before-load when.core/stop
         :after-load when.core/start
         :preloads     [devtools.preload]}}}}
and I get:
:shadow.cljs.devtools.server.dev-http/handler-load-ex - {:http-handler when.helpers/handler}
FileNotFoundException Could not locate when/helpers__init.class, when/helpers.clj or when/helpers.cljc on classpath.

...

[:app] Build failure:
The required namespace "when.helpers" is not available, it was required by "when/core.cljs".
"when/helpers.clj" was found on the classpath. Should this be a .cljs file?
during compilation.

Tomáš Baránek17:01:49

And here is my when.helpers file:

thheller17:01:16

as I said. server side ring handlers are running in CLJ thus this needs to be a .clj file not .cljs

2
🙏 2
kwladyka22:01:30

What make less issues for Cursive in practice? shadow-cljs pom vs deps.edn? Including running REPL with Crusive in Intellij.

thheller22:01:22

I never had an issue with either but deps.edn gives you more flexibility due do better :local/root support

👍 2