File Coverage

blib/lib/JavaScript/Minifier/XS.pm
Criterion Covered Total %
statement 8 8 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 11 11 100.0


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