globalizejs / globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
https://globalizejs.com
MIT License
4.8k stars 603 forks source link

[Breaking] Precompiled globalize from 1.4.2 is not loading in 1.5.0 #895

Open compulim opened 4 years ago

compulim commented 4 years ago

Background

Using globalize-compiler@1.1.1, we compiled CLDR data using globalize@1.4.2.

But when we load the compiled code using globalize@1.5.0/dist/globalize-runtime, we are seeing the following error.

                return partsJoin( numberToPartsFormatter( value ));
                                  ^

TypeError: numberToPartsFormatter is not a function
    at Object.numberFormatter (...\test-globalize-1.5.0\node_modules\globalize\dist\globalize-runtime\number.js:579:21)
    at ...\test-globalize-1.5.0\node_modules\globalize\dist\globalize-runtime\date.js:697:38
    at String.replace (<anonymous>)
    at dateFormat (...\test-globalize-1.5.0\node_modules\globalize\dist\globalize-runtime\date.js:388:21)
    at dateToPartsFormatter (...\test-globalize-1.5.0\node_modules\globalize\dist\globalize-runtime\date.js:1575:10)
    at dateFormatter (...\test-globalize-1.5.0\node_modules\globalize\dist\globalize-runtime\date.js:715:21)
    at Object.<anonymous> (...\test-globalize-1.5.0\index2.js:3:67)
    at Module._compile (internal/modules/cjs/loader.js:734:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:745:10)
    at Module.load (internal/modules/cjs/loader.js:626:32)

Description

I believe the semantic version mechanism in globalize is not honored.

The data generated by 1.4.2, should be able to load by 1.5.0. If the older data is not intended to be loadable, the runtime engine should be bumped to 2.0.0.

We checked runtime of 1.4.3 can successfully load data compiled from 1.4.2.

We are developing a web UI component package and we packaged the precompiled globalize.js (1.4.2) in our NPM package. As globalize recently released as 1.5.0 about 2 days ago, all new installs of our web UI package start picking up globalize@1.5.0 and it's breaking our component because the data cannot be loaded with the newer runtime.

Diff of precompiled globalize.js

(Minus is from 1.4.2, plus is from 1.5.0)

  (function( root, factory ) {

    // UMD returnExports
    if ( typeof define === "function" && define.amd ) {

      // AMD
      define( ["globalize/dist/globalize-runtime/number","globalize/dist/globalize-runtime/date"], factory );
    } else if ( typeof exports === "object" ) {

      // Node, CommonJS
      module.exports = factory( require("globalize/dist/globalize-runtime/number"), require("globalize/dist/globalize-runtime/date") );
    } else {

      // Global
      factory( root.Globalize );
    }
  }( this, function( Globalize ) {

  var numberRound = Globalize._numberRound;
+ var numberToPartsFormatterFn = Globalize._numberToPartsFormatterFn;
  var numberFormatterFn = Globalize._numberFormatterFn;
  var dateToPartsFormatterFn = Globalize._dateToPartsFormatterFn;
  var dateFormatterFn = Globalize._dateFormatterFn;

+ Globalize.a821123423 = numberToPartsFormatterFn(["",,1,0,0,,,,,,"","0","-0","-","",numberRound(),"∞","NaN",{".":".",",":",","%":"%","+":"+","-":"-","E":"E","‰":"‰"},]);
+ Globalize.b314646955 = numberToPartsFormatterFn(["",,2,0,0,,,,,,"","00","-00","-","",numberRound(),"∞","NaN",{".":".",",":",","%":"%","+":"+","-":"-","E":"E","‰":"‰"},]);
- Globalize.a1378886668 = numberFormatterFn(["",,1,0,0,,,,,,"","0","-0","-","",numberRound(),"∞","NaN",{".":".",",":",","%":"%","+":"+","-":"-","E":"E","‰":"‰"},]);
+ Globalize.a1378886668 = numberFormatterFn(Globalize("en").numberToPartsFormatter({"raw":"0"}));
- Globalize.b203855544 = numberFormatterFn(["",,2,0,0,,,,,,"","00","-00","-","",numberRound(),"∞","NaN",{".":".",",":",","%":"%","+":"+","-":"-","E":"E","‰":"‰"},]);
+ Globalize.b203855544 = numberFormatterFn(Globalize("en").numberToPartsFormatter({"raw":"00"}));
  Globalize.b2037043728 = dateToPartsFormatterFn({"1":Globalize("en").numberFormatter({"raw":"0"}),"2":Globalize("en").numberFormatter({"raw":"00"})}, {"pattern":"MMMM d 'at' h:mm a","timeSeparator":":","months":{"M":{"4":{"1":"January","2":"February","3":"March","4":"April","5":"May","6":"June","7":"July","8":"August","9":"September","10":"October","11":"November","12":"December"}}},"dayPeriods":{"am":"AM","pm":"PM"}});
  Globalize.b1609185159 = dateFormatterFn(Globalize("en").dateToPartsFormatter({"skeleton":"MMMMdhm"}));

  return Globalize;

  }));

Repro steps

  1. Clone my repository at https://github.com/compulim/test-globalize-1.5.0
  2. npm install
    • This will install globalize@1.4.2
  3. node compile
    • This will generate a PrecompiledGlobalize.js file
  4. npm install globalize@1.5.0
  5. node run
    • This will load the data from PrecompiledGlobalize.js with globalize@1.5.0/dist/globalize-runtime
dbrugger commented 4 years ago

we were running into the same issue, with a slight variation though: globalize@1.5.0 (and 1.6.0) runtime threw the same error when using data compiled with globalize-compiler 1.0.0 (and no variation in the runtime)