Fork me on GitHub
#shadow-cljs
<
2020-07-02
>
JAtkins05:07:31

How possible is it to dynamically load cljs modules? Use case is I'm trying to make a plugin system for an electron app. I'd like to have everything run under advanced compilation and be able to pull in optional deps, hopefully without dragging multiple copies of the cljs std library in. Does the EMCA support (-ish) in shadow have any bearing on this? If not this channel is most likely to have the answer anyway 🙂 AFAICT this is pretty easily possible if I don't care about importing cljs core multiple times. I can just use ^:export on the important functions and then use emca import.

Chris McCormick06:07:36

could you make a small js file that wraps require at runtime?

Chris McCormick06:07:50

oh wait, sorry, cljs modules not js modules.

thheller07:07:22

@jatkin plugins systems are impossible if you use :advanced. :advanced is whole-program optimization so it removes everything that isn't used. as soon as one of your plugins would use something that was removed previously everything breaks.

thheller07:07:28

multiple cljs.core is also not a great idea since they won't be compatible with each other. so can't pass a {:foo "bar"} map from one plugin to main or other plugin and expect that to work

pmooser08:07:35

I'm trying to use react-color, but importing it from one of my namespaces gives me an error, which seems to be from:

failed to load module$node_modules$react_color$lib$components$common$index

pmooser08:07:56

Any idea what that may indicate about what I'm doing wrong?

pmooser08:07:06

Ok, that comes from this error (I think):

Uncaught TypeError: Cannot redefine property: Alpha

pmooser08:07:38

And maybe that's from this code?

Object.defineProperty(exports, 'Alpha', {
  enumerable: true,
  get: function get() {
    return _interopRequireDefault(_Alpha).default;
  }
});

pmooser08:07:49

But I'm a bit mystified by it nonetheless ...

pmooser08:07:50

Ahh ... maybe this is just in the bootstrap build or something?

pmooser08:07:33

I'm not sure if there's a way to exclude just this one react npm from the bootstrap build, or why it's a problem ...

thheller08:07:47

what is a bootstrap build?

thheller08:07:06

you mean actual self-hosted :target :bootstrap?

pmooser08:07:52

Yes, that is what I mean. Sorry.

pmooser08:07:06

I mean this specifically blows up only in that build - I have a normal app build and then the self-hosted one.

pmooser08:07:20

If I exclude the whole NS that imports this, the error doesn't occur.

thheller08:07:51

no clue. if you setup a reproducible example I can take a look otherwise you are on your own.

thheller08:07:05

too much self-hosted weirdness I don't want to get into

thheller08:07:51

can't redefine sounds like it is already loaded but it is trying to load over itself

thheller08:07:04

which shouldn't happen if you properly bootstrap/init and all of that but I don't know

pmooser08:07:05

Sure, I understand it's mysterious and not a priority for you. Just thought something might sound obvious!

pmooser08:07:27

The whole mechanics of the self-hosted build are more or less a mystery for me anyway.

thheller09:07:41

anything self-hosted is just as much a mystery to me 😉

pmooser09:07:26

Heh ! I hope eventually I understand a little more ...

defa09:07:44

I’m having an issue that my entry-point (init-fn) is missing in production builds. shadow-cljs compile app works fine, but shadow-cljs release app seems not to export my init funciton.

thheller09:07:16

its not supposed to be exported if you use :init-fn in your build config

thheller09:07:41

otherwise you control whats exported via ^:export

defa09:07:21

I use

:modules    {:main {:entries [someapp.core]}}
and
(defn ^:export init [] ...

defa09:07:53

the init function is in someapp.core

thheller09:07:44

ok that should create the somapp.core.init in JS?

defa09:07:00

but not as a function. There is something like :

var shadow$provide = {};
(function(){
    ...
    Aa("someapp.core.init",function(){var a=new U(null,1,5,W,[zD],null);
    ...
}).call(this);

defa09:07:42

I can see no difference to https://github.com/shadow-cljs/examples/tree/master/re-frame but obviously I’m missing something.

thheller09:07:47

yeah that is supposed to create someapp.core.init with function(){var a...

thheller09:07:17

what is your actual problem though?

thheller09:07:26

how are you using the function? or why don't use use :init-fn instead? or when do you actually use it?

defa09:07:05

In index.hml : <body><script>someapp.core.init();</script></body>

defa09:07:52

I also tried :init-fn like in the docs but doesn’t work either.

thheller09:07:32

well where is the script loaded?

thheller09:07:50

gotta load the main.js for it to exist somewhere?

defa09:07:19

<html lang="en">
  <head>
    <meta charset='utf-8'>
    <!-- ... -->
    <script src="/js/compiled/main.js"></script>
  </head>
  <body>
    <div id="app"></div>
    <script>someapp.core.init();</script>
  </body>
</html>

thheller09:07:40

and thats not a 404 or so? that is actually loading fine without any errors?

thheller09:07:08

I mean what does the browser console say?

defa09:07:10

Can't find variable: someapp … but now I see that there is an error way before and that might cause the that the init-method will not be defined, right?

defa09:07:18

I’m having a router in my app (reitit) and that seems to have an issue with compiler optimisations in production as it seems?

defa09:07:37

=> Error: No protocol method Router.match-by-name defined for type null:

thheller09:07:41

yes any error during load will cause the export to never be created

thheller09:07:48

well something is nil. gotta find out what 😉

thheller09:07:04

shadow-cljs release app --debug will make that a bit easier

defa09:07:18

Thanks, Thomas, I will start the search…

defa09:07:03

Chrome clojure devtools helped to find the error quickly. It was a debug statement (println "*** " (reitit.frontend.easy/href ::somethin)) on toplevel … bummer.

strsnd14:07:23

I seem to be hanging in an endless loop in watch where shadow-cljs is recompiling some files constantly. Anyone seen that before?

thheller17:07:37

is something maybe touching your source files or so? so shadow-cljs writes something, something else watches the output and touches something which then triggers shadow-cljs to compile again?

strsnd17:07:05

I checked with inotifywatch (using linux here) that there are no modifications to my src tree. I am investigating a bit later today what is going on there.

strsnd18:07:05

Oh, can it have to do with me renaming my resources/ directory to resources-dev/ and I didn't notice since then? Looks like in shadow/build/classpath.clj it gets filtered.

strsnd18:07:05

If I delete the contents of cljs-runtime, while it is looping, it regenerates and stops. Maybe the pattern doesn't match correctly here.

strsnd18:07:58

Yeah, that was the change that leads to this behavior.

benny19:07:03

i’m stuck somehow, i’ve added cljs-ajax as a dependency and no matter what i do, i cannot get the repl to recognize the library:

(require '[ajax.core :refer [GET]])
=> nil
(GET "")
=> #object[ReferenceError ReferenceError: Can't find variable: ajax]

benny19:07:31

i’ve added other dependencies like sente and everything works just fine

thheller20:07:49

not enough info to comment. need more context. what :target? anything in the console? etc?

benny20:07:41

here’s my build:

:dev
                {:target     :react-native
                 :init-fn    client.core/init
                 :output-dir "app"
                 :compiler-options {:closure-defines
                                    {"re_frame.trace.trace_enabled_QMARK_" true}}
                 :devtools   {:after-load steroid.rn.core/reload
                              :build-notify steroid.rn.core/build-notify
                              :preloads [re-frisk-remote.preload]}}

benny20:07:32

re: console, initially i tried running a shadow server and connecting via nrepl in cursive but tried dropping to a cljs-repl and ran the above in a non-user namespace

benny20:07:23

just tried clearing everything, rebuilding, and got the same thing

thheller20:07:39

include it in one of you files first

thheller20:07:19

then try the REPL. I think there is an open issue about this.

thheller21:07:27

I don't use react-native myself and my setup for it keeps breaking ... so kinda frustrating to work on it 😛

benny21:07:23

i suspected that would help too but it doesn’t unfortunately 😞

thheller21:07:16

well any errors during load?

thheller21:07:03

seems like a known problem?

benny22:07:30

no errors are surfaced at least. by the sounds of it, people have had luck with cljs-http in rn