Fork me on GitHub
#shadow-cljs
<
2021-02-28
>
johnjelinek08:02:22

(def my-stack #(let [stack %
                     _ (DockerProvider. stack "default")
                     image (Image. stack "nginxImage" {})
                     container (Container. stack "nginxContainer" {} #_{:ports [{:internal 80
                                                                                 :external 8000}]})]
                 (set! (.-name image) "nginx:latest")
                 (set! (.-keepLocally image) false)
                 (set! (.-image container) (.-latest image))
                 (set! (.-name container) "tutorial")
                 #_(set! (.-ports container) {"internal" 80
                                            "external" 8000})))
I should be able to set opts for these js interop objects like this:
(Container. stack "nginxContainer" {:port [{:internal 80 :external 8000}])
but this isn't working and so I'm having to use (set! -- any idea why that's not working?

johnjelinek08:02:46

I don't seem to be getting any errors, but the values just aren't getting set during runtime

johnjelinek08:02:13

side-note: the actual ports example fails either way (with or without set!) -- the error there is that it can't seem to map over [{,,,}]

thheller09:02:59

first of all. why is this a def with an anonymous function? makes its terribly hard to read.

thheller09:02:37

(Container. stack "nginxContainer" {:port [{:internal 80 :external 8000}]) I assume Container is some JS Object and you are passing a CLJS map as an argument which it very likely won't understand

thheller09:02:57

use (Container. stack "nginxContainer" (clj->js {:port [{:internal 80 :external 8000}]))

thheller09:02:56

the last commented out set! has the same issue. trying to pass a CLJS map to any JS object will either fail directly or fail silently since they all expect plain JS objects

thheller09:02:34

you can also construct JS objects directly via #js {:foo "bar"}

johnjelinek15:02:05

@U05224H0W: thanks! clj-js worked! #js {,,, did not

johnjelinek15:02:58

ah, if I'm doing #js, I gotta apply it more liberally:

(Container. stack "nginxContainer" #js {:image (.-latest image)
                                              :name "tutorial"
                                              :ports #js [#js {:internal 80
                                                               :external 8000}]})

johnjelinek15:02:35

😆 this should address your first-of-all:

(defn my-stack [app name]
  (let [stack (TerraformStack. app name)
        image (Image. stack "nginxImage" #js {:name "nginx:latest"
                                              :keepLocally false})]
    (DockerProvider. stack "default")
    (Container. stack "nginxContainer" (clj->js {:image (.-latest image)
                                                 :name "tutorial"
                                                 :ports [{:internal 80
                                                          :external 8000}]}))))

johnjelinek15:02:01

thank you again for your help!

thheller15:02:14

yeah #js doesn't apply to nested values so clj->js is better for more complex structures where you don't care about performance

👍 3
nmkip14:02:10

Hi, I'm trying to make a simple chrome extension but I'm having some problems with the initial setup of the project. When I watch the :extension build and then open the url that I'm matching in my shadow-cljs.edn I'm getting the following error multiple times:

browser.cljs:406 WebSocket connection to '' failed: Unknown reason
shadow$cljs$devtools$client$browser$ws_connect_impl @ browser.cljs:406
shadow$cljs$devtools$client$browser$ws_connect @ browser.cljs:388
browser.cljs:20 🠶 shadow-cljs: websocket error Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2, …}
browser.cljs:20 🠶 shadow-cljs: WebSocket disconnected!
It keeps trying to reconnect and always fails. I'm using binaryage/chromex shadow example as my template with some changes: • Removed deps.edn • Removed all dependencies • Changed the :content-script :matches to a specific url instead of <all_urls> More info in thread 🧵

nmkip14:02:04

This is how my shadow-cljs.edn file looks like:

{:source-paths ["src/background"
                "src/popup"
                "src/content-script"]

#_#_ :dependencies [[binaryage/chromex "RELEASE"]
                [binaryage/devtools "RELEASE"]]
 
 :builds {:extension

          {:target           :chrome-extension
           :extension-dir    "resources/unpacked"
           :manifest-file    "resources/unpacked/manifest.edn"
           :compiler-options {; 
                              :closure-output-charset "US-ASCII"
                              ; we need to fully inline source maps to avoid devtools security restrictions of loading .map
                              ; files via chrome-extension://<extension-id>/out/cljs-runtime/goog.debug.error.js.map urls
                              :source-map-inline      true}
           :outputs          {:background     {:output-type :chrome/background
                                               :entries     [chromex-sample.background]}
                              :content-script {:output-type    :chrome/content-script
                                               :chrome/options {:matches [""]
                                                                :run-at  "document_end"}
                                               :entries        [chromex-sample.content-script]}
                              :popup          {:output-type :chrome/shared
                                               :entries     [chromex-sample.popup]}}}}}

nmkip14:02:06

This is how my manifest.edn file looks like:

{:name             "Test chrome extension"
 :version          "1.0"
 :description      "Test chrome extension"
 :manifest-version 2

 :permissions      ["http://*/*"
                    "https://*/*"]

 :browser-action   {:default-title "Show the popup"
                    :default-icon  "images/icon48.png"
                    :default-popup "popup.html"}

 :content-security-policy
                   ["default-src 'self';"
                    ;; FIXME: unsafe-eval should be injected for dev, user shouldn't have to write this
                    "script-src 'self' 'unsafe-eval' ;"
                    "connect-src * data: blob: filesystem:;"
                    "style-src 'self' data: chrome-extension-resource: 'unsafe-inline';"
                    "img-src 'self' data: chrome-extension-resource:;"
                    ;; FIXME: localhost only? don't want to allow any origin though
                    "frame-src 'self' :* data: chrome-extension-resource:;"
                    "font-src 'self' data: chrome-extension-resource:;"
                    "media-src * data: blob: filesystem:;"]}

thheller15:02:44

> failed: Unknown reason

thheller15:02:57

there must be a reaon. dunno how you find out what 😛

nmkip15:02:16

There's probably a reason xD

nmkip15:02:47

and it's probably my fault 😛

thheller15:02:09

config looks ok. does it work if you change the :matches?

nmkip15:02:40

I believe the :matches is working as I'm only getting shadow error messages in this site: https://classroom.google.com/u/0/ta/not-reviewed/all

nmkip15:02:21

It works if I use :matches [""]

nmkip15:02:48

shadow-cljs connects to the websocket and if I eval a println in the content_script.cljs I can see it in the console

thheller15:02:37

well yeah it must be allowed to connect to the shadow-cljs server for the REPL and hot-reload stuff

thheller15:02:52

(for development builds, ie. watch and compile)

thheller15:02:15

release can and should be stricter

nmkip15:02:35

and how do I allow it?

thheller15:02:34

its an array so just add multiple things

nmkip15:02:48

what array are you talking about?

thheller15:02:56

:matches ["" ":*/*"] probably

nmkip15:02:35

Using this shadow-cljs connects to the websocket in localhost, but I get the websocket error in google classroom

nmkip16:02:20

This is the localhost console:

nmkip16:02:51

This is google classroom console:

nmkip16:02:49

And this is the shadow-cljs.edn file:

{:source-paths ["src/background"
                "src/popup"
                "src/content-script"]

 #_#_:dependencies [[binaryage/chromex "RELEASE"]
                    [binaryage/devtools "RELEASE"]]

 :builds {:extension

          {:target           :chrome-extension
           :extension-dir    "resources/unpacked"
           :manifest-file    "resources/unpacked/manifest.edn"
           :compiler-options {:closure-output-charset "US-ASCII"
                              :source-map-inline      true}
           :outputs          {:background     {:output-type :chrome/background
                                               :entries     [chromex-sample.background]}
                              :content-script {:output-type    :chrome/content-script
                                               :chrome/options {:matches ["" ""]
                                                                :run-at  "document_end"}
                                               :entries        [chromex-sample.content-script]}
                              :popup          {:output-type :chrome/shared
                                               :entries     [chromex-sample.popup]}}}}}

thheller16:02:31

try ":*/*" instead of ""

thheller16:02:36

not a clue really. been a while since I looked at any of this. maybe chrome got stricter security restrictions these days

thheller16:02:47

do you still have some csp rules in your HTML maybe?

nmkip16:02:20

I'm using brave browser I don't know if that's a problem

thheller16:02:49

might be. not a clue, never used it.

nmkip16:02:27

As far as I know brave uses the same extensions as chrome.

nmkip16:02:33

I will download chrome and try there.

thheller16:02:54

more likely that something in your config doesn't quite match

thheller16:02:00

what about your html?

thheller16:02:09

is there any?

nmkip16:02:14

the only html is the popup, just a hello world 😛

thheller16:02:31

ok, does that have some csp rules?

thheller16:02:51

<meta name="Content-Security-Policy" ...>

thheller16:02:42

then I'm out of ideas

nmkip16:02:32

okay, no problem! Thanks a lot 🙂 I'll try a bit more and if I fail I'll just use javascript and suffer 😛

thheller16:02:23

well the websocket is only used for hot-reload and REPL. if you don't care about those then you can just set :devtools {:enabled false} and it won't try to connect

nmkip16:02:52

good to know , maybe Ill do that

nmkip17:02:03

@U05224H0W I've noticed that I'm having this problem with https sites

thheller17:02:56

hmm yeah that might be a reason. might need to setup ssl for shadow-cljs so is all ssl

nmkip15:02:19

I tried that

naomarik18:02:23

Has anyone got react-native project going using WSL2? I'm wondering how/where the port from the app (below 50594 and 50612) is being determined so I can forward it properly to my WSL2 instance:

[Sun Feb 28 2021 22:39:55.649]  ERROR    shadow-cljs - remote-error {"isTrusted": false, "message": "failed to connect to /172.25.210.33 (port 9630) from /192.168.0.166 (port 50594) after 10000ms"}
[Sun Feb 28 2021 22:40:10.782]  WARN     The shadow-cljs Websocket was disconnected.
[Sun Feb 28 2021 22:40:10.792]  ERROR    shadow-cljs - remote-error {"isTrusted": false, "message": "failed to connect to /172.25.210.33 (port 9630) from /192.168.0.166 (port 50612) after 10000ms"}
[Sun Feb 28 2021 22:40:10.852]  LOG      giving up trying to connect

thheller18:02:01

that is the port of the react-native app client. you don't need to do anything for that port as it will be random always

thheller18:02:17

you just need to be able to connect from the app to shadow-cljs. looks like that is trying to use the wrong IP

naomarik18:02:07

yeah it's using the IP internally from WSL2... I have adb tcpip running from windows then within WSL2 adb connect to connect to the device

thheller18:02:16

shadow-cljs watch app --config-merge '{:local-ip "1.2.3.4"}' lets you set a specific one

naomarik18:02:51

that worked 😉

naomarik18:02:18

I can't just add the local-ip key within my shadow-cljs.edn ?

naomarik18:02:23

I tried that first and didn't seem to do anything

thheller18:02:35

currently needs to go into the build config

thheller18:02:48

still need create a better way to control all this

naomarik18:02:43

cool that works too