line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Javascript::Closure; |
2
|
4
|
|
|
4
|
|
194384
|
use 5.008008; |
|
4
|
|
|
|
|
17
|
|
|
4
|
|
|
|
|
149
|
|
3
|
4
|
|
|
4
|
|
19
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
124
|
|
4
|
4
|
|
|
4
|
|
20
|
use warnings; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
118
|
|
5
|
4
|
|
|
4
|
|
19
|
use Carp; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
358
|
|
6
|
4
|
|
|
4
|
|
2287
|
use LWP::UserAgent; |
|
4
|
|
|
|
|
116607
|
|
|
4
|
|
|
|
|
318
|
|
7
|
|
|
|
|
|
|
our $VERSION = 0.07; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use constant { |
10
|
4
|
|
|
|
|
4001
|
WHITESPACE_ONLY => 'WHITESPACE_ONLY', |
11
|
|
|
|
|
|
|
SIMPLE_OPTIMIZATIONS => 'SIMPLE_OPTIMIZATIONS', |
12
|
|
|
|
|
|
|
ADVANCED_OPTIMIZATIONS => 'ADVANCED_OPTIMIZATIONS', |
13
|
|
|
|
|
|
|
QUIET => 'QUIET', |
14
|
|
|
|
|
|
|
DEFAULT => 'DEFAULT', |
15
|
|
|
|
|
|
|
VERBOSE => 'VERBOSE', |
16
|
|
|
|
|
|
|
COMPILED_CODE => 'compiled_code', |
17
|
|
|
|
|
|
|
WARNINGS => 'warnings', |
18
|
|
|
|
|
|
|
ERRORS => 'errors', |
19
|
|
|
|
|
|
|
STATISTICS => 'statistics', |
20
|
|
|
|
|
|
|
TEXT => 'text', |
21
|
|
|
|
|
|
|
JSON => 'json', |
22
|
|
|
|
|
|
|
XML => 'xml', |
23
|
|
|
|
|
|
|
CLOSURE_COMPILER_SERVICE=>'http://closure-compiler.appspot.com/compile' |
24
|
4
|
|
|
4
|
|
30
|
}; |
|
4
|
|
|
|
|
6
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my @compilation_level = qw(WHITESPACE_ONLY SIMPLE_OPTIMIZATIONS ADVANCED_OPTIMIZATIONS); |
27
|
|
|
|
|
|
|
my @output_info = qw(COMPILED_CODE WARNINGS ERRORS STATISTICS); |
28
|
|
|
|
|
|
|
my @output_format = qw(TEXT JSON XML); |
29
|
|
|
|
|
|
|
my @warning_level = qw(QUIET DEFAULT VERBOSE); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
require Exporter; |
32
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
33
|
|
|
|
|
|
|
our @EXPORT_OK = ('minify',@compilation_level,@output_info,@output_format,@warning_level); |
34
|
|
|
|
|
|
|
our %EXPORT_TAGS = (CONSTANTS => [@compilation_level,@output_info,@output_format,@warning_level]); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our $TIMEOUT = 5; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub minify { |
39
|
2
|
|
|
2
|
1
|
885425
|
my %args = @_; |
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
|
|
7
|
my %send = (); |
42
|
2
|
|
100
|
|
|
20
|
$send{output_info} = _verify(\@output_info, $args{output_info} ,'lower') || COMPILED_CODE; |
43
|
2
|
|
100
|
|
|
13
|
$send{output_format} = _verify(\@output_format, $args{output_format} ,'lower') || TEXT; |
44
|
2
|
|
50
|
|
|
14
|
$send{compilation_level} = _verify(\@compilation_level,$args{compilation_level} ) || WHITESPACE_ONLY; |
45
|
2
|
|
50
|
|
|
13
|
$send{warning_level} = _verify(\@warning_level ,$args{warning_level} ) || DEFAULT; |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
50
|
|
|
14
|
my $js = $args{input} || 'var test=true;//this is a test'; |
48
|
|
|
|
|
|
|
#create the user agent |
49
|
2
|
|
|
|
|
10
|
my $ua = _create_ua(); |
50
|
2
|
|
|
|
|
12
|
my $source = _cleanup_code_source($ua,$js); |
51
|
|
|
|
|
|
|
|
52
|
2
|
50
|
|
|
|
6
|
$send{js_code} = $source->{js_code} if(@{ $source->{js_code} } > 0); |
|
2
|
|
|
|
|
14
|
|
53
|
2
|
50
|
|
|
|
3
|
$send{code_url} = $source->{code_url} if(@{ $source->{code_url} } > 0); |
|
2
|
|
|
|
|
10
|
|
54
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
10
|
return _compile($ua,\%send); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
#hacky i know |
59
|
|
|
|
|
|
|
sub _verify { |
60
|
8
|
|
|
8
|
|
24
|
my ($vars,$value,$lower) = @_; |
61
|
|
|
|
|
|
|
|
62
|
8
|
100
|
|
|
|
76
|
return undef if(!$value);#set default value |
63
|
|
|
|
|
|
|
|
64
|
2
|
100
|
|
|
|
9
|
$value = [$value] if(ref($value) ne 'ARRAY'); |
65
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
8
|
my $choice = join(',',@$vars); |
67
|
2
|
50
|
|
|
|
12
|
$choice = "\L$choice" if($lower); |
68
|
|
|
|
|
|
|
|
69
|
2
|
|
|
|
|
6
|
foreach my $val (@$value){ |
70
|
4
|
50
|
33
|
|
|
96
|
croak $val.' is not in:'.$choice if($val && !grep(/^$val$/i,@$vars)); |
71
|
4
|
50
|
|
|
|
16
|
$val = ($lower) ? "\L$val" :"\U$val"; |
72
|
|
|
|
|
|
|
} |
73
|
2
|
|
|
|
|
10
|
return $value; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _create_ua { |
77
|
2
|
|
|
2
|
|
22
|
my $ua = LWP::UserAgent->new; |
78
|
2
|
|
|
|
|
2754
|
$ua-> agent(__PACKAGE__."/".$VERSION); |
79
|
2
|
|
|
|
|
118
|
$ua-> timeout($TIMEOUT); |
80
|
2
|
|
|
|
|
28
|
return $ua; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _compile { |
84
|
2
|
|
|
2
|
|
4
|
my ($ua,$args)=@_; |
85
|
|
|
|
|
|
|
|
86
|
2
|
|
|
|
|
35
|
my $res = $ua->post(CLOSURE_COMPILER_SERVICE,$args); |
87
|
2
|
50
|
|
|
|
787110
|
return $res->content if ($res->is_success); |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
0
|
croak 'Fail to connect to '.CLOSURE_COMPILER_SERVICE.':'.$res->as_string; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _cleanup_code_source { |
93
|
2
|
|
|
2
|
|
7
|
my ($ua,$code_source) = @_; |
94
|
|
|
|
|
|
|
|
95
|
2
|
100
|
|
|
|
12
|
$code_source = [$code_source] if(ref($code_source) ne 'ARRAY'); |
96
|
|
|
|
|
|
|
|
97
|
2
|
|
|
|
|
5
|
my (@str,@urls); |
98
|
2
|
|
|
|
|
7
|
foreach my $js (@$code_source) { |
99
|
2
|
50
|
|
|
|
12
|
if($js!~m{^http://}){ |
100
|
2
|
|
|
|
|
7
|
push @str,$js; next; |
|
2
|
|
|
|
|
7
|
|
101
|
|
|
|
|
|
|
} |
102
|
0
|
0
|
|
|
|
0
|
if($ua->get($js)->is_success){ |
103
|
0
|
|
|
|
|
0
|
push @urls,$js; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
else { |
106
|
0
|
|
|
|
|
0
|
carp 'The following url could not be fetched:'.$js; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
return { |
110
|
2
|
|
|
|
|
34
|
js_code =>\@str, |
111
|
|
|
|
|
|
|
code_url =>\@urls |
112
|
|
|
|
|
|
|
}; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
"The earth is blue like an orange."; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |