NAME

WebService::Google::Closure - Perl interface to the Google Closure Javascript compiler service

SYNOPSIS

This module will take given Javascript code and compile it into compact, high-performance code using the Google Closure compiler.

use WebService::Google::Closure;

my $js_code = "
  function hello(name) {
      alert('Hello, ' + name);
  }
  hello('New user');
";

my $res = WebService::Google::Closure->new(
  js_code => $js_code,
)->compile;

print $res->code;
# prints;
# function hello(a){alert("Hello, "+a)}hello("New user");


# Now tell Closure to be more aggressive
my $res2 = WebService::Google::Closure->new(
  compilation_level => "ADVANCED_OPTIMIZATIONS",
  js_code => $js_code,
)->compile;

print $res2->code;
# prints;
# alert("Hello, New user");

print "Original size   = " . $res2->stats->original_size . "\n";
print "Compressed size = " . $res2->stats->compressed_size . "\n";

For more information on the Google Closure compiler, visit its website at http://code.google.com/closure/

METHODS

new

Possible options;

compilation_level

Specifying how aggressive the compiler should be. There are currently three options.

"WHITESPACE_ONLY" or 1

Just removes whitespace and comments from your JavaScript.

"SIMPLE_OPTIMIZATIONS" or 2 (default)

Performs compression and optimization that does not interfere with the interaction between the compiled JavaScript and other JavaScript. This level renames only local variables.

"ADVANCED_OPTIMIZATIONS" or 3

Achieves the highest level of compression by renaming symbols in your JavaScript. When using ADVANCED_OPTIMIZATIONS compilation you must perform extra steps to preserve references to external symbols.

js_code

A string containing Javascript code.

file

One or more filenames of the files you want compiled

Example:

use WebService::Google::Closure;

my $cl1 = WebService::Google::Closure->new(
   file => "/var/www/js/base.js",
);

my $cl2 = WebService::Google::Closure->new(
   file => [qw( /var/www/js/base.js /var/www/js/classes.js )],
 );
url

One or more urls to the files you want compiled

Example:

use WebService::Google::Closure;

my $res = WebService::Google::Closure->new(
   url => "http://code.jquery.com/jquery-1.4.2.js",
   compilation_level => 3,
)->compile;

print "Orig Size = " . $res->stats->original_size . "\n";
print "Comp Size = " . $res->stats->compressed_size . "\n";

# prints;
# Orig Size = 163855
# Comp Size = 65523

compile

Returns a WebService::Google::Closure::Response object.

Will die if unable to connect to the Google closure service.

AUTHOR

Magnus Erixzon, <magnus at erixzon.com>

TODO

externs

When using the compilation level ADVANCED_OPTIMIZATIONS, the compiler achieves extra compression by being more aggressive in the ways that it transforms code and renames symbols. However, this more aggressive approach means that you must take greater care when you use ADVANCED_OPTIMIZATIONS to ensure that the output code works the same way as the input code.

One problem is if your code uses external code that you're not submitting to the compiler - The compiler might then optimize code away, as its not aware of the externally defined functions. The solutions to this is using "externs". I'll implement this when I need it, or if someone asks for it.

See http://code.google.com/closure/compiler/docs/api-tutorial3.html for more information.

BUGS

Please report any bugs or feature requests to bug-webservice-google-closure at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-Google-Closure. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc WebService::Google::Closure

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright 2010 Magnus Erixzon.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.