Advice regarding minification

For developers using the Construct 2 Javascript SDK

Post » Sun May 19, 2013 3:15 pm

Hi all,

So for my Windows Phone plugin, I have some C# that calls JavaScript functions by name. It's the only way to create intercommunication between native code and the JavaScript.

Of course this causes problems when minifying as the function names are no longer human-readable. Ideally I'd just wrap my code with some tags to define an area that remains unminified, though this isn't supported.

Does anyone have any other ideas how this could be implemented?
Moderator
B
72
S
14
G
11
Posts: 900
Reputation: 11,920

Post » Sun May 19, 2013 3:39 pm

Closure Compiler doesn't rename anything using string syntax (Object["property"]). Everything using dot syntax (Object.property) is renamed.

So if you add a function to the window object using string syntax:

window["MyFunction"] = function () {...};

you should be able to call it from C# using the same syntax, and it will survive minification, e.g. by executing the string:

"window[\"MyFunction\"]();"

Usually for robustness I add a check that the function exists, to prevent it crashing in case it tries to execute that before your plugin has created the function:

"if (window[\"MyFunction\"]) window[\"MyFunction\"]();"

In short, if anything is sensitive to being renamed, use string syntax.Ashley2013-05-19 15:40:05
Scirra Founder
B
395
S
231
G
88
Posts: 24,367
Reputation: 193,684

Post » Sun May 19, 2013 4:25 pm

@Ashley excellent! Thanks for the advice!
Moderator
B
72
S
14
G
11
Posts: 900
Reputation: 11,920


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests