| 1 |
NAME |
| 2 |
|
| 3 |
JavaScript::Minifier::XS - XS based JavaScript minifier |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
|
| 7 |
use JavaScript::Minifier::XS qw(minify); |
| 8 |
my $js = '...'; |
| 9 |
my $minified = minify($js); |
| 10 |
|
| 11 |
DESCRIPTION |
| 12 |
|
| 13 |
JavaScript::Minifier::XS is a JavaScript "minifier"; its designed to |
| 14 |
remove unnecessary whitespace and comments from JavaScript files, which |
| 15 |
also not breaking the JavaScript. |
| 16 |
|
| 17 |
JavaScript::Minifier::XS is similar in function to |
| 18 |
JavaScript::Minifier, but is substantially faster as its written in XS |
| 19 |
and not just pure Perl. |
| 20 |
|
| 21 |
METHODS |
| 22 |
|
| 23 |
minify($js) |
| 24 |
|
| 25 |
Minifies the given $js, returning the minified JavaScript back to the |
| 26 |
caller. |
| 27 |
|
| 28 |
HOW IT WORKS |
| 29 |
|
| 30 |
JavaScript::Minifier::XS minifies the JavaScript by removing |
| 31 |
unnecessary whitespace from JavaScript documents. Comments (both block |
| 32 |
and line) are also removed, except when (a) they contain the word |
| 33 |
"copyright" in them, or (b) they're needed to implement "IE Conditional |
| 34 |
Compilation". |
| 35 |
|
| 36 |
Internally, the minification process is done by taking multiple passes |
| 37 |
through the JavaScript document: |
| 38 |
|
| 39 |
Pass 1: Tokenize |
| 40 |
|
| 41 |
First, we go through and parse the JavaScript document into a series of |
| 42 |
tokens internally. The tokenizing process does not check to make sure |
| 43 |
you've got syntactically valid JavaScript, it just breaks up the text |
| 44 |
into a stream of tokens suitable for processing by the subsequent |
| 45 |
stages. |
| 46 |
|
| 47 |
Pass 2: Collapse |
| 48 |
|
| 49 |
We then march through the token list and collapse certain tokens down |
| 50 |
to their smallest possible representation. If they're still included in |
| 51 |
the final results we only want to include them at their shortest. |
| 52 |
|
| 53 |
Whitespace |
| 54 |
|
| 55 |
Runs of multiple whitespace characters are reduced down to a single |
| 56 |
whitespace character. If the whitespace contains any "end of line" |
| 57 |
(EOL) characters, then the end result is the first EOL character |
| 58 |
encountered. Otherwise, the result is the first whitespace character |
| 59 |
in the run. |
| 60 |
|
| 61 |
Pass 3: Pruning |
| 62 |
|
| 63 |
We then go back through the token list and prune and remove unnecessary |
| 64 |
tokens. |
| 65 |
|
| 66 |
Whitespace |
| 67 |
|
| 68 |
Wherever possible, whitespace is removed; before+after comment |
| 69 |
blocks, and before+after various symbols/sigils. |
| 70 |
|
| 71 |
Comments |
| 72 |
|
| 73 |
Comments that are either (a) IE conditional compilation comments, or |
| 74 |
that (b) contain the word "copyright" in them are preserved. All |
| 75 |
other comments (line and block) are removed. |
| 76 |
|
| 77 |
Everything else |
| 78 |
|
| 79 |
We keep everything else; identifiers, quoted literal strings, |
| 80 |
symbols/sigils, etc. |
| 81 |
|
| 82 |
Pass 4: Re-assembly |
| 83 |
|
| 84 |
Lastly, we go back through the token list and re-assemble it all back |
| 85 |
into a single JavaScript string, which is then returned back to the |
| 86 |
caller. |
| 87 |
|
| 88 |
AUTHOR |
| 89 |
|
| 90 |
Graham TerMarsch ([email protected]) |
| 91 |
|
| 92 |
COPYRIGHT |
| 93 |
|
| 94 |
Copyright (C) 2007-, Graham TerMarsch. All Rights Reserved. |
| 95 |
|
| 96 |
This is free software; you can redistribute it and/or modify it under |
| 97 |
the same license as Perl itself. |
| 98 |
|
| 99 |
SEE ALSO |
| 100 |
|
| 101 |
JavaScript::Minifier. |
| 102 |
|