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

refactor: static createInstance for wider browser support #1973

Merged
merged 1 commit into from
Jun 15, 2023

Conversation

belgattitude
Copy link
Contributor

@belgattitude belgattitude commented Jun 15, 2023

Reason: i18next 23 dropped support for older node and browsers.... The new version runs fine with recent node, but I feel there's an issue regarding the transpilation of

class I18n extends EventEmitter {
  static createInstance = (options = {}, callback) => new I18n(options, callback)
}

It's currently transpiled as

class em extends D {
            static createInstance = function ()  {
                let e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {},
                    t = arguments.length > 1 ? arguments[1] : void 0;
                return new em(e, t)
            };
}

Runs fine from es2022 (not before) ... but browser support I'm less sure how static + function will work (https://caniuse.com/mdn-javascript_classes_static -> 95% / but I suspect that static = function is a different case).

With this PR the transpiled code would be

            static createInstance()  {
                let e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {},
                    t = arguments.length > 1 ? arguments[1] : void 0;
                return new em(e, t)
            };

Which:

  • allow to pass from es2019 to es2022 checks (difficult to exactly refer to node versions, but let's says node 14 is es2020 100% compat)
  • keep the browser baseline at threshold of 95%

Need to dig more...

PS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • commit message and code follows the Developer's Certification of Origin

@coveralls
Copy link

Coverage Status

coverage: 92.332% (-0.004%) from 92.336% when pulling b684f7d on belgattitude:refactor-static-instance into c45439e on i18next:master.

@@ -578,7 +578,7 @@ class I18n extends EventEmitter {
: 'ltr';
}

static createInstance = (options = {}, callback) => new I18n(options, callback)
static createInstance(options = {}, callback) { return new I18n(options, callback) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adrai do you think this change is equivalent ? In my understanding yes. (no late static binding issue, no async, no need for the function / class property), but I guess there is or was a reason..

Copy link
Member

Choose a reason for hiding this comment

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

we may ask @perrin4869 => e401175

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks equivalent to me!
I generally prefer to use arrow functions because it saves the need to use curly braces, so it was probably only an esthetics decision, feel free to change it 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@perrin4869 to give you a bit of context... in the latest I18next v23 we don't transpile for older browser/platforms... but the notation with static xxx = function might bring some issues (does not pass eschecks before es2022). So the idea here is to refactor at the source. See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you so much @perrin4869 !

@belgattitude belgattitude marked this pull request as ready for review June 15, 2023 16:36
@belgattitude
Copy link
Contributor Author

@adrai on my side it seems okay... feel free to merge whenever needed. I'll dig into other issues in the coming days

@adrai adrai merged commit d160a08 into i18next:master Jun 15, 2023
4 of 5 checks passed
@adrai
Copy link
Member

adrai commented Jun 15, 2023

thank you @belgattitude and @perrin4869 v23.0.2 has just been released

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.

None yet

4 participants