Fork me on GitHub
#figwheel-main
<
2022-05-10
>
augustl08:05:00

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?

augustl09:05:04

I wonder why react-dom is not minified, but moment is :thinking_face:

augustl09:05:00

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)))

augustl09:05:02

so from what I can tell so far, this example from the docs won't work in advanced compilation

hkjels10:05:29

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/

augustl10:05:26

@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

augustl11:05:22

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?

augustl11:05:51

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

augustl11:05:24

TIL: the cljs compiler sets :infer-externs true when you set :target :bundle

augustl11:05:33

also TIL: the webpack stuff is from cljs, not figwheel

augustl11:05:52

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

augustl11:05:08

moving this to the #clojurescript channel, it doesn't seem figwheel related 🙂

augustl12:05:12

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 :)

augustl12:05:40

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.

hkjels13:05:37

Now I’m curious if it works to set the type already in the require?

augustl13:05:23

yeah, I tried that actually. No compile error, but it was unable to infer unfortunately