File Coverage

blib/lib/Mojolicious/Plugin/AssetPack/Pipe/Combine.pm
Criterion Covered Total %
statement 21 23 91.3
branch 7 10 70.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 33 38 86.8


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::AssetPack::Pipe::Combine;
2 9     9   70 use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe';
  9         27  
  9         81  
3              
4 9     9   2471 use Mojolicious::Plugin::AssetPack::Util qw(checksum diag DEBUG);
  9         31  
  9         5986  
5              
6             has enabled => sub { shift->assetpack->minify };
7              
8             sub process {
9 13     13 1 89 my ($self, $assets) = @_;
10 13         96 my $combine = Mojo::Collection->new;
11 13         98 my @other;
12              
13 13 100       53 return unless $self->enabled;
14              
15 7         104 for my $asset (@$assets) {
16 13 50       158 if ($asset->isa('Mojolicious::Plugin::AssetPack::Asset::Null')) {
    50          
17 0         0 next;
18             }
19 26         167 elsif (grep { $asset->format eq $_ } qw(css js)) {
20 13         95 push @$combine, $asset;
21             }
22             else {
23 0         0 push @other, $asset;
24             }
25             }
26              
27             # preserve assets such as images and font files
28 7         26 @$assets = @other;
29              
30 7 50       26 if (@$combine) {
31 7         40 my $checksum = checksum $combine->map('checksum')->join(':');
32 7 100   13   133 my $content = $combine->map('content')->map(sub { /\n$/ ? $_ : "$_\n" })->join;
  13         909  
33 7         253 diag 'Combining assets into "%s" with checksum %s.', $self->topic, $checksum if DEBUG;
34 7         49 push @$assets,
35             Mojolicious::Plugin::AssetPack::Asset->new(url => $self->topic)->checksum($checksum)->minified(1)
36             ->content($content);
37             }
38             }
39              
40             1;
41              
42             =encoding utf8
43              
44             =head1 NAME
45              
46             Mojolicious::Plugin::AssetPack::Pipe::Combine - Combine multiple assets to one
47              
48             =head1 DESCRIPTION
49              
50             L will take a list of
51             assets and turn them into a single asset.
52              
53             =head1 ATTRIBUTES
54              
55             $bool = $self->enabled;
56              
57             Set this to false to disable combining assets into a single file. The default
58             value will be L.
59              
60             =head1 METHODS
61              
62             =head2 process
63              
64             See L.
65              
66             =head1 SEE ALSO
67              
68             L.
69              
70             =cut