Page MenuHomePhabricator

Replace Moment.js in core with something smaller (what?)
Open, LowPublic

Description

Moment.js is quite a large library and seems to replicate some of the logic we have in mw.msg. In Popups for example, this library is used to render a message "Edited 33 minutes ago", and this accounts for over 50% of the code. (See T137775)~ [JR: Popups no longer uses]

In MobileFrontend rather than using Moment.js we use a small library to provide information about last modified time with units. We then use this information in getLastModifiedMessage to render one of several messages.

This approach seems cheaper.

My question: should we deprecate use of moment.js in core for a more simplistic library, based on the MobileFrontend library (or some other library), that can be used with messages?

Event Timeline

Roan suggested https://github.com/jquery/globalize might be a good replacement but may equally be large.

Based on https://github.com/jquery/globalize#pick-the-modules-you-need it sounds like it might be smaller than moment, although those figures don't include the CLDR data that you need.

@Jdlrobson @Catrope “may equally be large” vs “might be smaller than moment”?!

Jdforrester-WMF renamed this task from Do we really need Moment.js in core? to Should we replace Moment.js in core with something smaller? (If yes, what?).Nov 28 2017, 7:10 PM
Jdforrester-WMF triaged this task as Low priority.

Another possible alternative hot off the news: https://github.com/xx45/dayjs – described as ”Fast 2KB date library alternative to Moment.js with the same modern API“

Another possible alternative hot off the news: https://github.com/xx45/dayjs – described as ”Fast 2KB date library alternative to Moment.js with the same modern API“

Unfortunately AFAICT that one is small and fast by not doing i18n, which is a non-starter for us.

I'm not familiar with all of moment.js abilities, but there's discussion for “internationalization” https://github.com/xx45/dayjs/issues/36

Do we use timezone localisation? This seems to be the biggest problem with moving away...

moment-timezone doesn't seem to be in use:

In the meanwhile I18n has landed in dayjs, see https://github.com/iamkun/dayjs/blob/dev/docs/en/I18n.md


For simple formatting, the JavaScript [Date.toLocaleString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString) might be sufficient (providing good browser support for its basic usage).

One question is also how strict the localized output must be, e.g., I'm relating to the en locale override in core/resources/src/moment/moment-dmy.js.

In core we have a few calls to moment() (codesearch), in MobileFrontend only two (codesearch)


Would it make sense to submit a patch replacing the moment usage in an extension such as MobileFrontend or MultimediaViewer as a starting point?

Quoting https://momentjs.com/docs/#/-project-status/ :

we would like to discourage Moment from being used in new projects going forward. Instead, we would like to recommend alternatives that are excellent choices for use in modern applications today

Aklapper renamed this task from Should we replace Moment.js in core with something smaller? (If yes, what?) to Replace Moment.js in core with something smaller (what?).Aug 25 2022, 8:16 AM

We could evaluate using Intl, not sure if it covers all of moment's timezone use cases but probably enough for simple date formatting. GrowthExperiments and other extensions are already using it. Probably some old browser compatibility would be desired, a partial polyfill maybe.

I suggested MediaWiki date formatter keywords to Moment.js in 2014 https://github.com/moment/moment/issues/1454 due to localization needs in moment.js to fully support MediaWiki and it was closed years later referring to the page also linked above, "We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.", so guess the correct direction here would be first deprecate moment.js module then try to reduce the uses till the removal as it doesn't support needed localization stuff (non-Gregorian calendars) which browser-own Intl does support for free.