replace in reverse length order - e.g. avoid *.woff to be replaced before *.woff2 resulting in wrong fingerprints

This commit is contained in:
2015-04-08 00:46:21 +01:00
parent 1bda767ceb
commit dfe53b72a4
2 changed files with 36 additions and 9 deletions

View File

@@ -16,12 +16,12 @@ fixes for the module are worth downloading.
##TO-DO List
There are still a lot of things on the way:
* Compress files - CSS and JS mainly
* Compress files - CSS and JS mainly - DONE
* Manifest files - glue together multiple CSS and JS files
* Allow separator between files - probably default to `/* filename */`
* Compile SASS/LESS/COFFEE
* Easy to add new "processors"
* Helper method for file urls
* Helper method for file urls - DONE without helper (find & replace)
* `asset-url('assets/path/to/asset.png')` would become `public/assets/path/to/asset-version12345678.png`
* also applied to fonts, etc...

View File

@@ -192,7 +192,7 @@ sub _metadata_from_file {
my $mime_type = $ft->checktype_filename( $file->absolute );
my ( $filename, $dirs, $suffix ) =
File::Basename::fileparse( $rel_path, qr/\.[^.]*/ );
File::Basename::fileparse( $rel_path, qr/(\.[^.]*?){1,2}/ );
$self->full_digest->add( $rel_path, $fingerprint, );
return {
@@ -264,7 +264,7 @@ sub _process_file {
$asset->{'mtime'} = $mtime;
#$l->debug( 'Getting file: ', { 'filter' => \&Dumper, 'value' => $file, }, );
#$l->debug( 'Getting file: ', { 'filter' => \&Dumper, 'value' => $file, }, );
my $content = $self->_get_content(
'asset' => $asset,
'original_file' => $file,
@@ -311,6 +311,7 @@ sub _replace_asset_references {
File::Basename::dirname( $asset->{'full_path'} );
my $total_subs = 0;
my @replacements;
for my $asset_ref ( $self->asset_cache_values ) {
my $rel_path_to_ref =
File::Spec->abs2rel( $asset_ref->{'full_path'}, $base_path, );
@@ -321,11 +322,35 @@ sub _replace_asset_references {
my $target_ref = $rel_path_to_ref;
$target_ref =~ s/$suffix$/-${fingerprint}${suffix}/g;
my $count_changes = $content =~ s/$rel_path_to_ref/$target_ref/g;
if ( $content !~ m/$rel_path_to_ref/ ) {
# No replacements to make here
next;
}
push( @replacements,
{ 'from' => $rel_path_to_ref, 'to' => $target_ref, },
);
}
if ( !@replacements ) {
return $content;
}
# Replace bigger matches first
my @ordered_replacements =
sort { length( $b->{'from'} ) <=> length( $a->{'from'} ) } @replacements;
$l->debug( 'Dump var: ',
{ 'filter' => \&Dumper, 'value' => \@ordered_replacements, },
);
for my $replacement (@ordered_replacements) {
my $from_str = $replacement->{'from'};
my $to_str = $replacement->{'to'};
my $count_changes = $content =~ s/$from_str/$to_str/g;
next unless $count_changes;
$total_subs += $count_changes;
$l->debug( 'Changed "', $rel_path_to_ref, '" to "', $target_ref, '" ',
$l->debug( 'Changed "', $from_str, '" to "', $to_str, '" ',
$count_changes, ' times.', );
}
@@ -353,11 +378,13 @@ sub _get_content {
my $minified;
if ( $asset->{'suffix'} eq '.css' ) {
if ( $asset->{'suffix'} =~ /\.css$/ ) {
$asset->{'dirty_fingerprint'} = 1;
$minified = CSS::Minifier::XS::minify($original);
}
if ( $asset->{'suffix'} eq '.js' ) {
if ( $asset->{'suffix'} =~ /\.js$/ ) {
$asset->{'dirty_fingerprint'} = 1;
$minified = JavaScript::Minifier::XS::minify($original);
}