This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-04
Channels
- # announcements (123)
- # asami (1)
- # babashka (50)
- # beginners (49)
- # biff (21)
- # calva (48)
- # cider (6)
- # clj-yaml (8)
- # cljsrn (2)
- # clojure (9)
- # clojure-conj (1)
- # clojure-indonesia (1)
- # clojure-losangeles (1)
- # clojure-uk (18)
- # clojurescript (21)
- # data-science (19)
- # datahike (13)
- # events (1)
- # fulcro (1)
- # honeysql (4)
- # hyperfiddle (40)
- # improve-getting-started (3)
- # inf-clojure (1)
- # malli (6)
- # off-topic (45)
- # releases (3)
- # rewrite-clj (14)
- # scittle (1)
- # shadow-cljs (57)
- # tools-deps (1)
- # vim (13)
- # xtdb (14)
I have a js file in my cljs project, really just out of lazyness because I want to copy some js example code and don't really know how to translate the async/await syntax. Anyway it all works fine in dev but under advanced compilation for some reason keys in objects are getting $
put around them and everything breaks.
Here's an example of the js function
export function createIDBPersister(idbValidKey) {
return {
persistClient: async (client) => {
set(idbValidKey, client);
},
restoreClient: async () => {
return await get(idbValidKey);
},
removeClient: async () => {
await del(idbValidKey);
},
};
}
and here is the result of console.log(createIDBPersister())
$persistClient$: async $client$$ => {…}
$removeClient$: async() => {…}
$restoreClient$: async() => {…}
[[Prototype]]: Object
It also renames my headers map for a fetch call so I start sending
$Authorization$: "Bearer ...."
which obviously breaks stuff too. the issue is only for code in this one js file which i'm importing into my cljs like
["/js/myJsFile" :refer [...]]
I guess the dollar signs are just because I have pseudonames/pretty print turned on to try and debug other advanced compilation errors, when they are turned off the keys are just minified which I guess is the issue. Is there any way to tell shadow to not put a specific file into advanced compilation? I feel like it should never do that for js files anyway
yes, :pseudo-names
turns persistClient
to $persistClient$
as a marker that it will be renamed, while still being able to tell what it was renamed from. purely a debugging aid, as you noticed without pseudo-names they are just shorter
if you use things properly in your code these references will also be renamed and everything will be happy
I think the problem is that it seems npm libs don't get run through advanced optimisations, and this client is passed to an npm lib which uses it, and then the names don't match up
ok it wasn't clear that it isn't the CLJS code using the JS code, it is other npm libs
or you could include the JS code in a different way, so it also only goes through :simple
oh? I think that would be my preferred approach. does that involve making it cjs compatible? or is it a shadow config option?
I guess my other option is just translate it to cljs and then auto externs should do its thing?
you then configure :js-options {:js-package-dirs ["packages" "node_modules"]}
in your build config
and from CLJS you then (:require ["your-js-code/myJsFile.js"])
like any other npm package
The required JS dependency "js/myJs" is not available, it was required by "app/core.cljs".
Dependency Trace:
app/core.cljs
Searched for npm packages in:
/Users/.../packages
/Users/.../node_modules
alexdavis@Alexs-MBP-2 myProject % ls packages/js
myJs.js
Maybe I'm not understanding something, do I need a package.jsonwell first of all don't use js
as the name. js/
has special meaning in CLJS, so using it like this is asking for trouble
package.json is not required, but helps for certain things. just a npm init -y
in that packages/your-lib
folder is enough, doesn't need anything else
(:require ["your-lib/myJs.js"])
looks for packages/your-lib/myJs.js
first, then if not found node_modules/your-lib/myJs.js
ok so I am now requiring directly like (:require ["helpers/myJs.js"])
and my file is in packages/helpers/myJs.js
but it still won't find it
does it matter that this js file is using export function ...
and other esm syntax?
beware with the name of you package. you will effectively override the name of a package in node_modules
. so, pick something that is unique enough to not interfere
definitely no collision, though even if it was overriding I would expect a different error
I don't see why it wouldn't work. unless you are maybe on some ancient shadow-cljs version?
2.19.5 in node modules and 2.21 in deps (not sure what is used for what). They are normal files
and you just created this packages
directory? it didn't exist prior with some already defined meaning?
not sure why that would change anything but always helps to verify you are in the correct proejct and in the correct folder
Still no luck... I am going to sleep now, in the morning I will put together a minimal example repo (and then it will probably start working and I'll notice some dumb typo 😄)
Thanks for all your help though, I at least feel I understand what is going on a bit better
ok well I setup the smallest example I could and still get the same problem, hopefully you can see what I'm doing wrong here https://github.com/armincerf/tmp-shadow-issue-example
hmm I guess some change I did made the package.json mandatory. go into cd packages/myAppJsFiles; npm init -y
fixes it