As of r206, projects exported from Construct 3 using script minification are now processed using Google Closure Compiler. Previously it used babel-minify. For the most part this is a compatible change - simple minification works similarly, and advanced minification does property mangling. However there is one change in how Closure Compiler handles global variables.
Previously we wrote our own property mangler for babel-minify which supported mangling global variables. However Closure Compiler does not support mangling global variables, unless they are written as properties on the global object. For example
console.log(MyGlobal) used to work, but now no longer does. Instead use a global property, e.g.
console.log(globalThis.MyGlobal). (Note don't use window, because it does not exist in worker mode.)
Here's another example. Previously you may have code like this:
self.MyGlobal = "hello world";
The reference to MyGlobal will no longer compile with advanced minification. All you need to do is add
self. before any MyGlobal references and it will work again:
self.MyGlobal = "hello world";
If you use global variables a lot, remember you can refer to them with local variables to help simplify the code, e.g.:
// Make local variable referring to global variable
const MyGlobal = self.MyGlobal;
// Use local variable without needing "self."
console.log("Use 1: ", MyGlobal);
console.log("Use 2: ", MyGlobal);
console.log("Use 3: ", MyGlobal);
console.log("Use 4: ", MyGlobal);
console.log("Use 5: ", MyGlobal);
For the record, I think Closure Compiler's approach is better. It avoids any chance of difficult name collision bugs when renaming names like MyGlobal, which if the wrong name is chosen, can collide with local variable names. When using self.MyGlobal, it is unambiguous and safer since it can never collide with local variable names.
Hopefully this will be a straightforward change for any addons that use this type of code. If you have third-party addons that now fail with advanced minification, this is probably the change you need to make.
Try Construct 3
Develop games in your browser. Powerful, performant & highly capable.
Construct 3 users don't see these ads
For addon developers, it seems calling anything global like
C3 is causing issues. So scripts like
this.Trigger(C3.Plugins.Sprite.Cnds.OnAnimFinished) wouldn't work anymore.
But, the Addon SDK says otherwise.
Kindly advise. Thank you.
There's nothing special about
C3, it's a global variable and so you have to reference it with
self.C3 like all other global variables.
However if you look at the latest addon SDK downloads, they put
const C3 = self.C3 at the top of the file scope. This lets you reference
self, because it makes it a local variable. You can use this technique for any global variables that are frequently used to avoid having to repeat
self. everywhere. The addon SDK documentation assumes you're working with the latest SDK download which lets you use
Oh right. Thanks, I just realized, that makes it easier.
Additional note to support the new minifier:
- Globalize these:
- Globalize these for domSide.js:
- Duplicate let / const / class / function declaration in the same scope is not allowed.
1_000_000 (Not yet).
There are 1 visitors browsing this topic (0 users and 1 guests)