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(self.MyGlobal)
or 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";
console.log(MyGlobal);
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";
console.log(self.MyGlobal);
If you use global variables a lot, remember you can refer to them with local variables to help simplify the code, e.g.:
function UsesGlobalsRepeatedly()
{
// 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.