Add modern ESM subpackages

The existing ESM entry points (libphonenumber-js, /min, /max, /mobile, /core) are named "es6" internally, but they aren't actually modern-syntax builds: @babel/preset-env runs without a target and therefore still transpiles classes, destructuring, for...of, etc. to older JavaScript. That keeps backwards compatibility, but modern consumers pay for Babel helper code that their own browser targets don't need.

Keep those existing entry points unchanged and add opt-in modern subpackages instead:

  • libphonenumber-js/min/es6
  • libphonenumber-js/max/es6
  • libphonenumber-js/mobile/es6
  • libphonenumber-js/core/es6

The default package entry points remain on the current transpilation path, so this is not a breaking change. Consumers that want the smaller modern build can opt in explicitly by importing from the new /es6 subpaths.

The new BABEL_ENV=es6-modern build writes source modules to es6-modern/ using @babel/preset-env with modules: false, bugfixes: true, loose: true and targets: { esmodules: true }. That lets Babel choose the native-ES-module browser cutoff from its own compat data instead of maintaining a hand-picked browser version list in this package.

The /es6 subpackage entry files follow the existing package layout: core/es6 points at the modern Babel output, while /min/es6, /max/es6 and /mobile/es6 mirror their current barrels and export wrappers with imports redirected through core/es6. Only the class wrappers are adjusted to use native extends/super, because the modern core build emits native classes, while preserving PhoneNumber instanceof compatibility for parsed phone numbers.

The existing BABEL_ENV=es6 build is preserved as the status quo. Its output remains byte-for-byte identical to the previous config; transform-for-of is now scoped to the legacy ESM/CommonJS/coverage build environments rather than being applied globally.

Measured with a consumer bundle using Rollup + Terser (ecma: 2020) and a public-API import set:

min    | 177,666 B | 140,563 B | 42,081 B | 38,223 B 
max    | 251,327 B | 214,224 B | 62,690 B | 58,750 B
mobile | 193,117 B | 156,014 B | 46,712 B | 42,682 B
core   | 93,076 B  |  56,158 B | 22,308 B | 18,223 B

That is roughly 37 KB raw and 3.8-4.1 KB gzip saved, while preserving the current default behavior for existing users.

Edited by Matt d'Entremont

Merge request reports

Loading