having issues with advanced compilation when using npm modules + webpack. This isn't related to externs, I think? Here's an example of the generated code before bundling, with :pseudo-names true . The method names are minified, as indicated by the dollar signs. But moment itself is not, the name in moment is still ISO_8601 without the dollar signs. Probably because moment itself is not part of the advanced compile minification? Any thoughts?
I wonder why react-dom is not minified, but moment is 🤔
seems like that's because it's a call directly on the imported react. (.locale moment) works fine in my code as well, it's methods called on moment that isn't, and gets minified (such as (.toDate (moment)))
so from what I can tell so far, this example from the docs won't work in advanced compilation
Is this with :infer-externs set to true?
I’m pretty sure I have used moment at some point with cljs & figwheel; nowadays I mostly just use regular javascript for dates. So that’s a tip, but not helpful if you need to do arithmetic on the dates.
https://time2hack.com/you-dont-need-libraries-for-internationalisation-i18n-of-dates/
@hkjels yes, the behaviour is the same both with and without it. In fact, I get an inferred_externs.js even if I don't set :infer-externs at all, so it seems like figwheel (or something else) sets it for me
I guess this is about "externs"? I sort of assumed it's not, since they are imported into the cljs namespaces. But from the perspective of the compiler, I guess they're just opaque required blobs, instead of global variables, but still external?
seems so - I manually added externs for moment and then it worked just fine. So the issue seems to be that figwheel + webpack isn't able to generate externs properly. Which makes sense, I guess. At the time of cljs compilation, it has no access to the npm packages
TIL: the cljs compiler sets :infer-externs true when you set :target :bundle
also TIL: the webpack stuff is from cljs, not figwheel
I also suppose annotations like ^js/moment won't work either. The problem isn't that cljs doesn't know which type the arguments are, but that it doesn't seem to know that (.toDate (moment)) after a (:require [moment]) in that namespace means that it can't optimize toDate on moment
moving this to the #clojurescript channel, it doesn't seem figwheel related 🙂
yeah! I'm considering moving away from those so I can just use npm modules directly. But now I see first hand the value add of a community driven effort for managing externs :)
It's fixed if I have (.-toDate ^js/moment (moment)) anywhere. I.e. if I tell cljs that (:require [moment]) is in fact js/moment. I guess ns + require doesn't know that, why would it.
Now I’m curious if it works to set the type already in the require?
yeah, I tried that actually. No compile error, but it was unable to infer unfortunately