Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

🐛 Make the webpack plugins return regular objects intead of Modules #618

Merged
merged 3 commits into from
Mar 29, 2019

Conversation

amrocha
Copy link
Contributor

@amrocha amrocha commented Mar 28, 2019

Currently the webpack plugin in react-i18n gives consumers a Module object to work with. This isn't what's expected, and this PR changes it to return a simple Object instead.

Dynamic import statements return object-like data primitives called Modules. These modules behave roughly like an object in terms of access, but don't have any of the standard Object functions since they're not created with an Object as a prototype.

I believe the expectation here is that react-i18n return a standard object for people to manipulate, not a Module, hence this change. For example, this caused a regression in web when @shopify/polaris was updated to use a custom simplified merge function, instead of lodash's merge function. lodash could deal with Modules, but the new simplified function expects simple Objects.

@amrocha amrocha requested a review from lemonmade March 28, 2019 23:22
@amrocha amrocha force-pushed the react-i18n-simple-object-import branch from d91ca56 to 43fd2e8 Compare March 28, 2019 23:46
@amrocha
Copy link
Contributor Author

amrocha commented Mar 28, 2019

I considered doing a one liner like:

    return {
      ...(await import(/* webpackChunkName: "Locales-i18n-[request]" */ `locales/${locale}.json`)),
    };

But the two-step approach seemed clearer to me. Opinions?

Copy link
Member

@lemonmade lemonmade left a comment

Choose a reason for hiding this comment

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

Thanks for taking a look at this. I think the more appropriate solution is to return the default export of the module, though.

@amrocha
Copy link
Contributor Author

amrocha commented Mar 29, 2019

Oh you're right, I didn't realize we could do that as well. Updated the PR :D

@@ -37,7 +37,8 @@ describe('babel-pluin-react-i18n', () => {
id: 'MyComponent_${defaultHash}',
fallback: _en,
async translations(locale) {
return await import(/* webpackChunkName: "MyComponent_${defaultHash}-i18n", webpackMode: "lazy-once" */ \`./translations/$\{locale}.json\`);
const dictionary = await import(/* webpackChunkName: "MyComponent_${defaultHash}-i18n", webpackMode: "lazy-once" */ \`./translations/$\{locale}.json\`);
return dictionary.default;
Copy link
Member

Choose a reason for hiding this comment

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

I think it may be safer to do dictionary && dictionary.default since there is not a 100% guarantee a module will be resolved totally correctly (and undefined is a valid value to return)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, especially since this is added to the code post-compile. Done!

@amrocha amrocha force-pushed the react-i18n-simple-object-import branch from cc8d2ae to 3bce59e Compare March 29, 2019 17:59
@amrocha amrocha force-pushed the react-i18n-simple-object-import branch from 33a2f1c to fe3ca32 Compare March 29, 2019 18:15
@amrocha amrocha changed the title Make the webpack plugins return regular objects intead of Modules 🐛 Make the webpack plugins return regular objects intead of Modules Mar 29, 2019
@amrocha amrocha changed the title 🐛 Make the webpack plugins return regular objects intead of Modules 🐛 Make the webpack plugins return regular objects intead of Modules Mar 29, 2019
@amrocha amrocha merged commit efd1191 into master Mar 29, 2019
@amrocha amrocha deleted the react-i18n-simple-object-import branch March 29, 2019 18:42
cartogram pushed a commit that referenced this pull request Apr 1, 2019
…o hooks__react-shortcuts

* 'hooks__react-shortcuts' of github.com:Shopify/quilt:
  yarn lock
  feedback
  Adds useShortcut hook
  Publish
  React testing improvements (#621)
  Publish
  🐛Make the webpack plugins return regular objects intead of Modules (#618)
  Publish
  @shopify/react-testing (#603)
  Publish
  update changelog
  ✅ updates tests to use GraphQL success response
  🐛 check success via valid webhookSubscription field
  Publish
  adding changelog entry and docs for #601
  Add client submit errors to errors in render prop
  Publish
  Update trigger to support act() (#612)
  Add a promise mock (#614)
cartogram pushed a commit that referenced this pull request Apr 2, 2019
)

🐛 Make the webpack plugins return regular objects intead of Modules
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants