NAME
Javascript::Closure - compress your javascript code using Google online service of Closure Compiler
VERSION
0.03
SYNOPSIS
#nothing is imported by default
use Javascript::Closure qw(minify);
#you can import the constants too
use Javascript::Closure qw(minify :CONSTANTS);
#open a file
open (FILE,'<','jscript.js') or die $!;
my @lines = <FILE>;
close FILE;
#compress the code. most of the time it will be all you need!
my $compressed = minify(input=>join('',@lines));
#output the result in another file
open FILE,'>','closure-jscript.js' or die $!;
print FILE $compressed;
close FILE;
#further tweaking of the result is possible though...
#you can add options:
my $compressed = minify(input => [$string,'http://www.domain.com/my.js',$string2,'http://www.domain2.com/my2.js'],
output_format => Javascript::Closure::XML,
output_info => Javascript::Closure::STATISTICS,
compilation_level=> Javascript::Closure::ADVANCED_OPTIMIZATIONS
);
my $compressed = minify(input => $string,
output_format => XML,
output_info => STATISTICS,
compilation_level=> ADVANCED_OPTIMIZATIONS
);
#specifiy multiple output_info
use Javascript::Closure qw(minify :CONSTANTS);
my $compressed = minify(input => $string,
output_format => JSON,
output_info => [STATISTICS WARNINGS COMPILED_CODE],
compilation_level=> SIMPLE_OPTIMIZATIONS
);
DESCRIPTION
This package allows you to compress your javascript code by using the online service of Closure Compiler offered by Google via a REST API. See http://closure-compiler.appspot.com/ for further information.
MOTIVATION
Needed a package to encapsulate a coherent API for a future Javascript::Minifier::Any package.
ADVANTAGES
Gives you access to the closure compression algo with an unified API.
CONSTANTS
-
- WHITESPACE_ONLY remove space and comments from javascript code. - SIMPLE_OPTIMIZATIONS compress the code by renaming local variables - ADVANCED_OPTIMIZATIONS compress all local variables, do some clever stripping down of the code (unused functions are removed) but you need to setup external references to do it properly.
See http://code.google.com/intl/ja/closure/compiler/docs/api-tutorial3.html for further information.
-
- XML return the output in XML format with the information set with your output_info settings - JSON return the output in JSON format with the information set with your output_info settings - TEXT return the output in raw text format with the information set with your output_info settings
See http://code.google.com/intl/ja/closure/compiler/docs/api-ref.html#out for further information.
-
- COMPILED_CODE return only the raw compressed javascript source code. - WARNINGS return any warnings found by the Closure Compiler (ie,code after a return statement) - ERRORS return any errors in your javascript code found by the Closure Compiler - STATISTICS return some statistics about the compilation process (original file size, compressed file size, time,etc)
See http://code.google.com/intl/ja/closure/compiler/docs/api-ref.html#output_info for further information.
SUBROUTINES/METHODS
- minify(input=scalar||array ref,compilation_level=>scalar,output_info=>scalar,output_format=>scalar)>
-
Takes an hash with the following parameters:
- input: scalar or array ref
The input accepts either a scalar containing the javascript code to be parsed or an url to grap the javascript. You can also use an array reference containing multiple urls or raw source code (input=>[$string,'http://www.domain.com/f.js']). If you combine both urls and raw source code, minify will create 2 queries to the service.
Other parameters are options set by default:
- compilation_level:scalar WHITESPACE_ONLY SIMPLE_OPTIMIZATIONS (default) ADVANCED_OPTIMIZATIONS - output_info: scalar or array ref COMPILED_CODE (default) WARNINGS ERRORS STATISTICS - output_format:scalar TEXT (default) JSON XML
minify returns the compressed version of the javascript code as a scalar. The package does not send back a JSON or XML object. Only the raw string from the service.
DIAGNOSTICS
Fail to connect to http://closure-compiler.appspot.com/compile:...
-
The module could not connect and successfully compress your javascript. See the detail error to get a hint.
TODO
- optional parameters
-
none of the following optional parameters are supported:
- warning_level - use_closure_library - formatting - output_file_name - exclude_default_externs - externs_url - js_externs
SEE ALSO
-
Javascript::Minifier Javascript::Packer JavaScript::Minifier::XS http://closure-compiler.appspot.com/
CONFIGURATION AND ENVIRONMENT
none
DEPENDENCIES
INCOMPATIBILITIES
none
BUGS AND LIMITATIONS
If you do me the favor to _use_ this module and find a bug, please email me i will try to do my best to fix it (patches welcome)!
AUTHOR
shiriru <shiriru0111[arobas]hotmail.com>
LICENSE AND COPYRIGHT
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
3 POD Errors
The following errors were encountered while parsing the POD:
- Around line 201:
'=item' outside of any '=over'
- Around line 246:
You forgot a '=back' before '=head1'
You forgot a '=back' before '=head1'
- Around line 320:
'=item' outside of any '=over'