eemeli / intl-pluralrules

Polyfill for Intl.PluralRules
https://www.npmjs.com/package/intl-pluralrules
ISC License
41 stars 7 forks source link

Bug in 1.1.0, TypeError this._select is not a function #12

Closed patrickhulce closed 5 years ago

patrickhulce commented 5 years ago

Howdy and thanks for the great package!

When using intl-messageformat from node in Lighthouse we get a fatal error since 1.1.0, lots of users started reporting this in the past few hours and we didn't have this issue in 1.0.3 (reverting fixes our users' bug).

Any chance a quick fix or revert of 1.1.0 in the meantime would be possible? :)

TypeError: this._select is not a function
    at PluralRules.select (/home/lighthouse/node_modules/intl-pluralrules/factory.js:143:21)
    at PluralFormat.getOption (/home/lighthouse/node_modules/intl-messageformat/dist/compiler.js:161:38)
    at formatPatterns (/home/lighthouse/node_modules/intl-messageformat/dist/core.js:69:43)
    at IntlMessageFormat.format (/home/lighthouse/node_modules/intl-messageformat/dist/core.js:139:24)
brendankenny commented 5 years ago

It looks like this was due to module/build changes? The failure starts here:

https://github.com/eemeli/intl-pluralrules/blob/6d1cb83389e097a2e634706a2d2eb2fe778d5e49/src/plural-rules.mjs#L12

getSelector('en') returns undefined (and the exception is thrown trying to call the return value).

It appears this was due to a change in the Plurals object there. In 1.0.3 it has all the locales directly on it, so Plurals is

{af: ..., ak: ..., am: ...}

and Plurals.en exists, but with the new module setup it's nesting within default, so Plurals is

{default: {af: ..., ak: ..., am: ...}})

so it looks like no locale lookups will succeed.

eemeli commented 5 years ago

The actual problem was a bit deeper, in make-plural. The shape of the exports of that package changed to support tree-shaking, and I had not taken into account how code that is Babel-transpiled from ES modules to CommonJS wraps the package when importing e.g. make-plural/plurals.js as intl-pluralrules/plural-rules.js does.

To fix, I've added the missing __esModules: true property upstream, which should fix the issue. I've also released a patch version 1.1.1 of intl-pluralrules that requires at least the patched version 6.0.1 of make-plural.

brendankenny commented 5 years ago

thanks for the quick fix!

patrickhulce commented 5 years ago

thanks very much @eemeli !