简体   繁体   中英

JavaScript compression that preserves public methods?

I'm trying to minimize a library that I've created. Unfortunately, because I started developing the library several years ago, it does not use any standard way of declaring methods and private properties. Basically, my code looks like:

/** @class Sample abstract class. */
myname.space.AbstractClass = function() {

    /** Sample protected property A
      * @private
      */
    this.longNameProtectedPropertyA = 10;

    /** Sample protected property B
      * @private
      */
    this.longNameProtectedPropertyB = 20;
}

/** Sample public method A.
  * @methodOf myname.space.AbstractClass#
  */
myname.space.AbstractClass.prototype.publicMethodA = function() {
    return this.longNamePrivatePropertyA;
}

/** Sample public method B.
  * @methodOf myname.space.AbstractClass#
  */
myname.space.AbstractClass.prototype.publicMethodB = function() {
    return this.longNamePrivatePropertyB;
}

/** @class Sample concrete class.
  * @extends myname.space.AbstractClass
  */
myname.space.ConcreteClass = function() {

    myname.space.AbstractClass.call(this);

    /** Sample protected property C.
      * @protected
      */
    this.longNameProtectedPropertyC = 30;
}
myname.space.ConcreteClass.trickyExtend(myname.space.AbstractClass);

/** Overrides publicMethodB of the base class.
  * @methodOf myname.space.ConcreteClass# */
myname.space.ConcreteClass.prototype.publicMethodB = function() {
    return this.longNameProtectedPropertyC * this.longNameProtectedPropertyB;
}

In such a long example I tried to show the coding style that I use. I realize that today, it is non-standard. But my library is already written like that and it works perfectly.

Now my problem is that I want to minify the code. What I need is to compress the long names of private properties, but I need to preserve the names of public methods.

I've tried the YUI compressor and Google's Closure compiler. Neither worked as I needed. Either both private properties' and public methods' names were obfuscated, or none of them were.

Is there a way how to do that? I'm ready to spend long time by annotating hundreds of my methods/properties if it would help. Is there some minifier that could help me?

You could try rewriting, before minimisation, with associative syntax, eg :

myname.space.AbstractClass.prototype['publicMethodA'] = function() {
    return this.longNamePrivatePropertyA;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM