Fork me on GitHub
#shadow-cljs
<
2022-12-16
>
Ben Hammond21:12:20

Hi. I am playing with webworkers for the first time: I can successfully call into the webworker module, but I quickly see a console error

worker.js:1453 failed to load shadow.module.gamelogic.append.js ReferenceError: shadow is not defined
    at eval (shadow.module.gamelogic.append.js:2:1)
    at eval (<anonymous>)
    at goog.globalEval (robot.js:497:11)
    at env.evalLoad (robot.js:1451:12)
    at worker.js:1506:12
worker.js is my worker module gamelogic is a shared module that it depends upo; presumably Ihave forgotton to import something..?

thheller05:12:47

did you set :web-worker true in the build config for the worker module?

Ben Hammond10:12:49

:robot {:entries [foo.robot]
            :depends-on #{:gamelogic}
            :web-worker true}
    }

thheller10:12:30

and what is this file trying to call? just look at the source in the cljs-runtime dir

Ben Hammond10:12:35

could the gamelogic module be inadequate?

:gamelogic {:entries [foo.encode-board
                          foo.calc-moves]}

thheller10:12:56

first should figure out what it is actually trying to call

Ben Hammond11:12:39

so the action line that triggers the error is on the first line of shadow.module.gamelogic.append.js

shadow.cljs.devtools.client.env.module_loaded('gamelogic');

thheller11:12:17

and the worker script is itself loading the gamelogic module properly?

thheller11:12:30

there should be a importScripts at the top somewhere

thheller11:12:56

just to verify: this is a :target :browser build?

thheller11:12:33

and no other errors happens during load prior to this one?

thheller11:12:50

any exception could break the loading process and lead to this one

Ben Hammond11:12:18

yes this is the first error upon a reload]

thheller11:12:30

not asking about first

thheller11:12:35

asking about other errors

thheller11:12:00

ordering is something weird like that

Ben Hammond11:12:00

So I do have other CORS errors, because this is a dev experiment and it is trying to connect to public facing website was hoping that would not be relevant

thheller11:12:30

can't rule that out unfortunately

Ben Hammond11:12:55

so I have to set things up properly adn then try again

thheller11:12:57

well, you can by just commenting it out and seeing if the other error goes away 😉

thheller11:12:09

FWIW never ever ever run anything directly on load

thheller11:12:24

use :init-fn to let shadow-cljs call a init function for you

thheller11:12:39

do not run anything before that to removing likelyhood of such errors

thheller11:12:10

so you (defn init [] (do-stuff)) and shadow-cljs call it, instead of just (do-stuff) in an ns somewhere

thheller11:12:16

you were not. that example also uses :init-fn 😛

Ben Hammond11:12:14

perhaps I don't understand. Am attaching event listener globally like

(js/self.addEventListener "message"
                          (fn [^js evt]
and you are saying that this should be done within an init function?

Ben Hammond11:12:08

Interestingly, the web worker is behaving as expected; this error message is just creating console noise

Ben Hammond11:12:24

I'll tidy up my stuff and then try again

Ben Hammond11:12:29

thanks for the help

thheller12:12:46

yes, this should be in the init-fn

👍 1
thheller12:12:01

otherwise if the code hot-reloads it is called again

thheller12:12:10

thus adding the event listener again

thheller12:12:22

while the old one it still attached

Ben Hammond16:12:56

the error is not in the web worker; the error is in the main module

Ben Hammond16:12:55

When attempting to set up module dependencies, I set a :default true like this

:main {:entries [foo.main]
           :init-fn foo.main/main
           :depends-on #{:gamelogic}
           :default true}
and clearly it does no do what I expected it to do

Ben Hammond16:12:01

I was originally hoping to avoid specifying dependencies but shadow-cljs told me that it could not figure out the hierarchy so I set the :default flag, and that error seemed to go away

Ben Hammond16:12:13

but I got got the subsequent

gamelogic.js:1451 ReferenceError: shadow is not defined
    at eval (shadow.module.gamelogic.append.js:2:1)
    at eval (<anonymous>)
    at goog.globalEval (gamelogic.js:497:11)
    at env.evalLoad (gamelogic.js:1559:12)
    at gamelogic.js:1615:12

Ben Hammond16:12:33

do I have to use module dependencies?

Ben Hammond16:12:45

so this is the error I get with no dependencies

[:foo] Build failure:
two modules without deps, please specify which one is the default
{:a :main, :b :robot}
ExceptionInfo: two modules without deps, please specify which one is the default
        shadow.build.targets.browser/pick-default-module-from-config/fn--25576 (browser.clj:174)
 

Ben Hammond16:12:05

if I set the problematic :default true in the main module then I get a different error

[:foo] Build failure:
no common dependency found for src
{:src [:shadow.build.classpath/resource "goog/base.js"], :deps #{:main :robot}}
ExceptionInfo: no common dependency found for src
        shadow.build.modules/compact-build-modules/find-closest-common-dependency--15601

Ben Hammond16:12:15

what does that :default flag really do?

thheller18:12:09

you are not supposed to ever set it. you are supposed to define one module that has no :depends-on. the rest must have it

thheller18:12:32

and yes, you must specify all proper dependencies so a proper graph can be built out of it

👍 1
thanks2 1