Third-party addon changes to support new script minifier

0 favourites
  • 5 posts
From the Asset Store
Comprehensive localization solution for Construct 3 projects, no addons required!
  • 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.

  • Hi,

    For addon developers, it seems calling anything global likeC3 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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 C3 without 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 C3 without self.

  • Oh right. Thanks, I just realized, that makes it easier.

  • Additional note to support the new minifier:

    1. Globalize these: self.C3, self.SDK, self.lang
    2. Globalize these for domSide.js: self.RuntimeInterface, self.DOMElementHandler
    3. Duplicate let / const / class / function declaration in the same scope is not allowed.
    4. JavaScript Numeric Separators are not supported, ex: 1_000_000 (Not yet).
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)