How to speed up loading the Google closure library?

I am writing a simple phone parser based on [ libphonenumber ]. Unfortunately, "http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js" loads forever, and when I close the file and just turn it on as src = "base.js", it appears a lot of mistakes.

I assume this is because the library is not loaded yet, therefore goog.require () instructions do not work.

What can I do?

<!DOCTYPE html> <html> <head> <title>Phone Number Parser</title> <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script> <script> goog.require('goog.dom'); goog.require('goog.json'); goog.require('goog.proto2.ObjectSerializer'); goog.require('goog.string.StringBuffer'); </script> <script src="phonemetadata.pb.js"></script> <script src="phonenumber.pb.js"></script> <script src="metadata.js"></script> <script src="phonenumberutil.js"></script> </head> <body> <script> numbers = ['6509066389', '+40723875777', '720-935-6433', '914-262-7178', '7123040634']; for (i in numbers) { console.log(format_for_five9(numbers[i])); } function format_for_five9(phoneNumber) { var $ = goog.dom.getElement; var regionCode = 'US'; var output = new goog.string.StringBuffer(); try { var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance(); var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode); number_json_serial = goog.json.serialize(new goog.proto2.ObjectSerializer(goog.proto2.ObjectSerializer.KeyOption.NAME).serialize(number)); number_json = goog.json.parse(number_json_serial); if(phoneUtil.isValidNumberForRegion(number, regionCode)) { five9_format = number_json.national_number.toString(); } else { five9_format = number_json.country_code.toString() + number_json.national_number.toString(); } } catch (e) { output.append('\n' + e); console.log(e); } return five9_format; } </script> </body> </html> 
+4
source share
2 answers

You do not have to link the library directly.

The solution is to download the entire library and place on it the same web server that hosts the above code. You should probably save javascript in the same directory as phonemetadata.pb.js, metadata.js etc. This will allow you to include the script in the same way as everyone else:

<script src="base.js">

You can download Closure via git ( git clone https://github.com/google/closure-library.git ) or as a zip file .

+7
source

The closure library should be used in conjunction with the closure compiler to compile and minimize your javascript for production. It is not intended to be used as a raw file in production. Even in your developer's environment, you can use http://plovr.com/ to dynamically compile and maintain your javascript.

Closing is very detailed in its original form due to annotations of type and structure similar to java, the closure compiler not only minimizes the script, but also optimizes and removes unused scripts in order to speed up the work.

Here's an example of using plovr to dynamically serve your javascript code

 java -Xmx256m -jar plovr.jar serve -p 9811 /path/to/your/closure/config.js 

This will serve the compiled javascript files on localhost: 9811. For production:

 java -jar plovr.jar build /path/to/your/closure/config.js > production.js 

See the documentation at http://plovr.com/docs.html on how to configure plovr to compile or maintain your javascript.

+11
source

All Articles