Search code examples
google-closure

Google Closure loaded from two separate hosts


I have a site (www.myhost.com) which uses Google Closure uncompiled (the modules are loaded through deps.js).

I would like to import another Google Closure application from another host. (e.g. www.otherhost.com).

In order to accomplish this, I load the deps file from the other host using a script tag:

<script src="<http://www.otherhost.com/libraries/deps.js" type="text/javascript"></script>

The deps.js is loaded just fine. Following that line I have the this goog.require statement:

 goog.require('MyObject');

The object is not loaded properly because the dependencies loaded in deps.js are relative references (eg ../..) and closure tries to load them from www.myhost.com

Is there a way to load closure objects from two different hosts?


Solution

  • Here is the code which solves the problem. Basically, Closure uses a global variable, goog.basePath, to load the deps. Changing this to the other server caused the deps to be loaded from the proper place. Due to proper use of namespacing between the two servers, our global object was created correctly and no duplicates were found.

       var savedBasedPath = goog.basePath;
       goog.basePath = 'http://www.otherhost.com/libraries/closure/goog/';
       goog.require('MyObject');
       goog.basePath = savedBasedPath;
    

    A suggestion to the Google Closure team would be to allow an optional parameter to goog.require which is the basePath, eliminating the need for this hack.