Fork me on GitHub
#shadow-cljs
<
2023-10-07
>
dommas21:10:44

Hi, I’m currently trying to build a mobile app using https://github.com/PEZ/rn-rf-shadow and so far everything is smooth sailing! Now I’m trying to add a third-party library that uses Buffer and using it results in the following error:

ERROR  ReferenceError: Property 'Buffer' doesn't exist, js engine: hermes
After manually adding buffer via npx expo install buffer the error persists. However, once I add global.Buffer = require('buffer').Buffer; at the beginning of the index.js produced by shadow-cljs and reload the app in expo, the error disappears and the expected result is shown. But naturally, the added line disappears as soon as shadow-cljs generates a new index.js 😛 Is there any way I could make shadow prepend this line to the output index.js (or set the global variable in another way)? I played around with :prepend-js and :prepend a bit and all kinds of stuff in :modules but I couldn’t really make it work / see any differences. I cooked up a minimal demo-repo in case anybody is interested in helping me figure this out 😁 https://github.com/dummaso/buffer-problem-demo Thanks a lot in advance!

thheller06:10:32

:modules is not a valid config entry for :react-native builds, so it has no effect

thheller06:10:19

I don't have a working react-native setup, so I cannot test this. when exactly does this error occur?

thheller06:10:58

and which library uses Buffer?

thheller06:10:52

besides that for :react-native shadow-cljs is not the processing the JS code

thheller06:10:20

so maybe thats something you need to configure on the react-native build side

thheller06:10:35

depending on WHEN the JS code uses buffer it might be enough to just use (js/goog.exportSymbol "Buffer" Buffer) in your app.cljs

dommas07:10:51

Good morning, Thomas! Thank you for your support! 1. The error seems to occur after bundling and before shadow-cljs logs that it’s ready. 2. The culprit in this case is https://github.com/cschwarz/wkx for parsing/serializing GeoJson-related data. In my demo-repo, I use it within the wkb->geo-json fn 3. Adding (js/goog.exportSymbol "Buffer" Buffer) to the top of app.cljs unfortunately didn’t show any effect

dommas07:10:15

Here’s the output every time it fails:

› Reloading apps
Android Bundling complete 76ms
 ERROR  ReferenceError: Property 'Buffer' doesn't exist, js engine: hermes
 ERROR  Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called., js engine: hermes
 LOG  shadow-cljs #3 ready!
Changing the js engine to jsc unfortunately also didn’t fix my issue

dommas07:10:25

Upon removing all wkx-related code from appl.cljs it ofc works again:

› Reloading apps
Android Bundling complete 62ms
 LOG  shadow-cljs #9 ready!
 LOG  shadow-cljs load JS example/app.cljs
 LOG  shadow-cljs call example.app/start

dommas08:10:49

I think I might have figured it out, giving it a quick test

dommas08:10:33

Defining Buffer globally in a separate globals.js via global.Buffer = require('buffer').Buffer and then just importing globals.js in the index.js at the project root seems to do the trick 😁

👍 1