goog.inherits present in the output file

109 views Asked by At

I'm trying to use Closure Compiler and Closure Library.

When I use the library everything is ok, I'm including "base.js" in my simulation and it works with all my javascript files.

The problem is present when I "compilate" my application: In the output file I've got a reference to a closure library'sfunction "goog.inherits".

From what I've read, it's not necessary to include "base.js" in production. I'm working on a library, so I don't want to force users to have a reference to the Closure Library.

How can I do?

Here is my code:

NM.ObjectEvent = function( type )
{
    goog.base(this);
}
goog.inherits( NM.ObjectEvent, NM.Event );

And the script look like that:

java -jar compiler.jar  --compilation_level SIMPLE_OPTIMIZATIONS --js_output_file myLib.js `find ../src/ -name '*.js'`
2

There are 2 answers

0
momo On

Like John said, if you have references to goog.base and goog.inherits, you're referencing the library. Fortunately, you can emulate those functions... Something like this should work...

NM.ObjectEvent = function( type )
{
    NM.Event.call(this, type);
}
(function(){
  var temp = function(){};
  temp.prototype = NM.Event.prototype;
  NM.ObjectEvent.prototype = new temp();
}();

If you're using goog.base elsewhere (for example, to call superclass methods), then you'll need to do more work, but the above should suffice if you're only using base and inherits where shown in your original post.

0
John On

What you have heard does not apply to SIMPLE_OPTIMIZATIONS. With ADVANCED_OPTIMIZATIONS everything unused in base.js is removed, with SIMPLE_OPTIMIZATIONS only function local optimizations are performed and unused methods are not removed.

Regardless of the mode, if you use goog.inherits it will remain in some form. Something needs to do the work that goog.inherits does to setup the prototype chain.