File Coverage

lib/Mojolicious/Plugin/AssetPack/Pipe/JavaScriptPacker.pm
Criterion Covered Total %
statement 19 24 79.1
branch 3 8 37.5
condition 1 3 33.3
subroutine 5 5 100.0
pod 1 1 100.0
total 29 41 70.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::AssetPack::Pipe::JavaScriptPacker;
2 2     2   1015 use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe';
  2         5  
  2         11  
3 2     2   329 use Mojolicious::Plugin::AssetPack::Util qw(diag load_module DEBUG);
  2         4  
  2         129  
4 2     2   1155 use JavaScript::Packer;
  2         20392  
  2         948  
5              
6             my $packer = JavaScript::Packer->init();
7              
8             has config => sub {
9             my $self = shift;
10             my $config = $self->assetpack->config || $self->assetpack->config({});
11             $config->{JavaScriptPacker} ||= {};
12             };
13             has minify_opts => sub { { %{shift->config->{minify_opts} ||= {}}, } };#
14              
15             sub process {
16 4     4 1 1086 my ($self, $assets) = @_;
17 4         18 my $store = $self->assetpack->store;
18            
19 4 100       42 return unless $self->assetpack->minify;
20            
21 2         19 my $file;
22             return $assets->each(
23             sub {
24 2     2   19 my ($asset, $index) = @_;
25 2         8 my $attrs = $asset->TO_JSON;
26 2         180 $attrs->{key} = 'js-min';
27 2         4 $attrs->{minified} = 1;
28            
29 2 50 33     6 return if $asset->format ne 'js' || $asset->minified;
30 0 0         return $asset->content($file)->minified(1)
31             if $file = $store->load($attrs);
32 0 0         return unless length(my $content = $asset->content);
33 0           DEBUG && diag "Minify javascript=[%s] with checksum=[%s] and minify_opts=[@{[ %{$self->minify_opts} ]}]", $asset->url, $asset->checksum, ;
34 0           $packer->minify(\$content, $self->minify_opts);
35 0           $asset->content($store->save(\$content, $attrs))->minified(1);
36             }
37 2         15 );
38             }
39              
40             1;