Fork me on GitHub
#cljs-dev
<
2019-03-28
>
zane18:03:02

How is cljs.js/*loaded* intended to be used?

zane18:03:45

Specifically, I'm trying to use cljs.js/eval-str to evaluate code that :requires and uses cljs.analyzer/macroexpand-1. Not sure if the right thing to so is to augment my cljs.js/*load-fn* to retrieve the cljs.analyzer source (and all its dependencies) or something more radical like,

(ns …
  (:require [cljs.js :as cljs] 
            [cljs.analyzer]))

(swap! cljs/*loaded* conj 'cljs.analyzer)
(cljs/eval-str …)

dnolen19:03:42

I don't believe you need to use it

dnolen19:03:46

why do you think you do?

dnolen19:03:34

might also be helpful to understand what you are trying with a short description

dnolen19:03:48

like are you loading a bunch of stuff from your server over HTTP or something?

zane20:03:47

I'm trying to use cljs.js/eval-str to evaluate code that :requires and uses cljs.analyzer/macroexpand-1. Not sure if the right thing to so is to augment my cljs.js/*load-fn* to retrieve the cljs.analyzer source (and all its dependencies) or something more radical like,

(ns …
  (:require [cljs.js :as cljs] 
            [cljs.analyzer]))

(swap! cljs/*loaded* conj 'cljs.analyzer)
(cljs/eval-str …)

zane20:03:36

Does that answer your question?

zane20:03:19

@joshuamilesthayer is working with me on this. 🙂

dnolen20:03:29

but why do you need to do that?

dnolen20:03:37

if you're self hosted analyzer is already there

zane20:03:57

Ah, okay.

dnolen20:03:11

self host fundamentally must have the analyzer & compiler and any deps

zane20:03:19

Makes sense now that you say that. 🙂

zane20:03:32

In that case is the right thing to do to have our load-fn return nil for that dependency?

dnolen20:03:58

the loading mechanism already checks loaded

dnolen20:03:03

I wouldn't do anything at all

dnolen20:03:05

just load stuff

dnolen20:03:23

if you look at the top of the cljs.js ns you'll see the requires

zane20:03:38

We're finding that our load-fn is called for cljs.analyzer if the code being eval-str'd :requires it, but that may be because we're making a mistake somewhere else?

dnolen20:03:25

oh hrm, I think you're right

dnolen20:03:33

we don't populate *loaded*

dnolen20:03:49

for what is already included necessarily

dnolen20:03:12

might want to look at how Planck etc. handle this

dnolen20:03:33

just putting analyzer in there won't be good enough of course

dnolen20:03:41

since that's not the transitive graph of deps

zane20:03:10

ok, we'll have a look at what Planck does. Thanks!

zane20:03:33

I can also file an issue for populating *loaded* if you think that's a good idea.

dnolen20:03:32

probably not going to bother

4
dnolen20:03:37

it's not as simple as it sounds

dnolen20:03:43

since it has to consider the entire build

dnolen20:03:53

not just what cljs.js requires

zane20:03:11

ok. I'll refrain from filing anything then.

dnolen20:03:21

one easy thing of course is to just look at the namespaces

dnolen20:03:30

this is possible since you're not advanced compiling

dnolen20:03:44

i.e. split the ns segments into strings and check goog.global

zane20:03:24

I'm not sure I understand. Do you mean manually trace the transitive dependencies and conj them all in ourselves?

zane20:03:34

If so I'm not sure I understand that last part about goog.global.

dnolen20:03:15

you don't need to bother with any of that

dnolen20:03:43

well for at least a certain set of namespaces

dnolen20:03:51

specifically cljs.*

dnolen20:03:57

or clojure.*

dnolen20:03:03

if the ns starts with that

dnolen20:03:11

then you can just check goog.global

dnolen20:03:28

goog.global["cljs"]["core"] etc.

dnolen20:03:33

but that can be done programmatically

dnolen20:03:13

you probably don't want to do this in general if you want to support standard REPL behavior for :reload :reload-all

dnolen20:03:22

but for core nses should be fine

dnolen20:03:57

goog.global is always bound to the global context regardless of JS environment

dnolen20:03:08

that's where all the namespaces live

zane21:03:40

I spoke to David in person about this 👆:skin-tone-2: and he said a better way to deal with this is by dumping the compiler state after requiring the necessary namespaces + passing that state into cljs.js/eval-str.