« lib/Mojolicious/Plugin/AssetPack/Pipe/JavaScriptPacker.pm
↑ Summary
blib/lib/Mojolicious/Plugin/AssetPack/Che.pm »
| 1 | package Mojolicious::Plugin::AssetPack::Pipe::CombineFile; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Mojolicious::Plugin::AssetPack::Util qw(checksum diag DEBUG); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use IO::Compress::Gzip 'gzip'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Mojo::Util qw(url_unescape decode); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | has enabled => sub { shift->assetpack->minify }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | has app => sub { shift->assetpack->app }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | has config => sub { my $self = shift; my $config = $self->assetpack->config || $self->assetpack->config({}); $config->{CombineFile} ||= {}; }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
Condition40%2/5
$self->assetpack->config || $self->assetpack->config({})
$config->{"CombineFile"} ||= {}
MC/DC0%0/2
$self->assetpack->config || $self->assetpack->config({})
$self->assetpack->config
$self->assetpack->config({})
MC/DC0%0/1
$config->{"CombineFile"} ||= {}
$config->{"CombineFile"}
Truth table33%1/3
A || B
A= $self->assetpack->configB= $self->assetpack->config({})
Truth table50%1/2
A
A= $config->{"CombineFile"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | has serve => sub { shift->assetpack->serve_cb }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | has revision => sub { shift->assetpack->revision // '' }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Condition50%1/2
(shift())->assetpack->revision // ""
MC/DC0%0/1
(shift())->assetpack->revision // ""
(shift())->assetpack->revision
Truth table50%1/2
A
A= (shift())->assetpack->revision
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | my $self = shift->SUPER::new(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | $self->app->routes->any('/assets/*topic') | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | ->methods(qw(HEAD GET)) | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | ->name('assetpack by topic') | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ->to(cb => $self->_cb_route_by_topic); | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | sub process { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process: called CC=13 SCAR=26.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 6 | my ($self, $assets) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | my $collect = Mojo::Collection->new; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | my $topicURL = Mojo::URL->new($self->topic); | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | my $topic = url_unescape($topicURL->path->to_string);#->charset('UTF-8') | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | ▶ | my $format = $topicURL->path->[-1] =~ /\.(\w+)$/ ? lc $1 : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | my $checksum = checksum $topic.$self->revision;#name=>decode('UTF-8', $topic) | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | 6 | my $attrs = {key => "combine-file", url=>$topic, name=>decode('UTF-8', $topic), checksum=>$checksum, minified=>1, format=>$format,};# for store | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 6 | my $store = $self->assetpack->store; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | return | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 6 | ▶ | unless $self->enabled || $format ~~ ['html', 'json']; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->enabled or $format ~~ ["html", "json"]
MC/DC100%2/2
$self->enabled or $format ~~ ["html", "json"]
$self->enabled
$format ~~ ["html", "json"]
Truth table100%3/3
A or B
A= $self->enabledB= $format ~~ ["html", "json"]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | DEBUG && diag "Process topic [%s]", $topic; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | #~ return unless $self->enabled;!!! below | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | #~ diag "Load [%s]", $self->assetpack->app->dumper($store->load($attrs)); | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | my (@other, $content) = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | for my $asset (@$assets) { | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | next | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 10 | ▶ | if $asset->isa('Mojolicious::Plugin::AssetPack::Asset::Null'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 10 | ▶ | push @$collect, $asset | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
grep(($asset->format eq $_), ('css', 'js', 'json', 'html')) && (push(@$collect, $asset) && (next))
push @$collect, $asset and next
MC/DC0%0/2
grep(($asset->format eq $_), ('css', 'js', 'json', 'html')) && (push(@$collect, $asset) && (next))
grep(($asset->format eq $_), ('css', 'js', 'json', 'html'))
push @$collect, $asset
Truth table0%0/3
A && B
A= grep(($asset->format eq $_), ('css', 'js', 'json', 'html'))B= push @$collect, $asset
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | and next | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | if grep $asset->format eq $_, qw(css js json html); | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | push @other, $asset; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # preserve assets such as images and font files | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 5 | @$assets = @other; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | ▶ | if (@$collect) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | #~ my $format = $collect->[0]->format; | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | #~ return | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | #~ unless $self->enabled || $format ~~ ['html', 'json']; | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | $content = $collect->map('content')->map(sub { /\n$/ ? $_ : "$_\n" })->join; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | #~ my $checksum = checksum $topic.$self->revision;# | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | DEBUG && diag 'Combined [%s] assets into "%s" with revision=[%s] checksum[%s] and format[%s].', scalar @$collect, $topic, $self->revision, $checksum, $format; | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | #~ push @process, | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | Mojo::File::path($self->app->static->paths->[0], 'cache', $topic)->dirname->make_path; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 5 | $store->save(\$content, $attrs); | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 5 | push @$assets, | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | Mojolicious::Plugin::AssetPack::Asset->new(url => $topic)->checksum($checksum)->minified(1) | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | ->content($content); | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | ▶ | if ($content && $self->config->{gzip} && ($self->config->{gzip}{min_size} || 1000) < $content->size) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%4/8
$content and $self->config->{"gzip"} and ($self->config->{"gzip"}{"min_size"} || 1000) < $content->size
$content and $self->config->{"gzip"}
$self->config->{'gzip'}{'min_size'} || 1000
MC/DC0%0/3
$content and $self->config->{"gzip"} and ($self->config->{"gzip"}{"min_size"} || 1000) < $content->size
$content
$self->config->{"gzip"}
$self->config->{'gzip'}{'min_size'}
Truth table25%1/4
A and B and C
A= $contentB= $self->config->{"gzip"}C= $self->config->{'gzip'}{'min_size'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | gzip \($content->to_string) => \(my $gzip), {-Level => 9}; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | my $checksum_gzip = checksum $topic.$self->revision.'.gzip'; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | DEBUG && diag 'GZIP combined topic=[%s] with revision=[%s] checksum=[%s] and format=[%s] and rate=[%s/%s].', $topic, $self->revision, $checksum, $format, $content->size, length($gzip); | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | $self->assetpack->{by_checksum}{$checksum_gzip} = $store->save(\$gzip, {key => "combine-file-gzip", url=>$topic.'.gzip', name=>decode('UTF-8', $topic.'.gzip'), checksum=>$checksum_gzip, minified=>1, format=>$format,});# name=>decode('UTF-8', $topic.'.gzip') | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | #~ push @$assets, | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | #~ Mojolicious::Plugin::AssetPack::Asset->new(url => $topic.'.gzip')->checksum($checksum_gzip)->minified(1) | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | #~ ->content($gzip); | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | sub _cb_route_by_topic { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cb_route_by_topic: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 6 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 6 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 6 | my $topic = $c->stash('topic'); | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | utf8::encode($topic); | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | #~ my $checksum = checksum Mojo::Util::encode('UTF-8', $topic.$self->revision); | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 6 | my $checksum = checksum $topic.$self->revision; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | #~ my $checksum = checksum $topic.($self->app->config('version') // $self->app->config('версия') // ''); | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 6 | DEBUG && diag 'Routing combined topic=[%s] checksum=[%s]', $topic, $checksum; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 6 | $c->stash('name'=>$checksum); | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | 6 | $c->stash('checksum'=>$checksum); | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 6 | return $self->serve->($c); | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | Доброго всем | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | Mojolicious::Plugin::AssetPack::Pipe::CombineFile - Store combined and gzipped asset to cache file instead of memory. | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | $app->plugin('AssetPack::Che' => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | pipes => [qw(Sass Css JavaScript CombineFile)], | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | CombineFile => { gzip => {min_size => 1000},}, # pipe options | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | process => [ | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | ['foo.html'=>qw(templates/foo.html templates/bar.html)], | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | ..., | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | =head1 CONFIG | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | B<CombineFile> determine config for this pipe module. Hashref keys: | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | B<gzip> - hashref options. | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =head1 ROUTE | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | B</assets/*topic> will auto place. | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | Get combined asset by url | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | <scheme>//<host>/assets/foo.html | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | L<Mojolicious::Plugin::AssetPack::Che> | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | =head1 BUGS / CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-AssetPack-Che/issues>. Pull requests also welcome. | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | Copyright 2016-2020 Mikhail Che. | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||