File Coverage

blib/lib/Mojolicious/Plugin/AssetPack/Che.pm
Criterion Covered Total %
statement 43 47 91.4
branch 14 26 53.8
condition 8 13 61.5
subroutine 8 9 88.8
pod 3 4 75.0
total 76 99 76.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::AssetPack::Che;
2 4     4   354502 use Mojo::Base 'Mojolicious::Plugin::AssetPack';
  4         198463  
  4         28  
3 4     4   145834 use Mojolicious::Plugin::AssetPack::Util qw( DEBUG diag checksum );#
  4         12  
  4         223  
4 4     4   26 use Mojo::URL;
  4         11  
  4         24  
5              
6             has [qw(config app)] => undef, weak=>1;
7             has revision => sub { my $app = shift->app; $app->config('revision') // $app->config('version') // $app->config('версия') // ''; };
8              
9             sub register {
10 3     3 1 266 my ($self, $app, $config) = @_;
11 3         12 $self->config($config);
12 3         37 $self->app($app);
13             #~ Scalar::Util::weaken($self->{app});
14 3         48 Mojo::File->new($app->home->rel_file('assets'), 'cache')->remove_tree({keep_root => 1});
15 3         901 $self->SUPER::register($app, $config);
16            
17             # Patch the asset route
18 3         609 $self->route;
19 3         1351 $app->routes->find('assetpack')->pattern->defaults->{cb} = $self->serve_cb();
20            
21 3         249 $self->store->_types->type(html => ['text/html;charset=UTF-8']);# Restore deleted Jan
22             $self->store->default_headers($config->{default_headers})
23 3 50       815 if $config->{default_headers};
24            
25 3         9 my $process = $config->{process};
26             $self->process(ref eq 'ARRAY' ? @$_ : $_) #($_->[0], map Mojo::URL->new($_), @$_[1..$#$_])
27 3 0       46 for ref $process eq 'HASH' ? map([$_=> ref $process->{$_} eq 'ARRAY' ? @{$process->{$_}} : $process->{$_}], keys %$process) : ref $process eq 'ARRAY' ? @$process : ();
  0 50       0  
    50          
    50          
28            
29 3         1870 return $self;
30             }
31              
32             # redefine for nested topics
33             sub process {
34 6     6 1 1489 my ($self, $topic, @input) = @_;
35 6         28 utf8::encode($topic);
36            
37 6 100       31 $self->route unless $self->{route_added}++;
38 6 50       23 return $self->_process_from_def($topic) unless @input;
39            
40 6         47 my $assets = Mojo::Collection->new;
41 6         41 for my $url (@input) {
42 12         34 utf8::encode($url);
43 12 50       35 if (my $nested = $self->processed($url)) {
44 0         0 push @$assets, @$nested;
45 0         0 next;
46             }
47 12 50       70 my $asset = Scalar::Util::blessed($url) ? $url : $self->store->asset($url);
48 12 50       1951 die qq(Could not find input asset "$url" .) unless Scalar::Util::blessed($asset);
49 12         36 push @$assets, $asset;
50             }
51            
52 6 50   0   17 return $self->tap(sub { $_->{input}{$topic} = $assets }) if $self->{lazy};
  0         0  
53 6         53 return $self->_process($topic => $assets);
54             }
55              
56             #!!! me frozen at parent version 2.02
57 12     12 1 43 sub processed { $_[0]->{by_topic}{$_[1]} }
58              
59             sub serve_cb {
60 6     6 0 35 my $self= shift;
61             return sub {
62 10     10   117573 my $c = shift;
63 10         29 my $checksum = $c->stash('checksum');
64 10 100 50     118 if (($c->req->headers->accept_encoding // '') =~ /gzip/i && (my $asset = $self->{by_checksum}{$checksum})) {
      66        
65 9         376 my $checksum_gzip = checksum($asset->url.$self->revision.'.gzip');
66 9 50 66     247 $asset = $self->{by_checksum}{$checksum_gzip}
      100        
      33        
67             and $c->res->headers->content_encoding('gzip')
68             and (DEBUG ? diag("Sent GZIPed topic [%s]", $asset->url) : 1)#$self->minify ? $self->app->log->info('') :
69             and $self->store->serve_asset($c, $asset)
70             and return $c->rendered;
71             }
72 7         77 Mojolicious::Plugin::AssetPack::_serve($c, @_);
73 6         75 };
74            
75             }
76              
77              
78             =pod
79              
80             =encoding utf8
81              
82             Доброго всем
83              
84             =head1 Mojolicious::Plugin::AssetPack::Che
85              
86             ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
87              
88             =head1 NAME
89              
90             Mojolicious::Plugin::AssetPack::Che - Child of Mojolicious::Plugin::AssetPack for little bit code.
91              
92             =head1 DESCRIPTION
93              
94             Can process assets during register plugin.
95              
96             Can nested assets.
97              
98             Can pipe HTML files with L.
99              
100             Can compile Vue templates L.
101              
102             Can pipe CSS, JS, JSON, HTML with L into disk cache. This pipe can also gzip and cache gzipped assets.
103              
104             Since parent version 1.28.
105              
106             =head1 VERSION
107              
108             Version 2.106 (test on base Mojolicious::Plugin::AssetPack v2.10)
109              
110             =cut
111              
112             our $VERSION = '2.106';
113              
114              
115             =head1 SYNOPSIS
116              
117             See parent module L for full documentation.
118              
119             On register the plugin C can contain additional optional argument B:
120              
121             $app->plugin('AssetPack::Che',
122             pipes => [qw(Sass Css JavaScriptPacker HTML CombineFile)],
123             CombineFile => { gzip => {min_size => 1000},}, # pipe options
124             HTML => {minify_opts=>{remove_newlines => 1,}},# pipe based on HTML::Packer
125             JavaScriptPacker => {minify_opts=>{}},# pipe based on JavaScript::Packer
126             process => [
127             ['foo.js' => qw(path/to/foo1.js path/to/foo2.js)],
128             ['bar.js' => qw(foo.js path/to/bar1.js path/to/bar2.js)], # nested topic
129             ['foo.html' => qw(path/to/foo1.html path/to/foo2.html)],
130             ...
131             ],
132             );
133              
134              
135             =head1 SEE ALSO
136              
137             L
138              
139             =head1 AUTHOR
140              
141             Михаил Че (Mikhail Che), C<< >>
142              
143             =head1 BUGS / CONTRIBUTING
144              
145             Please report any bugs or feature requests at L. Pull requests also welcome.
146              
147             =head1 COPYRIGHT
148              
149             Copyright 2016-2020 Mikhail Che.
150              
151             This library is free software; you can redistribute it and/or modify
152             it under the same terms as Perl itself.
153              
154              
155              
156             =cut
157              
158             1; # End of Mojolicious::Plugin::AssetPack::Che
159