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

Add named exports for createInstance #1681

Merged
merged 14 commits into from Nov 15, 2021
Merged

Add named exports for createInstance #1681

merged 14 commits into from Nov 15, 2021

Conversation

perrin4869
Copy link
Contributor

Updating i18next-fetch-backend to be an esm-first module, I found that I could not do import { createInstance } from "i18next" like I expected.
This PR adds support for this.

@jamuhl
Copy link
Member

jamuhl commented Oct 24, 2021

Don't find the issue but we had issues with mixed export in the past.

@jamuhl
Copy link
Member

jamuhl commented Oct 24, 2021

@coveralls
Copy link

coveralls commented Oct 24, 2021

Coverage Status

Coverage increased (+0.07%) to 90.871% when pulling 40d814b on perrin4869-patch-1 into 9ad73d5 on master.

@perrin4869
Copy link
Contributor Author

Oh I see! Yeah that would not have worked then.
I tried a different approach here, it adds named exports only on the esm module and marks it as type: "module" so that it can be loaded natively by node.js.

@jamuhl jamuhl requested a review from adrai October 25, 2021 05:40
@jamuhl
Copy link
Member

jamuhl commented Oct 25, 2021

@adrai what is your opinion? I think either we keep the thing as is - or bring back all named exports (personally I think named exports are overused on things that are objects and not pure functional)

@adrai
Copy link
Member

adrai commented Oct 25, 2021

Personally, I would not mix default exports and named exports...
The only thing we could do is better deconstruction support... like spiked here: https://github.com/adrai/i18next-next/blob/main/src/i18next.js#L624
image
This way you could do something like this:

import i18next from 'i18next';
const { createInstance } = i18next;

@perrin4869
Copy link
Contributor Author

createInstance btw is just a pure function, it even has a rule to disable eslint above it.

  /* eslint class-methods-use-this: 0 */
  createInstance(options = {}, callback) {
    return new I18n(options, callback);
  }

I still think this one can be safely added as a named export...

@jamuhl
Copy link
Member

jamuhl commented Oct 25, 2021

with pure function I mean it is just a function (not part of a class)

@perrin4869
Copy link
Contributor Author

Is there a reason createInstance is attached to a class's prototype? Is there a need to call it from a class instance?

@jamuhl
Copy link
Member

jamuhl commented Oct 25, 2021

instance.createInstance is a pattern used since long before deconstruction was a thing

@jamuhl
Copy link
Member

jamuhl commented Oct 25, 2021

Not even sure why this is such a big deal...most projects never will need createInstance anyway...there is nothing you gain by the named import

@perrin4869
Copy link
Contributor Author

You're absolutely right, it's not a big deal. It's just a preference man. You gain slightly nicer ergonomics.
I'm doing routine maintenance of some of my modules, and when updating i18next-fetch-backend I ran into this.
https://github.com/dotcore64/i18next-fetch-backend/blob/c30759a7703d63e365b7e4e9500e832724cc5ad6/test/index.js#L11
It just looks off, and it's something that I could quickly fix.
I also confirmed that node16 can load this module as native ESM with no troubles.

I think that if anything, the pattern would be to make createInstance a static method... I think that @babel/preset-env supports this out of the box now...
Feel free to close this if it's a no-go!

@adrai adrai mentioned this pull request Oct 26, 2021
3 tasks
adrai pushed a commit that referenced this pull request Nov 2, 2021
* Fix spelling

* Allow using t as a free function

This facilitates using t via destructuring or as a callback.  Several people have run into this; see #1287.

* Use a fixed time zone for automated tests

This fixes some test failures in some time zones (such as Eastern US).

* Bind all functions, not just t

As discussed at #1681 and #1682.
@stale
Copy link

stale bot commented Nov 9, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 9, 2021
@stale stale bot removed the stale label Nov 14, 2021
@perrin4869
Copy link
Contributor Author

Solved the merge conflicts and updated to reflect #1682. I actually agree with @jamuhl and don't particularly like #1682, originally all I wanted was to be able to import { createInstance } from 'i18next' XD
but since that one was merged, I went ahead and made the ESM module behave like the cjs. I also moved the binding outside of the constructor, so that only the members of the exported i18next instance are bound... having all functions bound to a member is just not intuitive...

@adrai
Copy link
Member

adrai commented Nov 14, 2021

outside of the constructor

This will probably not automatically work for cloneInstance and createInstance now: https://github.com/i18next/i18next/blob/master/src/i18next.js#L569

@perrin4869
Copy link
Contributor Author

Hm... I think there would be no problem calling those 2...
Nevertheless, it doesn't make much sense to bind cloneInstance to begin with - it's right there in the name :P
And createInstance i changed into a static method and it is bound separately.

@adrai
Copy link
Member

adrai commented Nov 14, 2021

I mean the bindMemberFunctions call is probably also needed to be called here: https://github.com/i18next/i18next/pull/1681/files#diff-6859f2a371e0f563ec9c27c5cf64cb82f8d91750467509caaa8f6f0e6d06dec5R565

@perrin4869
Copy link
Contributor Author

Ah, i see what you mean! I did that on purpose, i think binding on the default export instance is enough...
Do you think it's better to bind all instances?
I don't like the idea cause it could lead to unexpected or unintuitive behavior...

@adrai
Copy link
Member

adrai commented Nov 14, 2021

Ah, i see what you mean! I did that on purpose, i think binding on the default export instance is enough...
Do you think it's better to bind all instances?
I don't like the idea cause it could lead to unexpected or unintuitive behavior...

what do you think @joshkel @dinosaurjoe @nielsboecker @minodisk @stephent?

package.json Outdated Show resolved Hide resolved
rollup.config.js Outdated Show resolved Hide resolved
src/index.esm.js Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
rollup.config.js Outdated Show resolved Hide resolved
@adrai
Copy link
Member

adrai commented Nov 15, 2021

lgtm now...
just waiting for @joshkel's feedback for the bind question: #1681 (comment)

@joshkel
Copy link
Contributor

joshkel commented Nov 15, 2021

For my particular use case, I believe that binding on the default instance would be enough.

I would lean toward binding all instances; that feels more consistent and removes any possibility of error. But I'll defer to whatever others decide. Thank you.

@adrai
Copy link
Member

adrai commented Nov 15, 2021

I would lean toward binding all instances; that feels more consistent and removes any possibility of error. But I'll defer to whatever others decide. Thank you.

So let's do it for all instance creations ok @perrin4869 ?

@adrai adrai merged commit e401175 into master Nov 15, 2021
@perrin4869
Copy link
Contributor Author

Done!
I think that some people might not expect the instance functions to be bound, in general people don't bind their instances... don't know of other libs that do this... but let's go with this!

@adrai
Copy link
Member

adrai commented Nov 15, 2021

thank you for everything you've done @perrin4869 v21.5.0 has just been released

@stryaponoff
Copy link

I've got the warning after upgrading to v21.5.0 with Yarn 3.1.0:

warn Package i18next has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports"

But it seems to work anyway without any problems.

adrai added a commit that referenced this pull request Nov 16, 2021
@adrai
Copy link
Member

adrai commented Nov 16, 2021

I've got the warning after upgrading to v21.5.0 with Yarn 3.1.0:

warn Package i18next has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports"

But it seems to work anyway without any problems.

@stryaponoff it should be better with v21.5.1

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

6 participants