summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Template.pm
diff options
context:
space:
mode:
authorByron Jones <glob@mozilla.com>2014-09-17 18:33:47 +0200
committerByron Jones <glob@mozilla.com>2014-09-17 18:33:47 +0200
commitc8c2d8e219c8da48c547a15c227dcf934b2d7b57 (patch)
tree6cb3b527c707c1b890b309379d3fa930cf310bde /Bugzilla/Template.pm
parentd4a04eb346c3f17591337c9df26796323ae9274f (diff)
downloadbugzilla-c8c2d8e219c8da48c547a15c227dcf934b2d7b57.tar.gz
bugzilla-c8c2d8e219c8da48c547a15c227dcf934b2d7b57.tar.xz
Bug 1064395: concatenate and slightly minify javascript files
Diffstat (limited to 'Bugzilla/Template.pm')
-rw-r--r--Bugzilla/Template.pm51
1 files changed, 50 insertions, 1 deletions
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; },