Skip to content

Commit

Permalink
fix: check PluralRules & RelativeTimeFormat lazily instead
Browse files Browse the repository at this point in the history
fixes #1487
  • Loading branch information
longlho committed Sep 18, 2019
1 parent 1fdb186 commit 3ed95fe
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
1 change: 0 additions & 1 deletion examples/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'core-js/stable';
import '@formatjs/intl-pluralrules/polyfill';
import '@formatjs/intl-relativetimeformat/polyfill';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Timezone from './TimeZone';
Expand Down
2 changes: 1 addition & 1 deletion src/components/relative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/

import '@formatjs/intl-relativetimeformat/polyfill';
import * as React from 'react';
import {Context} from './injectIntl';
import {FormatRelativeTimeOptions} from '../types';
Expand Down
7 changes: 7 additions & 0 deletions src/formatters/plural.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export function formatPlural(
value: Parameters<IntlFormatters['formatPlural']>[0],
options: Parameters<IntlFormatters['formatPlural']>[1] = {}
) {
if (!Intl.PluralRules) {
onError(
createError(`Intl.PluralRules is not available in this environment.
Try polyfilling it using "@formatjs/intl-pluralrules"
`)
);
}
let filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS);

try {
Expand Down
8 changes: 8 additions & 0 deletions src/formatters/relativeTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export function formatRelativeTime(
if (!unit) {
unit = 'second';
}
const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
if (!RelativeTimeFormat) {
config.onError(
createError(`Intl.RelativeTimeFormat is not available in this environment.
Try polyfilling it using "@formatjs/intl-relativetimeformat"
`)
);
}
try {
return getFormatter(config, getRelativeTimeFormat, options).format(
value,
Expand Down
10 changes: 0 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,6 @@ export function createIntlCache(): IntlCache {
*/
export function createFormatters(cache: IntlCache = createIntlCache()) {
const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
if (!RelativeTimeFormat) {
throw new Error(`Intl.RelativeTimeFormat is not available in this environment.
Try polyfilling it using "@formatjs/intl-relativetimeformat"
`);
}
if (!Intl.PluralRules) {
throw new Error(`Intl.PluralRules is not available in this environment.
Try polyfilling it using "@formatjs/intl-pluralrules"
`);
}
return {
getDateTimeFormat: memoizeIntlConstructor(
Intl.DateTimeFormat,
Expand Down

0 comments on commit 3ed95fe

Please sign in to comment.