New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modularize moment.js, make the core as light as possible #2373
Comments
Are you using the version from CDN or npm? My size increase is more like |
You'll want to use
|
Thanks for the rundown @srcspider, I have read about IgnorePlugin before, but I didn't click that it was applicable for these kinds of situations, so it helps to match theory and practical together 👍
Don't you want to edit that line out? It might seem a little too brash, where as I think your general tone of the issue is very helpful, and you have a valid point. Plus you seem to know how this should all go down, I think a PR would go down well, I hear they're looking for help. ;-) |
I think it would be great to have the option to I know it's a 👍 from me, but I'm not sure if there would be any other takers out there. |
@naartjie not to be the bearer of bad news but would probably recommend waiting on one of the owners to give their thoughts on this before putting any considerable effort in. No point in pushing only to be shot down (or never approved). However if you need it yourself right now, by all means.
Sadly I'm very much on the fence on ES6. It doesn't integrate with my current workflows, and can't see any advantage to the various recommendations of ES6 workflows I've encountered (and by "no advantage" what I mean is they're far far inferior in resulting size, architecture and just a lot more unnecessarily complex in general). It's hard to get exited to contributing to a "Port to ES6" when it's, overall, just a worse thing (for me). |
The port to es6 is supposed to benefit development, not users. There are packaged versions of moment, with and without locales, uploaded to npmjs. About making a small core that is extensible - that is going to be a big step back in usability for most users. If you feel like it patch the src/moment to include what you want and transpile. I'm closing this for now. If a moment without locales is not bundled properly feel free to reopen. |
@ichernev I tried looking, and all I found was moment and moment-timezone, could you point me to the version of moment without locales in npmjs. I actually asked for this in #2416. It feels like using the IgnorePlugin is a hack/workaround, if there is already a packaged version without locales. Thanks. |
Aaah, I see. Npmjs is historically used for server side, where you don't care about size. But for other build tools that go on top of it, I guess we shall add another target |
@ichernev I use npm for both frontend and backend, and I think this has become much more common. I'm also a bit concerned with loading in a giant library just for the use of a couple nice date manipulation apis. +1 for a lean-core via npm, and an easy way to optionally add locales / peripheral functionality with the default being exclusion (unless explicitly included). One thing I think would be great to be able to do, is load a single locale as needed via webpack's |
+1 definitely |
+1 |
would love this as well. |
+1 |
One of the first (and best) optimizations we did was remove moment.js from our client side app. I would love to use moment.js more frequently but the additional page weight just isn't worth it for the basic date manipulation we do. So, I'm a big +1 on this as well. |
@jhubert I'm actually starting to consider that. What did you replace moment with? It's one of the largest things in my app. |
@rey-wright I was really only using it for formatting, so I just wrote a few functions that formatted the dates exactly as I needed them and used them throughout the site. It's nothing elegant, but it's extremely fast and light. |
@jhubert yeah we were doing that but... I really wish moment would be setup better...but now it's like Moment is one of the biggest pieces of our app... So yeah we might eventually go back to this method as well. Thanks for the insight. |
+1. Moment.js looks great, but the 12kb makes it not worth the include (that's bigger than the framework we use!) |
tests.js file could be removed from the npm package. This file amounts to 63% of the total npm package size. A dedicated development npm package could could include this tests.js file. For desktop applications (electron) using npm packages using moment, moment contributes significantly to the final application size (moment folder is duplicated in nested nod_modules dir). |
@DerekDomino The point of modularizing moment.js is for two reasons:
Removing a tests file doesn't accomplish either of those goals, and in fact doing what you suggest would make it harder to develop moment. And if you're for some reason having browsers forced to download the tests.js code, something else is going wrong. |
@fresheneesz While yes, the suggestion doesn't fix the main problem, why would excluding a test file from the npm package make it harder to develop moment? It isn't the same as removing tests from the repository, and, generally, test code is not meant to be packaged. |
Oh, just the npm package? I guess it wouldn't then. Still don't think its a road worth traveling. |
👍 |
I need this since years ago. I can not believe it is not done yet. |
⏰ Day.js 2KB immutable date library alternative to Moment.js with the same modern API |
Hi, very interested still in this feature. Any progress made? |
@nephi5 @rianfowler Since this issue seems to be abandoned, you probably want to check this: https://github.com/you-dont-need/You-Dont-Need-Momentjs |
@ichernev Almost every experienced developer I talk to now recommends not using moment.js anymore. The reactions on many threads regarding this topic are pretty clear. Can you please give us a feedback if you consider (or even already work on) a modularized moment.js package? It seems like you're ignoring this thread - please let us know, whether or not there's a chance for a modularized version. Thanks! |
With date-fns do we really need moment.js anymore? |
I typically recommend date-fns these days. I like the fp form myself. |
See https://momentjs.com/docs/#/-project-status/ As mentioned before, the moment build without locales is of comparable size to date-fns. |
Instead of solving something, just close the issue 👍🏻 |
I think @marwahaha explained things pretty well in Project status section. At least it is good to know there is no reason to wait for this functionality in Moment. |
@MickL moment.js is effectively dead, there is nothing to solve. https://www.theregister.com/2020/09/15/moment_js_javascript_library_future/ We are migrating to https://github.com/iamkun/dayjs instead. Almost fully API-compatible with moment and extremely lightweight. |
For webpack project, use this: https://github.com/jmblog/how-to-optimize-momentjs-with-webpack |
The project status page effectively explains all the thing. You should move to other libraries if moment.js is not suitable. |
I remember when moment was a "lightweight" library, right now at 12kb (gziped no less) for, what it's in most cases, just very very simple date manipulation.
A simple solution to this problem would be to modularize momentjs so at least for environments like browerify, webpack and such a minimal version can be obtained.
eg.
Regular usage can stay the same,
Modular usage could look like this,
Let's pretend the above is in something like
lib/moment
and represents your default boilerplate configuration for it. Obviously you may eventually encounter situations where you need more advanced functions. If we add to the default boilerplate we unfortunately add to every other context that doesn't need it. This is a big bummer if we're trying to keep client size down with things like webpack code splitting.However, so long as the modular system is capable of being extending any time there's no problem whatsoeve. Ideally, to avoid subtle race condition bugs, we should be able to get a new "advanced" version as a separate instance:
Now only the module that actually uses that feature pays the cost. If you use moment in 100 places and only 1 actually needs internationalization just that one place will ever pay for it. This applies to all functions, all the core needs to do is store a plain date object and some getters. Is everything you do just pass unix time from the server? you can just have unixtime parser. Do you only ever validate dates on the server? you can skip on any and all validation, etc.
It's effectively as light as you building your own specialized helpers. And much like projects like gulp, postcss and so on the community can contribute easily to it though easily maintainable chunks.
And in this way, momentjs can, once again, be called "lightweight javascript date library".
The text was updated successfully, but these errors were encountered: