Skip to content
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

Convert common time helpers to Typescript #2391

Merged
merged 4 commits into from
Oct 31, 2020

Conversation

lhsazevedo
Copy link
Contributor

See #3533

Copy link
Sponsor Member

@askvortsov1 askvortsov1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mostly makes sense, thanks!

js/src/common/helpers/fullTime.tsx Outdated Show resolved Hide resolved
Copy link
Member

@clarkwinkelmann clarkwinkelmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does one import uses import * as dayjs from 'dayjs'; while the others use import dayjs from 'dayjs'; ?

Do we actually want to import global variables or could we just type-hint them somewhere? Using the global variable instead of an import will save up on the file size, as webpack won't have to add the additional code that resolves each import.

@dsevillamartin
Copy link
Member

@clarkwinkelmann We can test locally if the bundle size increases if they're just used for typings. Haven't done so myself yet.

@lhsazevedo
Copy link
Contributor Author

I removed the import * as dayjs variant and now all three changed files use the default export

Copy link
Sponsor Member

@askvortsov1 askvortsov1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! I'm approving this as long as

Do we actually want to import global variables or could we just type-hint them somewhere? Using the global variable instead of an import will save up on the file size, as webpack won't have to add the additional code that resolves each import.

doesn't prove to be an issue

@lhsazevedo
Copy link
Contributor Author

@askvortsov Thanks, I'll test this as soon as possible.

@lhsazevedo
Copy link
Contributor Author

lhsazevedo commented Oct 25, 2020

Test

So, apparently webpack doesn't inject the module resolving code on each module, but use a global one. Here's the test i did.

Flarum build

Heres src/common/utils/humanTime.ts unminified output:

"use strict";
__webpack_require__.d(__webpack_exports__, "a", function() { return humanTime; });
var dayjs = __webpack_require__(28);
var dayjs_default = /*#__PURE__*/__webpack_require__.n(dayjs);
var dayjs_plugin_relativeTime = __webpack_require__(73);
var dayjs_plugin_relativeTime_default = /*#__PURE__*/__webpack_require__.n(dayjs_plugin_relativeTime);

function humanTime(time) {
  var d = dayjs_default()(time);
  var now = dayjs_default()();

  // ...
}

Dayjs plugins

One thing we could optimize is, even not using directly, we need to import relativeTime dayjs plugin at js/src/common/utils/humanTime.ts. Without this import, typescript can't pick up tipes for the fromNow() method and complains.
The code still works though, because relativeTime plugin is installed at js/src/common/index.js:

https://github.com/flarum/core/blob/2ff46f5a2ebe2532f780f6e60a921beb41876d69/js/src/common/index.js#L12-L16

We can avoid this import if we move this plugin installation process to a separated dayjs library module, something like:

// js/src/common/lib/dayjs.ts

import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime';

dayjs.extend(relativeTime)

export default dayjs


// js/src/common/utils/humanTime.ts

// no need import relativeTime from 'dayjs/plugin/relativeTime';
import dayjs from '../../common/lib/dayjs'

Should I open a new issue for this? See #2417

@askvortsov1
Copy link
Sponsor Member

So, apparently webpack doesn't inject the module resolving code on each module, but use a global one. Here's the test i did.

Thank you for testing this!

We can avoid this import if we move this plugin installation process to a separated dayjs library module, something like:

Hmm I really like this idea! @clarkwinkelmann @rob006 could this also work for https://discuss.flarum.org/d/25196-beta-14-dayjs-translations?

@dsevillamartin
Copy link
Member

dsevillamartin commented Oct 26, 2020

@askvortsov1 It could technically work but the config files would need to use flarum.core.compat['flarum/lib/dayjs'] to access it.

We could also just have language packs set the JS objects to an app.* property and have the language logic set the dayjs locale itself with that data.

@lhsazevedo
Copy link
Contributor Author

It seems that it would be a more drastic change, and the code already works without it, so I created a new issue for this subject so that we can move forward here.

Copy link
Member

@clarkwinkelmann clarkwinkelmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm triggered by the imports order ahah (fullTime imports dayjs first, while humanTime imports Mithril first 🙉 )

Looks good otherwise.

@lhsazevedo
Copy link
Contributor Author

@clarkwinkelmann Fixed following this rule: external imports first, internal last, both alphabetically sorted.

@askvortsov1 askvortsov1 merged commit 52e45aa into flarum:master Oct 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants