Fork me on GitHub
#shadow-cljs
<
2023-10-18
>
henrik08:10:45

I’m looking to use some NPM packages with Weird Shit™ in them (WASM and whatnot). Is there a good guide for configuring shadow + webpack to work together, including the webpack part (of which I have no previous experience)?

thheller09:10:04

usually you want to follow the npm package guide on how to package them with webpack

thheller09:10:44

happy to help you with the shadow-cljs side of things, webpack I'm out 😛

thheller09:10:33

also as I recently learned some packages just work with shadow-cljs, no webpack required

thheller09:10:43

so it all varies greatly 😛

hifumi12309:10:48

besides setting :js-provider :external and knowing its not easy to reload npm deps when you do this, there isnt much needed for shadow to produce js files consumable by webpack

thheller09:10:50

best to setup a repo showing what you want to do and how far you got

henrik09:10:48

Totally understand that! This was more a question as to whether such an example or guide exists, not a demand to have all answers served on a platter 🙂 I need to look into https://automerge.org/, which has popped up previously I saw, and there are no easy answers with that one. However, I got basic webpack up and running (without Automerge), so that’s fine. What’s odd is that first compile works fine, and then when shadow recompiles, it can’t infer the types of some things it seems.

henrik09:10:34

I’m guessing this might have something to do with the output of webpack, but why first compile works and recompile does not is kind of weird.

thheller10:10:13

go is pretty bad when it comes to inference and type hinting, so thats pretty common. try to move all code that does any kind of interop out of go blocks

thheller10:10:34

happy to take a look if you setup a repo where I can reproduce this

thheller10:10:24

using webpack generally means that shadow-cljs is not processing the JS code, therefore has less information about potential externs

thheller10:10:32

thus leading to potentially more warnings about inference

henrik10:10:15

OK, that’s definitely good to know. Shadow has been forgiving with me up until webpack. It’s a wrapper around Auth0, so I might have to do some promise interop instead of returning channels then.

thheller10:10:44

don't use core.async for promise interop, if that is the only use of core.async you have

henrik10:10:26

No, not the only reason, it’s part of a larger async chunk of stuff happening. But I can move the border of core.async away from being that far into the Auth0 stuff, shouldn’t be a problem.

henrik10:10:32

Btw, speaking of async stuff, I don’t know if you’ve played with virtual threads on the JVM yet, but I’m really impressed by it. They’ve more or less found a way around function coloring. I can’t think of a less intrusive async solution out there.

thheller10:10:30

yeah its nice, but I don't expect it to be ever available for JS 😛

henrik11:10:15

I’m a dreamer

henrik13:10:41

Alright, starting to see why this is hopeless now. I’d forgotten that I have dealt with WASM loading before, when writing a plugin for an editor. The methodology is to make module loads async, and basically have your own code be the last in a long chain of async loads.

henrik14:10:43

Found a very ugly, but functional, workaround at least.

Chris McCormick03:10:07

You probaly know about it already but the promesa library can make this type of async waiting (e.g. for wasm module load) much saner. It is probably also possible to inline the wasm code to base64 (or ArrayBuffer definition) into your build so it doesn't have to wait for a network request.

henrik09:10:12

Thanks @UUSQUGUF3! In this case, I don’t have control over the loading behaviour, since I’m not the one developing the library. And I do want to stay up-to-date with the lib if possible. Otherwise, indeed, I could control the loading of the WASM. I ended up compiling a different JS file for Automerge, put it in a different script tag, and then I await it being fully loaded before utilizing it. I delay loading the main app until AM is fully loaded, since I don’t want async code to infect every aspect of the app.

👍 1
Mikko Harju10:10:25

Hi! Regarding the dependency tree – the docs list either deps.edn / leiningen option – what can be used if the dependencies are listed directly in the shadow-cljs.edn-file? Should I just move them to deps.edn and use the -Stree option or is there some command that can be used to see the deps tree in this case?

thheller11:10:02

npx shadow-cljs info has some basic info

👍 1
Mikko Harju13:10:23

That worked perfectly. Thanks!

emaun20:10:38

Has anyone ever experienced a chrome browser error when importing two separate things from a single javscript package and know how best to resolve?

react-dom.development Warning: Encountered two children with the same key, `AS`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

emaun20:10:54

my imports are setup like so:

(:require
   ["libphonenumber-js" :refer [getExampleNumber parsePhoneNumber formatIncompletePhoneNumber]]
   ["libphonenumber-js/mobile/examples" :as exampleNumbers])

dpsutton20:10:27

i think you are seeing an error from the runtime react as described here: https://sentry.io/answers/defining-proper-key-in-props/

dpsutton20:10:51

ie, you have a collection of items and two of them have the same key so react is getting confused. it doesn’t have a good “name” for each item

emaun20:10:34

ah yeah, I'm familiar with this from react processing lists/options, but first time seeing this thrown with imports. thinking-face thank you!

dpsutton20:10:56

i’m questioning if this is from imports or rerendering your application

dpsutton20:10:12

i don’t think this is a source code problem

thheller06:10:53

yeah, this has nothing to do with the :require forms. it is from react :key using AS twice in the same collection. could be that the required examples have that, but that doesn't mean the require is bad 😛

gratitude-thank-you 1