Object being removed or partailly collapsed when compiling in advanced mode - Google Closure Compiler

132 Views Asked by At

I am using angular with Closure and I am having trouble with the @export annotation. Specifically being able to export property definitions. I thought I had something working with the following code however it seems very temperamental.

/**
 * @export
 * @constructor
 */
com.MyController = function() {
   this.greeting = 'hello';
   this.goodbye = 'bye'
};

/**
 * @export
 */
com.MyController.prototype = {

    'sayGoodbye': function() {
        return this.goodbye;
    },

    'sayHello': function() {
        return this.greeting;
    }
};

Sometimes the compiler will export all the methods on the prototype, other times it will only export 'sayGoodbye' and then other times it will completely collapse the entire object defined on the prototype.

I know I can do this:

/**
 * @export
 */
com.MyController.prototype.sayHello = function() {
    return this.greeting;
};

Which works, however it starts to become very laborious to type this all the time when, as well as it being less readable.

I know there is a option with the compiler to setExportLocalPropertyDefinitions, but the open source version of the code doesn't currently have a flag for setting this - I have been messing with the Java source code to try and set this but with no luck so far.

Update: So it seems the methods on the prototype (eg. sayGoodbye) will get exported if there is another method with the same name in an unrelated class. I guess its getting exported not because its defined as a string as I originally hoped but because of some confusion between these two unrelated methods?

1

There are 1 best solutions below

0
On

Make sure the method signatures for export are defined:

Code that uses the @export annotation must either

  • include closure/base.js, or,

  • define both goog.exportSymbol and goog.exportProperty with the same method signature in their own codebase.

References