diff options
author | Byron Jones <glob@mozilla.com> | 2014-09-17 18:28:09 +0200 |
---|---|---|
committer | Byron Jones <glob@mozilla.com> | 2014-09-17 18:28:09 +0200 |
commit | 1bead98c517f658b17d42f2f79d8ce80317a7040 (patch) | |
tree | 16b569221a7d8b5124f92290f6be11122e6f2c14 /Bugzilla/Template.pm | |
parent | 2288b4680c04ec00bff02179de926fdfbd896c79 (diff) | |
download | bugzilla-1bead98c517f658b17d42f2f79d8ce80317a7040.tar.gz bugzilla-1bead98c517f658b17d42f2f79d8ce80317a7040.tar.xz |
Bug 1064395: concatenate and slightly minify javascript files
r=dkl,a=glob
Diffstat (limited to 'Bugzilla/Template.pm')
-rw-r--r-- | Bugzilla/Template.pm | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/Bugzilla/Template.pm b/Bugzilla/Template.pm index aee7933ed..c8f56d73d 100644 --- a/Bugzilla/Template.pm +++ b/Bugzilla/Template.pm @@ -530,7 +530,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); } @@ -543,6 +543,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) = @_; @@ -1054,6 +1102,7 @@ sub create { 'css_files' => \&css_files, yui_resolve_deps => \&yui_resolve_deps, + concatenate_js => \&_concatenate_js, # All classifications (sorted by sortkey, name) 'all_classifications' => sub { |