Evaluating Closure compiler and library.

68 views
Skip to first unread message

David W.

unread,
Jan 20, 2010, 1:28:12 AM1/20/10
to Closure Library Discuss
Hi there,

I'm currently working on a 10k LOC JavaScript project that uses a home-
grown build system to assemble the pieces. This system is very much
home-grown, and includes such wonders as abusing the C preprocessor to
make use of certain features.

Over the past week I've been trying to find a better system to convert
the project over to, and in particular moving away from the use of the
preprocessor, but finding something with the features I need is
proving difficult. The Closure library looks very promising,
particularly the support for debug logging and asserts, but I still
have a few questions.

Is it possible to perform conditional compilation at all? In my
current system, asserts and log messages can be removed in a release
build, however everything I've read about Closure makes no mention of
such a facility. The comments in the logging source code suggest it is
in use by Gmail, though on looking at Gmail's JS, I can't find any
substrings in common with the logging code, which suggests such a
thing might be possible.


David

bolinfest

unread,
Jan 20, 2010, 7:12:00 PM1/20/10
to Closure Library Discuss
Yes, variables that can be overridden at compile time using the
Closure Compiler can be specified with @define. The documentation is
at:

http://code.google.com/closure/compiler/docs/js-for-compiler.html#tag-define

David W.

unread,
Jan 23, 2010, 1:52:50 AM1/23/10
to Closure Library Discuss
Hi bolinfest,

Thanks for your reply. I have decided to bite the bullet and attempt a
mass-conversion of my code to Closure. So far, this has involved
deleting tonnes of preprocessor magic, a lot of random bits of code
that Closure Library bundles but were not included with jQuery, and of
course, converting to the module system.

I am now trying to untangle my logging functions from the
preprocessor, however I can't seem to arrive at some form that is
omitted entirely when building with DEBUG = 0.

My hackish system looks something like:

#ifdef DEBUG
function _format_log(file, line, thisObj, fmt)
{
/* ... */
}

# define LOG(...) _format_log(__FILE__, __LINE__, this,
__VAR_ARGS__);
# define ERROR(...) _format_error(...);
#else
# define LOG(...)
# define ERROR(...)
#endif


Ignoring the library's own logging infrastructure for a moment (it
does not support Firebug's rich formatting specifiers), or the fact
there is no way to capture __FILE__ and __LINE__ (I can live without),
the closest I can manage with Closure is:

/** @const */
var DEBUG = false;


function getObjectLogger(thisObj)
{
if(! DEBUG)
{
return function(){};
}
else
{
return /* .... */
}
}


Unfortunately this does not elide calls to the returned function, and
I can see no way of doing so. That means my (very large number of)
debug strings remain in the output file, blowing its size up
considerably.

I tried fiddling with @nosideeffects, but that annotation does not
seem designed for this purpose, and I could find no precedent in the
library source code for dealing with this situation.

Any ideas?

Thanks,


David

On Jan 21, 12:12 am, bolinfest <bolinf...@gmail.com> wrote:
> Yes, variables that can be overridden at compile time using the
> Closure Compiler can be specified with @define. The documentation is
> at:
>

> http://code.google.com/closure/compiler/docs/js-for-compiler.html#tag...

bolinfest

unread,
Jan 26, 2010, 7:38:13 PM1/26/10
to Closure Library Discuss
Hi David, if you could provide a more concrete example so I could see
what strings are not getting stripped, I could probably help you out.
From what information you have provided, one option might be to
rewrite things as:

var getObjectLogger = DEBUG ? function() { /* one thing */ } : function
() { /* another thing */ };

Also, I recommend using goog.DEBUG rather than introducing your own
DEBUG variable, assuming you want both types of DEBUG statements
stripped at the same time, which seems likely.

Cheers,
Michael

David Wilson

unread,
Feb 2, 2010, 4:54:53 AM2/2/10
to closure-lib...@googlegroups.com
Hi there,

On 27 January 2010 00:38, bolinfest <boli...@gmail.com> wrote:
> Hi David, if you could provide a more concrete example so I could see
> what strings are not getting stripped, I could probably help you out.
> From what information you have provided, one option might be to
> rewrite things as:
>
> var getObjectLogger = DEBUG ? function() { /* one thing */ } : function
> () { /* another thing */ };
>
> Also, I recommend using goog.DEBUG rather than introducing your own
> DEBUG variable, assuming you want both types of DEBUG statements
> stripped at the same time, which seems likely.

My aim was to provide a concise way of capturing 'this' when making
logging calls. It turns out simpler to just add 'this' as a parameter
to every call to the logging function by way of a global
search/replace, that way simply wrapping the function's body in
"if(goog.DEBUG) {}" causes calls to it to disappear during the
optimised build.

Thanks again,


David

Reply all
Reply to author
Forward
0 new messages