Skip to content

Commit

Permalink
Updated to JavaScript::Minifier 1.15. Enabled automatic utilization o…
Browse files Browse the repository at this point in the history
…f JavaScript::Minifier::XS if available. Thanks to Fedor A. Fetisov (@faf), @zoffixznet and Yuri Myasoedov (@ymyasoedov). See #6.
  • Loading branch information
jepf committed Feb 11, 2021
1 parent 3440991 commit b363a0f
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 6.0.33 ????-??-??
- 2021-02-11 Updated to JavaScript::Minifier 1.15. Enabled automatic utilization of JavaScript::Minifier::XS if available. Thanks to Fedor A. Fetisov (@faf), @zoffixznet and Yuri Myasoedov (@ymyasoedov). See https://github.com/znuny/Znuny/issues/6.
- 2020-02-11 Enabled automatic utilization of CSS::Minifier::XS if available.
- 2020-02-11 Fixed return value format of user search. Thanks to Nicola Cordioli (@niccord).

Expand Down
6 changes: 6 additions & 0 deletions COPYING-Third-Party
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ o Name: CPAN JavaScript::Minifier
License: GNU v2 - http://www.gnu.org/licenses/gpl.html
Note: Copyright (C) 2007 by Peter Michaux

o Name: CPAN JavaScript::Minifier::XS
Web: n/a
License: GNU v1 - http://www.gnu.org/licenses/old-licenses/gpl-1.0.html
Artistic v1 - https://opensource.org/licenses/artistic-license-1.0
Note: Copyright (C) 2020 by Graham TerMarsch

o Name: CPAN YAML
Web: n/a
License: GNU v2 - http://www.gnu.org/licenses/gpl.html
Expand Down
33 changes: 31 additions & 2 deletions Kernel/System/Loader.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ package Kernel::System::Loader;
use strict;
use warnings;

use CSS::Minifier qw(); # default minifier, will only be used if CSS::Minifier:XS is not available.
use JavaScript::Minifier qw();
use CSS::Minifier qw(); # default minifier, will only be used if CSS::Minifier:XS is not available.
use JavaScript::Minifier qw(); # default minifier, will only be used if JavaScript::Minifier:XS is not available.

our @ObjectDependencies = (
'Kernel::Config',
Expand Down Expand Up @@ -396,6 +396,11 @@ sub MinifyJavaScript {
return;
}

my $IsJavaScriptMinifierXSAvailable = $Self->IsJavaScriptMinifierXSAvailable();
if ($IsJavaScriptMinifierXSAvailable) {
return JavaScript::Minifier::XS::minify( $Param{Code} );
}

return JavaScript::Minifier::minify( input => $Param{Code} );
}

Expand Down Expand Up @@ -531,6 +536,30 @@ sub CacheDelete {
return @Result;
}

=head2 IsJavaScriptMinifierXSAvailable()
Tries to load JavaScript::Minifier::XS if available which provides faster creation of minified JavaScript.
Returns true value if JavaScript::Minifier::XS is available and loaded.
=cut

sub IsJavaScriptMinifierXSAvailable {
my ( $Self, $Param ) = @_;

return $Self->{JavaScriptMinifierXSAvailable} if defined $Self->{JavaScriptMinifierXSAvailable};

$Self->{JavaScriptMinifierXSAvailable} = eval {
require JavaScript::Minifier::XS;
JavaScript::Minifier::XS->import();
1;
};

$Self->{JavaScriptMinifierXSAvailable} //= 0;

return $Self->{JavaScriptMinifierXSAvailable};
}

=head2 IsCSSMinifierXSAvailable()
Tries to load CSS::Minifier::XS if available which provides faster creation of minified CSS.
Expand Down
20 changes: 17 additions & 3 deletions Kernel/cpan-lib/JavaScript/Minifier.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package JavaScript::Minifier;
use strict;
use warnings;

our $VERSION = '1.14'; # VERSION
our $VERSION = '1.15'; # VERSION

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(minify);

# string 'return' as array to handle special case of returning a regexp from a function
my @return = qw(r e t u r n);

#return true if the character is allowed in identifier.
sub isAlphanum {
return ($_[0] =~ /[\w\$\\]/ || ord($_[0]) > 126);
Expand Down Expand Up @@ -225,6 +228,8 @@ sub minify {
$s->{b} = _get($s);
$s->{c} = _get($s);
$s->{d} = _get($s);
$s->{return_flag} = 0;
$s->{return_string} = '';
$s->{last} = undef; # assign for safety
$s->{lastnws} = undef; # assign for safety

Expand All @@ -237,6 +242,12 @@ sub minify {
die 'minifier bug: minify while loop starting with whitespace, stopped';
}

# track 'return' operator
if ($s->{a} ne '/') {
$s->{return_flag} = defined($return[length($s->{return_string})]) && $s->{a} eq $return[length($s->{return_string})];
$s->{return_string} = $s->{return_flag} ? $s->{return_string} . $s->{a} : '';
}

# Each branch handles trailing whitespace and ensures $s->{a} is on non-whitespace or undef when branch finishes
if ($s->{a} eq '/') { # a division, comment, or regexp literal
if (defined($s->{b}) && $s->{b} eq '/') { # slash-slash comment
Expand Down Expand Up @@ -292,8 +303,9 @@ sub minify {
die 'unterminated comment, stopped';
}
}
elsif (defined($s->{lastnws}) && ($s->{lastnws} eq ')' || $s->{lastnws} eq ']' ||
$s->{lastnws} eq '.' || isAlphanum($s->{lastnws}))) { # division
elsif ((defined($s->{lastnws}) && ($s->{lastnws} eq ')' || $s->{lastnws} eq ']' ||
$s->{lastnws} eq '.' || isAlphanum($s->{lastnws}))) && (!$s->{return_flag} || length($s->{return_string}) != scalar(@return))) { # division

action1($s);
collapseWhitespace($s);
# don't want a division to become a slash-slash comment with following conditional comment
Expand Down Expand Up @@ -433,6 +445,8 @@ Miller 'tmhall' Hall
Вячеслав 'vti' Тихановский
Fedor A. 'faf' Fetisov
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2007 by Peter Michaux
Expand Down
12 changes: 12 additions & 0 deletions bin/otrs.CheckModules.pl
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,18 @@
ports => 'security/p5-IO-Socket-SSL',
},
},
{
Module => 'JavaScript::Minifier::XS',
Required => 0,
Comment =>
'Alternative to JavaScript::Minifier in XS, which is slightly faster than JavaScript::Minifier (pure Perl).',
InstTypes => {
aptget => 'libjavascript-minifier-xs-perl',
emerge => 'dev-perl/JavaScript-Minifier-XS',
zypper => 'perl-JavaScript-Minifier-XS',
ports => 'converters/p5-JavaScript-Minifier-XS',
},
},
{
Module => 'JSON::XS',
Required => 0,
Expand Down
28 changes: 24 additions & 4 deletions scripts/test/Loader.t
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,22 @@ my $Home = $ConfigObject->Get('Home');

my $MinifiedJS = $LoaderObject->MinifyJavaScript( Code => $JavaScript );

# Remove any trailing new lines (also from last line, therefore no chomp)
$MinifiedJS =~ s{\n+\z}{}ms;

my $ExpectedMinifiedFilename = 'OTRS.Agent.App.Login.min_for_javascript_minifier.js';
if ( $LoaderObject->IsJavaScriptMinifierXSAvailable() ) {
$ExpectedMinifiedFilename = 'OTRS.Agent.App.Login.min_for_javascript_minifier_xs.js';
}

my $ExpectedJS = $MainObject->FileRead(
Location => $Home . '/scripts/test/sample/Loader/OTRS.Agent.App.Login.min.js',
Location => "$Home/scripts/test/sample/Loader/$ExpectedMinifiedFilename",
);
$ExpectedJS = ${$ExpectedJS};
$ExpectedJS =~ s{\r\n}{\n}xmsg;

#chomp $ExpectedJS;
# Remove any trailing new lines (also from last line, therefore no chomp)
$ExpectedJS =~ s{\n+\z}{}ms;

$Self->Is(
$MinifiedJS || '',
Expand Down Expand Up @@ -147,16 +156,27 @@ my $Home = $ConfigObject->Get('Home');
my $MinifiedJS = $MainObject->FileRead(
Location => $ConfigObject->Get('TempDir') . "/$MinifiedJSFilename",
);

$MinifiedJS = ${$MinifiedJS};
$MinifiedJS =~ s{\r\n}{\n}xmsg;
chomp $MinifiedJS;

# Remove any trailing new lines (also from last line, therefore no chomp)
$MinifiedJS =~ s{\n+\z}{}ms;

my $ExpectedMinifiedFilename = 'CombinedJavaScript.min_for_javascript_minifier.js';
if ( $LoaderObject->IsJavaScriptMinifierXSAvailable() ) {
$ExpectedMinifiedFilename = 'CombinedJavaScript.min_for_javascript_minifier_xs.js';
}

my $Expected = $MainObject->FileRead(
Location => $Home . '/scripts/test/sample/Loader/CombinedJavaScript.min.js',
Location => "$Home/scripts/test/sample/Loader/$ExpectedMinifiedFilename",
);
$Expected = ${$Expected};
$Expected =~ s{\r\n}{\n}xmsg;

# Remove any trailing new lines (also from last line, therefore no chomp)
$Expected =~ s{\n+\z}{}ms;

$Self->Is(
$MinifiedJS,
$Expected,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (C) 2001-2021 OTRS AG, https://otrs.com/\n";
"use strict";var OTRS=OTRS||{};OTRS.Agent=OTRS.Agent||{};OTRS.Agent.App=OTRS.Agent.App||{};OTRS.Agent.App.Login=(function(TargetNS){TargetNS.Init=function(){if(!OTRS.Debug.BrowserCheck()){$('#LoginBox').hide();$('#OldBrowser').show();return;}
OTRS.Form.EnableForm($('#LoginBox form, #PasswordBox form'));if($('#User').val()&&$('#User').val().length){$('#Password').focus();}
else{$('#User').focus();}
$('#LostPassword, #BackToLogin').click(function(){$('#LoginBox, #PasswordBox').toggle();return false;});Now=new Date();$('#TimeOffset').val(Now.getTimezoneOffset());}
return TargetNS;}(OTRS.Agent.App.Login||{}));

0 comments on commit b363a0f

Please sign in to comment.