Prevent Closure Compiler moving methods out of a class

Viewed 66

I'm trying to write a JavaScript support script for Roll20 and to minify and typecheck it using the Closure Compiler.

Roll20 has very primitive module support and simply catenates together all scripts that are run in one game. Because of this, their style guide states that use of the global namespace should be minimised. In order to do this, my main script runs:

class Pathfinder2Utilities {
   ... methods ...

   constructor() {
     ... adds a roll20 event handler to call methods on the constructed instance ...
   }
}
new Pathfinder2Utilities();

This means that only Pathfinder2Utilities is exposed to the global namespace, and the executing instance of the class is anonymous and takes up no namespace. However, when I attempt to minify this using Closure Compiler, it moves some of the methods out of the class and turns them into global functions with single-letter names. It also adds some similar functions of its own. Since these functions would then clash with any other compiled script this makes the script unsuitable to upload.

The work-around suggested on the Closure Compiler is to add references to the global functions as members of the Pathfinder2Utilities object. But this isn't viable in this case because the global functions will still exist to cause name clashes.

How can I tell Closure to preserve the structure of the Pathfinder2Utilities class?

0 Answers
Related