Referencing external scripts and Closure

For developers using the Construct 2 Javascript SDK

Post » Thu Aug 02, 2012 8:31 pm

I noticed that the when the Facebook plugin gets minified, the FB api calls like FB.api stay the same to avoid breaking the code that calls those functions. I tried looking in the Facebook runtime.js file but I could not see any code that would instructs Closure to avoid renaming those methods. If I copy and paste the runtime.js into http://closure-compiler.appspot.com/home, the resulting file does rename those functions and gives warning like the following:

JSC_INEXISTENT_PROPERTY: Property api never defined on FB at line 308 character 0
FB.api('/me/feed', 'POST', publish, function(response) {

I am trying to make my copperlicht3d plugin work with minification, but Closure is renaming the CL3D.xxx functions that belong to copperlicht in my code. It renames them to something like CL3D.Ki, etc. Is there anything that I am missing?

BTW, I did read the section of exporting symbols (https://developers.google.com/closure/compiler/docs/api-tutorial3#removal) but I did not see any symbol exporting in the Facebook plugin's runtime file.juantar2012-08-02 20:32:45
B
26
S
7
G
3
Posts: 118
Reputation: 4,264

Post » Fri Aug 03, 2012 12:12 pm

On export C2 also passes <install path>\exporters\html5\c2-externs.js to Closure Compiler as a list of externs to not be renamed (see Closure Compiler's documentation on this). A few FB functions are listed there to guarantee they are not mangled by Closure Compiler.

Third party plugins can't edit this file, so just use the alternative syntax instead: rather than using FB.foo.bar(), use window["FB"]["foo"]["bar"](). This has the same result. A few official plugins take advantage of the c2-externs.js list simply for readability (so they can be written the first way and still not be renamed).
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Fri Aug 03, 2012 9:05 pm

Thank you @Ashley!

That explains why the Facebook API calls did not get mingled.

I will go ahead and declare the externs in my plugin runtime.js file.
B
26
S
7
G
3
Posts: 118
Reputation: 4,264


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests