[React Intl] Use Webpack to Conditionally Include an Intl Polyfill for Older Browsers
2023-09-14 08:59:18 时间
Some browsers, such as Safari < 10
& IE < 11
, do not support the JavaScript Internationalization API, which react-intl
depends on. In order to support these browsers, we’ll conditionally include an Intl polyfill using webpack require.ensure
. This ensures only browsers that need the polyfill incur the extra load.
if (!window.Intl) { require.ensure([ 'intl', 'intl/locale-data/jsonp/en.js', 'intl/locale-data/jsonp/fr.js', 'intl/locale-data/jsonp/es.js' ], (require) => { require('intl'); require('intl/locale-data/jsonp/en.js'); require('intl/locale-data/jsonp/fr.js'); require('intl/locale-data/jsonp/es.js'); runApp(); }) } else { runApp(); } function runApp() { addLocaleData([...en, ...fr, ...es]); let locale = (navigator.languages && navigator.languages[0]) || navigator.language || navigator.userLanguage || 'en-US'; ReactDOM.render( <IntlProvider locale={locale} messages={flattenMessages(messages[locale])}> <App /> </IntlProvider>, document.getElementById('root') ); }
相关文章
- webpack
- 如何用 esbuild 替换 Create React App 中的 Webpack
- React v18.x 在 react-router v6 使用 lazy 动态加载组件实现
- Cannot find module 'webpack-cli/bin/config-yargs'
- 笔记 | 一些常用的 webpack 配置
- React Error: Failed at the xxx@0.1.0 start script 'react-scripts start'.
- pwa+webpack,初探与踩坑
- webpack版本选择_webpack官网
- webbench源码分析_webpack原理和机制
- 入门webpack的最佳实践(基于webpack4.X 5.X)--打包速度优化
- webstorm必装十大插件_webpack常用插件
- webpack-ES6转ES5[通俗易懂]
- Webpack最佳实践
- webpack优化
- webpack 4.x 之安装与简单使用操作
- react的jsx和React.createElement是什么关系?面试常问_2023-02-27
- 从react源码角度看React-Hydrate原理
- webpack-dev-server使用报错
- Webpack实现多页面打包
- 8-1. 「webpack源码分析」一个具体案例再次深入看buildChunkGraph的运行过程
- 基于webpack,不使用任何脚手架,创建纯粹的webpack项目