Fork me on GitHub
#clojurescript
<
2023-12-18
>
jpmonettas11:12:20

Hi! Is there a way of customizing goog-define vars per modules? Currently if we want to customize a value per build, we can add to lets say websocket.cljs file something like (goog-define port 7722) and then for each build do :

:builds 
{:my-app     {... :compiler-options {:closure-defines {websocket/port 7722}}
 :web-worker {... :compiler-options {:closure-defines {websocket/port 7733}}
but looking at shadow-cljs UserGuide looks like the recommended way of https://shadow-cljs.github.io/docs/UsersGuide.html#_web_workers is with a different module under the same build. Is there a way of customizing stuff for each module?

thheller12:12:48

why not just use two different defines?

thheller12:12:09

and no, this is not possible as they are compile time constants that can only have one value

jpmonettas12:12:59

why not just use two different defines?this is because that websocket/port thing is tooling configuration. I would like different builds to connect to different instances of the tools, which is possible with closure-defines, but not when compiling to separate modules. So this "websocket" namespace is the same for all builds/modules and coming from a library, but I want to be able to setup it differently for the main app and the web-worker for example

jpmonettas12:12:29

@U05224H0W is there a way of having different preloads for your app and web-workers when building this way?

thheller12:12:19

there is no requirement to make this one build

thheller12:12:48

you can just make a separate build for the worker if you wish. it'll just be more JS for the user to download

thheller12:12:09

yes :preloads can be defined per module

jpmonettas12:12:11

nice, thanks!

lilactown16:12:27

I want to have a macro that "lifts" a definition (a static constant) into the top-level. Is this currently possible with the default CLJS compiler or shadow-cljs?

thheller16:12:11

shadow-cljs yes, regular no

thheller16:12:15

basically shadow just binds this var when it compiles shadow.build.compiler/*analyze-top*

thheller16:12:41

which is just a function taking a form to analyze, and the result being "prepended" at the top

thheller16:12:53

kinda hacky but works šŸ˜›

šŸ˜‚ 1
lilactown17:12:49

this looks similar to what I want to do. I want to take something like

(defn foo []
  ($ :div ($ :span ($ :text "foo"))))
and generate
(def tmpl01
  (doto (js/document.createElement "template")
    (-> (.-innerHTML) (set! "
foo
")))) (defn foo [] (.. tmpl01 -content -firstChild (.cloneNode true)))

thheller17:12:03

yep, its exactly that basically

thheller17:12:39

(<< [:div [:span "foo"]]) creating (def the-thing ...) at the top level and (the-thing ...) where it was used

lilactown17:12:07

so IIUC I can just call shadow.build.compiler/*analyze-top* in my macro with the code I want and it will add that to the top of the file?

thheller17:12:43

just before the current top level form, not top of the file.

thheller17:12:23

(defn foo []
  (<< [:div [:span "foo"]]))
;; generates
(do (def the-thing (create-the-thing))
    (defn foo []
      (the-thing)))

lilactown17:12:38

great! thank you. this is perfect

diego.videco21:12:37

Hello everyone. On a shadow-cljs project Iā€™d like to have some tests run whenever I reload any of my namespaces: Like this:

(defn ^:dev/after-load autoload! []
  (js/console.log "== Autoload entry point ==")
  (let [test-results (with-out-str (my.test-runner/run-tests))]
    (if (str/includes? test-results "FAIL")
      (js/console.error test-results)
      (js/console.log test-results))))
I have added my.test-runner to the :devtools :preloads vector. But I am having trouble on CI, when the code should not run at all. Is there a way around this? I am using preloads with the aim of avoiding including this namespace on the production build.

thheller21:12:05

CI should run against release builds IMHO

thheller21:12:24

otherwise you could use a closure define to selectively disable it

diego.videco21:12:36

Yeah, CI runs that way, what I would like is to somehow remove that code, or to know if there is way to only run this code on dev.

thheller21:12:31

you said its in a preload? so it is already removed?

thheller21:12:01

ah wait .. you have the autoload! function in your regular namespace?

diego.videco21:12:16

but not the autoload! function. Sorry for the confusion. But I guess I can move that to a preload.

thheller21:12:27

yes, you move that function to a preload