Prevent Google Closure Compiler from renaming settings objects

后端 未结 6 2038
礼貌的吻别
礼貌的吻别 2020-12-06 05:56

I\'m trying to get the Google Closure Compiler to not rename objects when passed as settings or data to a function. By looking at the annotations present in jQuery, I though

6条回答
  •  伪装坚强ぢ
    2020-12-06 05:59

    Apparently annotations are not to blame here, simply by introducing some unused properties to the settings object will result in the compiler renaming stuff.

    I'd like to know where these came from and the only logical explanation I have so far (confirmed here), is that the compiler keeps a global name table of things it won't rename. Simply having a extern with a name will result in any property of that name to be keept around.

    /** @type {Object.} */
    var t = window["t"] = {
      transform: function(m, e) {
        e.transform = m;
      },
      skew: function(m, e) {
        e.skew = m;
      }
    }
    
    /** 
     * @constructor
     */
    function b() {
      this.transform = [];
      this.elementThing = document.createElement("DIV");
    }
    
    t.transform(new b().transform, new b().elementThing);
    

    Results in the following output:

    function c() {
        this.transform = [];
        this.a = document.createElement("DIV")
    }(window.t = {
        transform: function (a, b) {
            b.transform = a
        },
        b: function (a, b) {
            b.b = a
        }
    }).transform((new c).transform, (new c).a);
    

    Notice how transform isn't renamed but elementThing is, even if I try to annotate this type I can't get it to rename transform accordingly.

    But if I add the following extern source function a() {}; a.prototype.elementThing = function() {}; it won't rename elementThing despite looking at the code, I can clearly tell that the type returned by the constructor is unrelated to the extern a, yet somehow, this is how the compiler does it. I guess this is just a limitation of the closure compiler, which I think is a darn shame.

提交回复
热议问题