figwheel-main

2022-05-10T08:10:00.210559Z

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?

2022-05-10T09:29:04.539979Z

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

2022-05-10T09:35:00.348559Z

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

2022-05-10T09:36:02.005489Z

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

hkjels 2022-05-10T10:03:29.157449Z

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/

2022-05-10T10:50:26.329679Z

@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

2022-05-10T11:37:22.486419Z

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?

2022-05-10T11:42:51.806939Z

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

2022-05-10T11:45:24.237079Z

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

2022-05-10T11:45:33.253119Z

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

2022-05-10T11:48:52.410689Z

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

2022-05-10T11:53:08.906299Z

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

hkjels 2022-05-10T12:10:37.703049Z

tried http://cljsjs.github.io ?

💯 1
2022-05-10T12:20:12.894399Z

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

2022-05-10T12:41:40.800439Z

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.

hkjels 2022-05-10T13:23:37.309249Z

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

2022-05-10T13:43:23.525679Z

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