diff options
author | Byron Jones <glob@mozilla.com> | 2014-09-17 18:33:47 +0200 |
---|---|---|
committer | Byron Jones <glob@mozilla.com> | 2014-09-17 18:33:47 +0200 |
commit | c8c2d8e219c8da48c547a15c227dcf934b2d7b57 (patch) | |
tree | 6cb3b527c707c1b890b309379d3fa930cf310bde /Bugzilla | |
parent | d4a04eb346c3f17591337c9df26796323ae9274f (diff) | |
download | bugzilla-c8c2d8e219c8da48c547a15c227dcf934b2d7b57.tar.gz bugzilla-c8c2d8e219c8da48c547a15c227dcf934b2d7b57.tar.xz |
Bug 1064395: concatenate and slightly minify javascript files
Diffstat (limited to 'Bugzilla')
-rw-r--r-- | Bugzilla/Constants.pm | 7 | ||||
-rw-r--r-- | Bugzilla/Install/Filesystem.pm | 25 | ||||
-rw-r--r-- | Bugzilla/Template.pm | 51 |
3 files changed, 76 insertions, 7 deletions
diff --git a/Bugzilla/Constants.pm b/Bugzilla/Constants.pm index a37638705..0c3c72a49 100644 --- a/Bugzilla/Constants.pm +++ b/Bugzilla/Constants.pm @@ -46,6 +46,8 @@ use Memoize; bz_locations + CONCATENATE_ASSETS + IS_NULL NOT_NULL @@ -223,6 +225,11 @@ use constant REST_DOC => "http://www.bugzilla.org/docs/tip/en/html/api/"; use constant REMOTE_FILE => 'http://updates.bugzilla.org/bugzilla-update.xml'; use constant LOCAL_FILE => 'bugzilla-update.xml'; # Relative to datadir. +# When true CSS and JavaScript assets will be concatanted and minified at +# run-time, to reduce the number of requests required to render a page. +# Setting this to a false value can help debugging. +use constant CONCATENATE_ASSETS => 1; + # These are unique values that are unlikely to match a string or a number, # to be used in criteria for match() functions and other things. They start # and end with spaces because most Bugzilla stuff has trim() called on it, diff --git a/Bugzilla/Install/Filesystem.pm b/Bugzilla/Install/Filesystem.pm index 9c2d3026f..621ece803 100644 --- a/Bugzilla/Install/Filesystem.pm +++ b/Bugzilla/Install/Filesystem.pm @@ -38,6 +38,7 @@ use File::Find; use File::Path; use File::Basename; use File::Copy qw(move); +use File::Slurp; use IO::File; use POSIX (); @@ -382,7 +383,7 @@ EOT "$assetsdir/.htaccess" => { perms => WS_SERVE, contents => <<EOT # Allow access to .css files -<FilesMatch \\.css\$> +<FilesMatch \\.(css|js)\$> Allow from all </FilesMatch> @@ -425,6 +426,7 @@ sub update_filesystem { my $datadir = bz_locations->{'datadir'}; my $graphsdir = bz_locations->{'graphsdir'}; + my $assetsdir = bz_locations->{'assetsdir'}; # If the graphs/ directory doesn't exist, we're upgrading from # a version old enough that we need to update the $datadir/mining # format. @@ -458,6 +460,13 @@ sub update_filesystem { _rename_file($oldparamsfile, "$datadir/$oldparamsfile"); } + # Remove old assets htaccess file to force recreation with correct values. + if (-e "$assetsdir/.htaccess") { + if (read_file("$assetsdir/.htaccess") =~ /<FilesMatch \\\.css\$>/) { + unlink("$assetsdir/.htaccess"); + } + } + _create_files(%files); if ($params->{index_html}) { _create_files(%{$fs->{index_html}}); @@ -501,7 +510,7 @@ EOT _remove_empty_css_files(); _convert_single_file_skins(); - _remove_dynamic_css_files(); + _remove_dynamic_assets(); } sub _remove_empty_css_files { @@ -546,10 +555,14 @@ sub _convert_single_file_skins { } } -# delete all automatically generated css files to force recreation at the next -# request. -sub _remove_dynamic_css_files { - foreach my $file (glob(bz_locations()->{assetsdir} . '/*.css')) { +# delete all automatically generated css/js files to force recreation at the +# next request. +sub _remove_dynamic_assets { + my @files = ( + glob(bz_locations()->{assetsdir} . '/*.css'), + glob(bz_locations()->{assetsdir} . '/*.js'), + ); + foreach my $file (@files) { unlink($file); } diff --git a/Bugzilla/Template.pm b/Bugzilla/Template.pm index 057298b06..6d6aee750 100644 --- a/Bugzilla/Template.pm +++ b/Bugzilla/Template.pm @@ -512,7 +512,7 @@ sub _concatenate_css { write_file($file, $content); } - $file =~ s/^\Q$cgi_path\E\///; + $file =~ s/^\Q$cgi_path\E\///o; return mtime_filter($file); } @@ -525,6 +525,54 @@ sub _css_url_rewrite { return 'url(../../' . dirname($source) . '/' . $url . ')'; } +sub _concatenate_js { + return @_ unless CONCATENATE_ASSETS; + my ($sources) = @_; + return [] unless $sources && ref($sources); + + my %files = + map { + (my $file = $_) =~ s/(^[^\?]+)\?.+/$1/; + $_ => $file; + } @$sources; + + my $cgi_path = bz_locations()->{cgi_path}; + my $skins_path = bz_locations()->{assetsdir}; + + # build minified files + my @minified; + foreach my $source (@$sources) { + next unless -e "$cgi_path/$files{$source}"; + my $file = $skins_path . '/' . md5_hex($source) . '.js'; + if (!-e $file) { + my $content = read_file("$cgi_path/$files{$source}"); + + # minimal minification + $content =~ s#/\*.*?\*/##sg; # block comments + $content =~ s#(^ +| +$)##gm; # leading/trailing spaces + $content =~ s#^//.+$##gm; # single line comments + $content =~ s#\n{2,}#\n#g; # blank lines + $content =~ s#(^\s+|\s+$)##g; # whitespace at the start/end of file + + write_file($file, "/* $files{$source} */\n" . $content . "\n"); + } + push @minified, $file; + } + + # concat files + my $file = $skins_path . '/' . md5_hex(join(' ', @$sources)) . '.js'; + if (!-e $file) { + my $content = ''; + foreach my $source (@minified) { + $content .= read_file($source); + } + write_file($file, $content); + } + + $file =~ s/^\Q$cgi_path\E\///o; + return [ $file ]; +} + # YUI dependency resolution sub yui_resolve_deps { my ($yui, $yui_deps) = @_; @@ -1003,6 +1051,7 @@ sub create { 'css_files' => \&css_files, yui_resolve_deps => \&yui_resolve_deps, + concatenate_js => \&_concatenate_js, # Whether or not keywords are enabled, in this Bugzilla. 'use_keywords' => sub { return Bugzilla::Keyword->any_exist; }, |