| 1 | package Mojolicious::Plugin::AssetPack::Pipe::Fetch; | ||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Mojolicious::Plugin::AssetPack::Util qw(diag DEBUG); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Mojo::URL; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | # Only made public for quick fixes. Subject for change | ||||||||||
| 8 | our %FORMATS = ( | ||||||||||
| 9 | css => { | ||||||||||
| 10 | re => qr{url\((['"]{0,1})(.*?)\1\)}, | ||||||||||
| 11 | 0 | ▶ | pos => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 12 | 0 | my ($start, $url, $quotes) = @_; | |||||||||
| 13 | 0 | my $len = length $url; | |||||||||
| 14 | 0 | return $start - length($quotes) - $len - 1, $len; | |||||||||
| 15 | }, | ||||||||||
| 16 | }, | ||||||||||
| 17 | js => { | ||||||||||
| 18 | re => qr{(//\W*sourceMappingURL=)(\S+)}m, | ||||||||||
| 19 | 0 | ▶ | pos => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 20 | 0 | my ($start, $url) = @_; | |||||||||
| 21 | 0 | my $len = length $url; | |||||||||
| 22 | 0 | return $start - $len, $len; | |||||||||
| 23 | }, | ||||||||||
| 24 | }, | ||||||||||
| 25 | 1 | ); | |||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | sub process { | ||||||||
|
Subroutine
process: called CC=1 SCAR=2.3
Pod
documented
| |||||||||||
| 28 | 1 | my ($self, $assets) = @_; | |||||||||
| 29 | 1 | my $store = $self->assetpack->store; | |||||||||
| 30 | 1 | my $route = $self->assetpack->route; | |||||||||
| 31 | 1 | my %related; | |||||||||
| 32 | |||||||||||
| 33 | 1 | ▶ | return $assets->each(sub { | ||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=34.6
| |||||||||||
| 34 | 1 | my ($asset, $index) = @_; | |||||||||
| 35 | 1 | ▶ | return unless $asset->url =~ /^https?:/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 1 | ▶ | return unless my $format = $FORMATS{$asset->format}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | 1 | my $base = Mojo::URL->new($asset->url); | |||||||||
| 39 | 1 | my $content = $asset->content; | |||||||||
| 40 | 1 | my $re = $format->{re}; | |||||||||
| 41 | |||||||||||
| 42 | 1 | ▶ | while ($content =~ /$re/g) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | my @matches = ($2, $1); | |||||||||
| 44 | 0 | my $url = $matches[0]; | |||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | next if $url =~ /^(?:\#|data:)/; # Avoid "data:image/svg+xml..." and "#foo" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 0 | $url = Mojo::URL->new($url); | |||||||||
| 49 | 0 | ▶ | $url = $url->base($base)->to_abs->fragment(undef) unless $url->is_abs; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | unless ($related{$url}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | diag "Fetch resource $url" if DEBUG; | |||||||||
| 53 | 0 | ▶ | my $related = $store->asset($url) or die "AssetPack was unable to fetch related asset $url"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | $self->assetpack->process($related->name, $related); | |||||||||
| 55 | 0 | my $path = $route->render($related->TO_JSON); | |||||||||
| 56 | 0 | $path =~ s!^/!!; | |||||||||
| 57 | 0 | my $up = join '', map {'../'} $path =~ m!\/!g; | |||||||||
| 58 | 0 | $related{$url} = "$up$path"; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | my ($start, $len) = $format->{pos}->(pos($content), @matches); | |||||||||
| 62 | 0 | substr $content, $start, $len, Mojo::URL->new($related{$url})->query(Mojo::Parameters->new); | |||||||||
| 63 | 0 | pos($content) = $start + $len; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 1 | $asset->content($content); | |||||||||
| 67 | 1 | }); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 1 | 1; | |||||||||
| 71 | |||||||||||
| 72 | =encoding utf8 | ||||||||||
| 73 | |||||||||||
| 74 | =head1 NAME | ||||||||||
| 75 | |||||||||||
| 76 | Mojolicious::Plugin::AssetPack::Pipe::Fetch - Fetch related assets | ||||||||||
| 77 | |||||||||||
| 78 | =head1 SYNOPSIS | ||||||||||
| 79 | |||||||||||
| 80 | use Mojolicious::Lite; | ||||||||||
| 81 | plugin AssetPack => {pipes => [qw(Css Fetch)]}; | ||||||||||
| 82 | app->asset->process( | ||||||||||
| 83 | "app.css" => | ||||||||||
| 84 | "https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" | ||||||||||
| 85 | ); | ||||||||||
| 86 | |||||||||||
| 87 | =head1 DESCRIPTION | ||||||||||
| 88 | |||||||||||
| 89 | L<Mojolicious::Plugin::AssetPack::Pipe::Fetch> will look for "url()" in a CSS | ||||||||||
| 90 | file and fetch the resource from the remote location. | ||||||||||
| 91 | |||||||||||
| 92 | Note that this pipe is EXPERIMENTAL and subject for change. | ||||||||||
| 93 | |||||||||||
| 94 | =head1 METHODS | ||||||||||
| 95 | |||||||||||
| 96 | =head2 process | ||||||||||
| 97 | |||||||||||
| 98 | See L<Mojolicious::Plugin::AssetPack::Pipe/process>. | ||||||||||
| 99 | |||||||||||
| 100 | =head1 SEE ALSO | ||||||||||
| 101 | |||||||||||
| 102 | L<Mojolicious::Plugin::AssetPack>. | ||||||||||
| 103 | |||||||||||
| 104 | =cut | ||||||||||