Uplift i18n code to use dynamic imports
See original GitHub issueFeature 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:
- Created 5 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 inlib
, the dynamic import code will be output asrequire.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
ordefaultTranslationsLoader.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 transpiledrequire.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.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 thehtml
tag ofpublic/index.html
. Without it, this type of behavior could be seen: