line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JavaScript::Prepare; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
185852
|
use Modern::Perl; |
|
7
|
|
|
|
|
107164
|
|
|
7
|
|
|
|
|
52
|
|
4
|
|
|
|
|
|
|
|
5
|
7
|
|
|
7
|
|
1622
|
use File::Basename; |
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
815
|
|
6
|
7
|
|
|
7
|
|
8513
|
use FileHandle; |
|
7
|
|
|
|
|
7626
|
|
|
7
|
|
|
|
|
42
|
|
7
|
7
|
|
|
7
|
|
10722
|
use JavaScript::Minifier::XS qw( minify ); |
|
7
|
|
|
|
|
21029
|
|
|
7
|
|
|
|
|
601
|
|
8
|
|
|
|
|
|
|
|
9
|
7
|
|
|
7
|
|
6384
|
use version; |
|
7
|
|
|
|
|
16119
|
|
|
7
|
|
|
|
|
46
|
|
10
|
|
|
|
|
|
|
our $VERSION = qv( 0.1 ); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
7
|
|
|
7
|
0
|
809
|
my $class = shift; |
15
|
7
|
|
|
|
|
22
|
my %args = @_; |
16
|
|
|
|
|
|
|
|
17
|
7
|
|
|
|
|
23
|
my $self = { |
18
|
|
|
|
|
|
|
strip => 0, |
19
|
|
|
|
|
|
|
}; |
20
|
7
|
|
|
|
|
19
|
bless $self, $class; |
21
|
|
|
|
|
|
|
|
22
|
7
|
100
|
|
|
|
32
|
$self->{'strip'} = 1 |
23
|
|
|
|
|
|
|
if defined $args{'strip'}; |
24
|
|
|
|
|
|
|
|
25
|
7
|
|
|
|
|
21
|
return $self; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub process { |
29
|
3
|
|
|
3
|
0
|
1141
|
my $self = shift; |
30
|
3
|
|
|
|
|
7
|
my @args = @_; |
31
|
|
|
|
|
|
|
|
32
|
3
|
|
|
|
|
3
|
my $minified = ''; |
33
|
3
|
|
|
|
|
6
|
foreach my $arg ( @args ) { |
34
|
4
|
|
|
|
|
5
|
given ( $arg ) { |
35
|
4
|
|
|
|
|
70
|
when ( -f $arg ) { |
36
|
3
|
|
|
|
|
8
|
$minified .= $self->process_file( $arg ); |
37
|
|
|
|
|
|
|
} |
38
|
1
|
|
|
|
|
10
|
when ( -d $arg ) { |
39
|
1
|
|
|
|
|
5
|
$minified .= $self->process_directory( $arg ); |
40
|
|
|
|
|
|
|
} |
41
|
0
|
|
|
|
|
0
|
default { |
42
|
0
|
|
|
|
|
0
|
return ''; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
3
|
|
|
|
|
9
|
return $minified; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub process_string { |
51
|
19
|
|
|
19
|
0
|
1342
|
my $self = shift; |
52
|
19
|
|
|
|
|
27
|
my $js = shift; |
53
|
|
|
|
|
|
|
|
54
|
19
|
100
|
|
|
|
99
|
$js =~ s{^ \s* console.log(.*?); \s* $}{}gmx |
55
|
|
|
|
|
|
|
if $self->{'strip'}; |
56
|
|
|
|
|
|
|
|
57
|
19
|
|
|
|
|
1177
|
my $minified = minify($js); |
58
|
|
|
|
|
|
|
|
59
|
19
|
100
|
66
|
|
|
190
|
return "${minified}\n" |
60
|
|
|
|
|
|
|
if defined $minified && length $minified; |
61
|
1
|
|
|
|
|
4
|
return ''; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub process_file { |
65
|
16
|
|
|
16
|
0
|
1915
|
my $self = shift; |
66
|
16
|
|
|
|
|
21
|
my $file = shift; |
67
|
|
|
|
|
|
|
|
68
|
16
|
|
|
|
|
41
|
my $content = $self->read_file( $file ); |
69
|
16
|
50
|
|
|
|
41
|
return '' unless $content; |
70
|
|
|
|
|
|
|
|
71
|
16
|
|
|
|
|
37
|
my $control_file = $content =~ m{^# control file}; |
72
|
|
|
|
|
|
|
|
73
|
16
|
100
|
|
|
|
35
|
if ( $control_file ) { |
74
|
2
|
|
|
|
|
12
|
return $self->process_control_file( $file ); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
else { |
77
|
14
|
|
|
|
|
38
|
return $self->process_string( $content ); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
sub process_control_file { |
81
|
2
|
|
|
2
|
0
|
4
|
my $self = shift; |
82
|
2
|
|
|
|
|
5
|
my $file = shift; |
83
|
|
|
|
|
|
|
|
84
|
2
|
|
|
|
|
130
|
my $dir = dirname $file; |
85
|
2
|
|
|
|
|
11
|
my $content = $self->read_file( $file ); |
86
|
2
|
|
|
|
|
11
|
my @lines = split m{\n}, $content; |
87
|
|
|
|
|
|
|
|
88
|
2
|
|
|
|
|
3
|
my $minified = ''; |
89
|
2
|
|
|
|
|
6
|
foreach my $line ( @lines ) { |
90
|
|
|
|
|
|
|
# |
91
|
8
|
|
|
|
|
32
|
$line =~ m{ |
92
|
|
|
|
|
|
|
^ |
93
|
|
|
|
|
|
|
( \S+ )? # $1: a filename |
94
|
|
|
|
|
|
|
\s* (?: \# .* )? # optional comment |
95
|
|
|
|
|
|
|
$ |
96
|
|
|
|
|
|
|
}x; |
97
|
|
|
|
|
|
|
|
98
|
8
|
100
|
|
|
|
43
|
$minified .= $self->process_file( "$dir/$1" ) |
99
|
|
|
|
|
|
|
if defined $1; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
2
|
|
|
|
|
11
|
return $minified; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
sub read_file { |
105
|
18
|
|
|
18
|
0
|
22
|
my $self = shift; |
106
|
18
|
|
|
|
|
19
|
my $file = shift; |
107
|
|
|
|
|
|
|
|
108
|
18
|
50
|
|
|
|
121
|
my $handle = FileHandle->new( $file ) |
109
|
|
|
|
|
|
|
or return; |
110
|
|
|
|
|
|
|
|
111
|
18
|
|
|
|
|
1310
|
my $content = do { |
112
|
18
|
|
|
|
|
52
|
local $/; |
113
|
|
|
|
|
|
|
<$handle> |
114
|
18
|
|
|
|
|
427
|
}; |
115
|
|
|
|
|
|
|
|
116
|
18
|
|
|
|
|
214
|
return $content; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub process_directory { |
120
|
2
|
|
|
2
|
0
|
517
|
my $self = shift; |
121
|
2
|
|
|
|
|
6
|
my $directory = shift; |
122
|
|
|
|
|
|
|
|
123
|
2
|
|
|
|
|
8
|
my @files = $self->get_files_in_directory( $directory ); |
124
|
2
|
|
|
|
|
3
|
my $minified; |
125
|
|
|
|
|
|
|
|
126
|
2
|
|
|
|
|
6
|
foreach my $file ( @files ) { |
127
|
4
|
|
|
|
|
13
|
$minified .= $self->process_file( $file ); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
2
|
|
|
|
|
10
|
return $minified; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
sub get_files_in_directory { |
133
|
6
|
|
|
6
|
0
|
13
|
my $self = shift; |
134
|
6
|
|
|
|
|
12
|
my $directory = shift; |
135
|
|
|
|
|
|
|
|
136
|
6
|
50
|
|
|
|
224
|
opendir my $handle, $directory |
137
|
|
|
|
|
|
|
or return; |
138
|
|
|
|
|
|
|
|
139
|
6
|
|
|
|
|
10
|
my @files; |
140
|
|
|
|
|
|
|
my @directories; |
141
|
6
|
|
|
|
|
96
|
while ( my $entry = readdir $handle ) { |
142
|
21
|
100
|
|
|
|
110
|
next if $entry =~ m{^\.}; |
143
|
|
|
|
|
|
|
|
144
|
9
|
|
|
|
|
21
|
my $target = "$directory/$entry"; |
145
|
|
|
|
|
|
|
|
146
|
9
|
100
|
|
|
|
142
|
push( @files, $target ) if -f $target; |
147
|
9
|
100
|
|
|
|
141
|
push( @directories, $target ) if -d $target; |
148
|
|
|
|
|
|
|
} |
149
|
6
|
|
|
|
|
76
|
closedir $handle; |
150
|
|
|
|
|
|
|
|
151
|
6
|
|
|
|
|
17
|
foreach my $dir ( @directories ) { |
152
|
3
|
|
|
|
|
4
|
my @subfiles; |
153
|
|
|
|
|
|
|
|
154
|
3
|
|
|
|
|
18
|
foreach my $file ( $self->get_files_in_directory( $dir ) ) { |
155
|
3
|
|
|
|
|
10
|
push @subfiles, $file; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
3
|
|
|
|
|
13
|
@files = ( @subfiles, @files ); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
6
|
|
|
|
|
31
|
return @files; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |