Fork me on GitHub
#shadow-cljs
<
2023-09-01
>
Chris McCormick01:09:19

I ran into a strange situation with this module: https://github.com/dankogai/js-base64 I am using it like this: ["js-base64" :refer [Base64]] and then (Base64.encode jsonstring) which works great in local development mode, but in production the following error happens:

2023-09-01 01:00:21 TypeError: Cannot read properties of undefined (reading 'encode')
When I do a (js/console.log Base64) in prod it prints as null. The module files exist in node_modules and are identical to my local.

thheller06:09:19

works fine for me. FWIW you should stick to proper interop when working with JS libs, this nested dot stuff is known to cause problems with inference and stuff. encode in this case is safe from renaming though, so I'm not sure what the problem would be. (Base64.encode ...) should be proper (.encode Base64 ...)

šŸ‘ 2
Chris McCormick01:09:23

Ok I will try to take another look at why this happened since it works fine for you. Thanks for checking.

Chris McCormick01:09:04

I also tried importing encode directly using [js-base64$Base64" :refer [encode]] and in that situation encode is null on prod.

Chris McCormick01:09:45

Prod is compiled like this: npx shadow-cljs release server --debug and the shadow-cljs.edn looks like this:

:builds {:server {:target :node-script
                   :output-to "devserver.js"
                   :main ytt.server/main!
                   :release {:output-to "build/server.js"}}

Chris McCormick01:09:39

In the end I have used a different library but I thought I'd report this here in case it helps.

hifumi12301:09:33

FWIW google closure library already comes with a base64 encoderā€¦ so you get it ā€œfor freeā€ with CLJS

hifumi12301:09:54

with that said, I assume the issue with your node script is that optimizations are set to advanced and base64 library doesnt play nicely with it

hifumi12301:09:19

I have ran into issues with the :node-script target where library code presumably gets mixed with the emitted JS output of ClojureScript, so you are forced into using simple optimizations or else you run into problems like youve described

Chris McCormick01:09:32

Oh I should have thought of using the closure lib, thanks!

hifumi12301:09:42

no problem šŸ˜„

hifumi12301:09:55

with that said, I recommend testing use of the npm base64 library with optimizations set to simple

hifumi12301:09:25

yes, your JS file will be huge, but it should work.. and if it does work, then you will have a faster base64 codec, since the base64 package on npm actually uses a native library to handle encode and decode

Chris McCormick02:09:18

For this particular project that's probably premature optimization (I am making a beeline towards shipping so I can get back to other things, and it's fast enough with the new library). That's a great point though, thanks!

Dustin Getz17:09:12

I believe this may be referencing a dead link:

WARNING: shadow-cljs not installed in project.
   See 
using shadow {:mvn/version "2.25.2"}

thheller17:09:35

indeed. I'll be removing that message entirely soon, so no use in documenting it now šŸ˜›

thheller17:09:03

it is also complaining about the npm package not being installed, no the deps.edn one

sergey.shvets18:09:40

Can shadow install ClojureScript dependencies from git tag?

hifumi12319:09:56

not supported by shadow-cljs itself, but you can use lein or deps.edn for this

sergey.shvets20:09:43

perfect, thanks!