question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uplift i18n code to use dynamic imports

See original GitHub issue

Feature Request

Description

Currently, we use require.ensure to dynamically load i18n data based on what the current locale is set as. This is based on the recommendation from the Intl polyfill we used. This has worked well, however, webpack recommends using dynamic imports over require.ensure. There is a possibility require.ensure becomes deprecated, however that doesn’t seem likely yet. This issue is to start discussion on the update.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
bjankordcommented, Oct 2, 2018

They’ve added support for dynamic imports in node_modules, in create-react-app v2. create-react-app explicitly prevents usage of require.ensure. Need to test and see how terra-ui components works with this change.

Edit: I’ve explored this some and I was able to get a variation of terra-base / terra-i18n code to work with create-react-app, but had to modify some code.

The aggregated translations pre-build tool would need to support outputting translation and intl loader files that use dynamic imports.

In terra-core, we use require.ensure in defaultIntlLoaders.js, defaultTranslationsLoaders.js, and the i18nLoader.js files.

These instances of require.ensure can all be flipped over to dynamic imports, however during the babel compilation / transformation process for code we distrubute in lib, the dynamic import code will be output as require.ensure instead of the dynamic import syntax so these files in terra-core will not work in create-react-app, they would work for our Cerner app starter kits.

In create-react-app, we wouldn’t need to use the defaultIntlLoader.js or defaultTranslationsLoader.js files and instead, would use the ones generated via the aggregated-translations pre-build tool, so that part can be solved/mitigated.

This leaves the require.ensure statement in i18nLoader.js to figure out how to adapt if we want to use terra-base / terra-i18n in create-react-app.

At first, I thought it would be challenging to get terra-base/terra-i18n (code-splitting for translations) working in CRA v2 without ejecting, but it seems possible, if we lift the i18nLoader function from terra-i18n/src/i18nLoader.js into terra-base. This would allow people to import Base from ‘terra-base/src/Base’; (would need to avoid using JSX in this file) which would use dynamic import code instead of the transpiled require.ensure syntax.

Until then, it may be easier to show users that want to use CRA v2 how to set up react-intl’s <IntlProvider /> and load in a single aggregated translation file + associated intl data.

1reaction
noahbenhamcommented, Oct 8, 2018

Leaving this here in case anyone has issues with Create React App 2.

Discussed with @bjankord offline - CRA2 consumers will also need to add dir="ltr to the html tag of public/index.html. Without it, this type of behavior could be seen:

screen shot 2018-10-04 at 12 14 32 pm screen shot 2018-10-04 at 12 14 40 pm
Read more comments on GitHub >

github_iconTop Results From Across the Web

Code-splitting react-i18n locales using dynamic imports
When we use dynamic imports to load the file, we should be aware of race conditions between loading the resources and calling the...
Read more >
Beginning JavaScript I18n with i18next and Moment.js | Phrase
Here's how to use i18next and Moment.js for your custom JavaScript ... Instead, we dynamically import the given locale on initialization.
Read more >
Dynamic imports and code splitting with Next.js
Dynamic imports, also known as code splitting, refers to the practice of dividing bundles of JavaScript code into smaller chunks, which are then ......
Read more >
Code Splitting With Dynamic Module Imports
A simple explanation of how to take advantage of dynamic imports to make your site load faster.
Read more >
25.33% Reduction in First Load JS with NextJS Dynamic Imports
... load JS bundle size by 25% with code splitting via dynamic imports, ... and you'd be doing yourself a disservice by not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found