Plugin developers: please test with the minifier

For developers using the Construct 2 Javascript SDK

Post » Mon Oct 24, 2011 1:48 am

Hi plugin developers,

Just a heads up - we're getting bug reports that projects don't work after exporting with minifying enabled. I'm pretty sure this is because of third party plugins.

The minifier renames object properties. If you mix object.prop and object["prop"] syntax your plugin will probably break the project on export. From the SDK documentation:

[quote]When exporting, Construct 2 gives the user the option to 'Minify script'. This runs the common and runtime scripts through Google Closure Compiler's ADVANCED_OPTIMIZATIONS mode. This imposes some limitations on what scripts can do. You must obey these limitations when writing your plugins, otherwise your plugin will be broken on export. More details can be found here (http://code.google.com/closure/compiler/docs/api-tutorial3.html).

The main thing is to always use dot syntax (Object.property) rather than bracket syntax (Object["property"]). All properties using dot syntax are changed by Closure Compiler, but none of the properties in bracket syntax are changed. Therefore, if you use Object.property in one place and Object["property"] in another to access the same property, the plugin will be broken on export. You may still use bracket syntax (e.g. for a dictionary of user-inputted strings) - just be aware of how Closure Compiler will transform the code.

Remember the edittime scripts are not passed through Google Closure Compiler, so you can write them how you like.[/quote]

Please double check all your plugins correctly minify on export! And don't forget to read the documentation! Ashley2011-10-31 12:21:43
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Mon Oct 24, 2011 3:38 am

Thanks for the notification.rexrainbow2011-10-24 10:26:56
B
97
S
22
G
178
Posts: 4,119
Reputation: 104,045

Post » Tue Nov 01, 2011 10:22 pm

Perhaps having a plugin "lint" page that does what the minified would do and highlight potential problems might be helpful? Folks might paste their plugin code and run "lint" and it highlights parts of the code that could be problems. Just my 2 cents.
B
18
S
3
G
5
Posts: 41
Reputation: 4,398

Post » Thu Nov 24, 2011 12:13 pm

Looks like we have to avoid using {"propertyname":value} pairs as well.

For example, this doesn't work:
[code]var test = {"count":0, "text" : "Some text"}[/code]

Instead, use this:
[code]var test = {};
test.count = 0;
test.text = "Some text";[/code]
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Fri Nov 25, 2011 4:11 pm

@Mipey, doing
[code]var test = {"count":0, "text" : "Some text"}[/code]
should be fine, so long as you access it with
[code]test["count"] = ...;
test["text"] = ...'[/code]
The minifier works by mangling all .property accesses, but never changes anything "in quotes". So you only break things by mixing and matching.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Fri Nov 25, 2011 8:50 pm

Good to know, thanks!
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Wed Nov 30, 2011 3:10 am

You guys are awesome

understanding all codes...

im so noob in front of you guys
(i just started to learn c++ and im twelve)
B
35
S
16
G
16
Posts: 2,222
Reputation: 16,589

Post » Wed Dec 21, 2011 2:00 am

My plugin makes heavy use of an external script... lots of Playtomic.Leaderboards.Save(data), Playtomic.Data.Views, etcetcetc

This caused problems with the minifier and I ended up having to change everything to Playtomic["Leaderboards"]["Save"] and so on.

So... it runs fine now, but it just looks a little strange to me. Did I do it right?
B
22
S
9
G
5
Posts: 122
Reputation: 5,386

Post » Wed Dec 21, 2011 2:11 am

Yes, that looks right, but the 'Playtomic' variable can be renamed by the minifier too. It needs to be accessed by [""] syntax as well. The best thing to do is a global variable within the anonymous function for the plugin code, e.g.

var myPlaytomic = window["Playtomic"];

The external script has to have finished loading before this will work! This then always refers to the global variable by that name. Then, use

myPlaytomic["Leaderboards"]["Save"]

and so on. The syntax isn't very pretty, but it's really important to stick with it for the minifier to work.Ashley2011-12-21 02:12:00
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Wed Dec 21, 2011 3:05 am

Hi Ashley,

Could it possible to mark a non-minify code area?
I know the constrain of minify. The minify not only change the variable name, but also 'compile' the code. The behavior before minify/after minify 'might be' different.
(Does physical behavior have this problem before?)rexrainbow2011-12-21 03:06:07
B
97
S
22
G
178
Posts: 4,119
Reputation: 104,045

Next

Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests