The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::JavaScript::Compactor - This module provides a means to compact javascript.

SYNOPSIS

 use Data::JavaScript::Compactor;
 my $compacted = Data::JavaScript::Compactor->compact( $javascript )
    or die $Data::JavaScript::Compactor::err_msg;

# OR, to just do a few steps #

 my $c = Data::JavaScript::Compactor->new();
 $c->data( $javascript );
 $c->replace_listeral_strings();
 $c->replace_white_space();
 my $new = $c->data();

DESCRIPTION

This module provides methods to compact javascript source down to just what is needed. It can remove all comments, put everything on one line (semi-)safely, and remove extra whitespace.

EXPORT

None by default.

"compcat" may be exported via "use Data::JavaScript::Compactor qw(compact);"

METHODS

Data::JavaScript::Compactor->compact($js)

Class method. This is a wrapper around all methods in here, to allow you to do all compacting operations in one call.

     my $compacted = Data::JavaScript::Compactor->compact( $javascript );

Data::JavaScript::Compactor->new()

Constructor. Currently takes no options. Returns Data::JavaScript::Compactor object.

$djc->data($js)

If the option $js is passed in, this sets the javascript that will be worked on.

If not passed in, this returns the javascript in whatever state it happens to be in (so you can step through, and pull the data out at any time).

$djc->determine_line_ending()

Method to automatically determine the line ending character in the source data.

$djc->eol_char("\n")

Method to set/override the line ending character which will be used to parse/join lines. Set to "\r\n" if you are working on a DOS / Windows formatted file.

$djc->replace_listeral_strings()

Finds all string literals (eg. things in quotes) and replaces them with tokens of the form "__N__" where N is the occurrance number in the file. The strings are stored inside the object so they may be resotred later.

This should be called before any of the destructive methods are used, in order to get these out of the way.

$djc->replace_white_space()

Per each line:

  • Removes all begining of line whitespace.

  • Removes all end of line whitespace.

  • Combined all series of whitespace into one space character (eg. s/\s+/ /g)

$djc->remove_blank_lines()

...does what it says.

$djc->combine_concats()

Removes any string literal concatenations. Eg.

    "bob and " +   "sam " + someVar;

Becomes:

    "bob and sam " + someVar

$djc->join_all()

Puts everything on one line.

$djc->replace_extra_whitespace()

This removes any excess whitespace. Eg.

    if (someVar = "foo") {

Becomes:

    if(someVar="foo"){

$djc->restore_literal_strings()

All string literals that were extracted with $djc->replace_listeral_strings() are restored. String literals retain all spacing and extra lines and such.

$djc->replace_final_eol()

Prior to this being called, the end of line is not terminated with a new line character. This adds one of whatever is set in $djc->eol_char().

NOTES

The following should only cause an issue in rare and odd situations... If the input file is in dos format (line termination with "\r\n" (ie. CR LF / Carriage return Line feed)), we'll attempt to make the output the same. If you have a mixture of embeded "\r\n" and "\n" characters (not escaped, those are still safe) then this script may get confused and make them all conform to whatever is first seen in the file.

TODO

Add in a way to retain some comments (eg. so we can retain copyright notices in javascript files). Something like the following:

    my $compact = Data::JavaScript::Compactor->compact( $javascript, keep_comments_matching => qr/copyright/i );

BUGS

There are a few bugs, which may rear their head in some minor situations.

Statements not terminated by semi-colon.

Javascript statement that are NOT terminated by a semi-colon (";") may break once compacted, as they will be put on the same line as the following statement. In many cases, this won't be a problem, but it could cause an issue. Ex.

    i = 5.4
    j = 42

The above would become "i=5.4 j=42", and would generate an error along the lines of "expected ':' before statement".

Ambiguous operator precidence

Operator precidence may get screwed up in ambiguous statements. Eg. "x = y + ++b;" will be compacted into "x=y+++b;", which means something different.

Still looking for them. If you find some, let us know.

SEE ALSO

The order of steps to compact the file were initially based upon the "JavaScript Crunchinator" (http://www.brainjar.com).

AUTHOR

Joshua I. Miller <jmiller@puriifeddata.net>

COPYRIGHT AND LICENSE

Copyright (c) 2005 by CallTech Communications, Inc.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.3 or, at your option, any later version of Perl 5 you may have available.