Fork me on GitHub
#shadow-cljs
<
2019-04-03
>
markx01:04:07

Hello! Is there a way to eval in cljs on node?

markx04:04:24

Well, I mean, cljs doesn’t have it. Is it possible that shadow-cljs provide this function?

markx16:04:19

Kinda, but not really. Current I have the need to dynamically load code source at runtime. There are clojure.core/load and clojure.core/load-file, but not available in cljs. So I’m wondering if this is possible via shadow-cljs.

lilactown16:04:12

in order to evaluate code at runtime, you have to follow the steps in that blog post

lilactown16:04:17

normally the CLJS compiler is not included in your bundle. in order to use eval, you have to include it

markx22:04:51

Great, so it could eval. Does it still only support browser though? I’ll need to use it on nodejs to load local files.

thheller22:04:37

I added support for node. you just use the shadow.cljs.bootstrap.node ns instead of the browser one

thheller22:04:20

(ns demo.bootstrap-script
  (:require
    [cljs.js :as cljs]
    [cljs.env :as env]
    [shadow.cljs.bootstrap.node :as boot]))

(defn print-result [{:keys [error value] :as result}]
  (prn [:result result]))

(def code "(prn ::foo) (+ 1 2)")

(defonce compile-state-ref (env/default-compiler-env))

(defn compile-it []
  (cljs/eval-str
    compile-state-ref
    code
    "[test]"
    {:eval cljs/js-eval
     :load (partial boot/load compile-state-ref)}
    print-result))

(defn main [& args]
  (boot/init compile-state-ref {} compile-it))

thheller22:04:02

:bootstrap-script-host
  {:target :node-script
   :main demo.bootstrap-script/main
   :output-to "out/demo-bootstrap/script.js"

   :compiler-options
   {:optimizations :simple}

   :devtools
   {:enabled false}}

  :bootstrap-script-support
  {:target :bootstrap
   :output-dir "out/demo-bootstrap/bootstrap"
   :exclude #{cljs.js}
   :entries [cljs.js demo.macro]
   :macros []
   :js-options
   {:js-provider :require}}

markx22:04:21

That could work. I’ll try. Thanks!

poernahi15:04:13

{:deps {:aliases [:dev :test]}
 :builds ...}
If I am using deps.edn and define multiple aliases, which one is used for development and which one is used for release build? For example, if I have cider-nrepl in the dev alias, will it get included in release build? From my experiment, it looks like shadow-cljs is doing the right thing, but would like to know how.

lilactown15:04:14

@poernahi shadow-cljs release uses the same classpath as for development

lilactown15:04:27

anything that's not referenced in your CLJS files will not be included

lilactown15:04:47

in this case, cider-nrepl is a Clojure dep so there's no way it will end up in your release 😉

robert.law16:04:34

Hi! Is there a way of pulling clj/cljs dependencies from private s3 repos? I've looked for something like this in doc but cannot see anything

:repos
               {"s3-private-snapshot" {:url ""}
                "s3-private-release" {:url ""}}

poernahi16:04:20

@lilactown thanks!

thheller17:04:17

@robert.law yes, use :repositories not :repos though. same as in lein. this is the lib used for that https://github.com/s3-wagon-private/s3-wagon-private

robert.law18:04:31

thanks that worked

markx22:04:18

Currently I’m trying to build an web app, including both clojure backend and cljs SPA. I think the backend will already have a http server for the api, so I’m looking to use this server to host static assets too, e.g. js files. But on dev, I also want shadow-cljs’ dev http server, so I can have hot-loading and everything. Is it possible to reuse my clojure http server as the shadow-cljs dev server?

thheller22:04:01

the hot-reloading is not coupled to the dev http server

thheller22:04:07

it works with any webserver

thheller22:04:23

so just make your CLJ server serve the static files and you are good to go

markx22:04:25

wow that’s great! Is there any magic in the dev http server?

thheller22:04:41

safe caching defaults I guess

thheller22:04:07

usual CLJ servers cache too much which is a bit annoying sometimes

markx22:04:31

So we should still prefer the dev server, if we can?

thheller22:04:15

you can use a setup like this

thheller22:04:17

:dev-http
 {8081
  {:root "public"
   :proxy-url ""}}

thheller22:04:38

the CLJ server is running on :8080 and serves everything BUT the static files

thheller22:04:56

so the caching rules of the dev server apply for the static files

thheller22:04:57

you could of course just set the proper cache rules for your server

thheller22:04:45

its just setting the cache-control: private, no-cache header

thheller22:04:54

should be easy enough with most CLJ servers

thheller23:04:28

but really the dev http servers just serve files

thheller23:04:33

nothing special about the server at all

thheller23:04:56

the websocket stuff is handled by the server running on :9630, nothing to do with the dev http server

markx23:04:25

Great! I’ll give it a try. Thanks!

thheller23:04:24

FWIW I'm currently working on an app using pedestal on the backend

thheller23:04:39

I just added an interceptor that sets the cache header (and don't use the shadow-cljs server at all)

markx23:04:20

Thanks for the recommendation. Anything special you like about it over Ring?