diff options
author | Yorhel <git@yorhel.nl> | 2015-08-17 14:51:14 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2015-08-17 14:52:47 +0200 |
commit | c24962392d8959eb912be14b225d2e8a08ae3f54 (patch) | |
tree | 92675bcfd9b7a3abdc8d552ae0bd51033e17b75f | |
parent | 7f81ec60b4c3fd1b6ef5aed2fe4a7f44ee21eeb1 (diff) |
jsgen: Support external command for JS compression (like uglifyjs)
Tends to compress a bit better than JavaScript::Minifier::JS. But is
also a lot slower, so not really useful when devving.
Stats for en.js:
raw gzip
uglifyjs 68199 19446
JS::Minifier::XS 79862 21624
Uncompressed 107662 28663
On an unrelated note, I like how jQuery boasts about being "Only 32kB
minified and gzipped.". That's quite a bit more than all of VNDB's
Javascript combined. For a damn library.
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | data/config_example.pl | 7 | ||||
-rw-r--r-- | data/global.pl | 12 | ||||
-rw-r--r-- | data/js/filter.js | 6 | ||||
-rwxr-xr-x | util/jsgen.pl | 37 |
5 files changed, 46 insertions, 17 deletions
@@ -61,6 +61,7 @@ Requirements util/jsgen.pl JSON::XS JavaScript::Minifier::XS (optional, minimizes JS output) + uglifyjs (optional, slower but better JS compression) util/spritegen.pl Image::Magick diff --git a/data/config_example.pl b/data/config_example.pl index 56ed2ec7..9c255bb2 100644 --- a/data/config_example.pl +++ b/data/config_example.pl @@ -23,7 +23,7 @@ package VNDB; ); -# Uncomment to disable certain features of Multi +# Uncomment to enable certain features of Multi #$M{modules}{API} = {}; #$M{modules}{APIDump} = {}; @@ -35,3 +35,8 @@ package VNDB; # pass => '<nickserv-password>', # masters => [ 'yorhel!~Ayo@your.hell' ], #}; + + +# Uncomment the compression method to use for the generated Javascript (or just leave as-is to disable compression) +#$JSGEN{compress} = 'JavaScript::Minifier::XS'; +#$JSGEN{compress} = "|/usr/bin/uglifyjs --compress --mangle"; diff --git a/data/global.pl b/data/global.pl index c8f2537b..5cefa5ff 100644 --- a/data/global.pl +++ b/data/global.pl @@ -1,9 +1,6 @@ package VNDB; -our(%O, %S, $ROOT); - - # options for TUWF our %O = ( db_login => [ 'dbi:Pg:dbname=vndb', 'vndb', 'passwd' ], @@ -18,7 +15,8 @@ our %O = ( # VNDB-specific options (object_data) -our %S = (%S, +our %S; +%S = (%S, version => `cd $VNDB::ROOT; git describe` =~ /^(.+)$/ && $1, url => 'http://vndb.org', # Only used by Multi, web pages infer their own address url_static => 'http://s.vndb.org', @@ -136,6 +134,12 @@ our %M = ( ); +# Options for jsgen.pl +our %JSGEN = ( + compress => undef, +); + + # allow the settings to be overwritten in config.pl require $ROOT.'/data/config.pl' if -f $ROOT.'/data/config.pl'; diff --git a/data/js/filter.js b/data/js/filter.js index a9766f55..b1c7d581 100644 --- a/data/js/filter.js +++ b/data/js/filter.js @@ -345,9 +345,6 @@ function filFTagInput(name, label, type) { var src = type=='tag' ? '/xml/tags.xml' : '/xml/traits.xml'; var visible = false; - var remove = function() { - ; - }; var addtag = function(ul, id, name, group) { ul.appendChild( tag('li', { fil_id: id }, @@ -388,7 +385,6 @@ function filFTagInput(name, label, type) { txt.disabled = true; if(visible) ajax(src+'?'+q.join(';'), function (hr) { - var l = []; var items = hr.responseXML.getElementsByTagName('item'); setText(ul, ''); for(var i=0; i<items.length; i++) @@ -419,7 +415,7 @@ function filFTagInput(name, label, type) { } return ''; }, - function(o) { filSelectField(o); false } + function(o) { filSelectField(o) } ); return [ diff --git a/util/jsgen.pl b/util/jsgen.pl index ae0a9402..72c78d9f 100755 --- a/util/jsgen.pl +++ b/util/jsgen.pl @@ -7,7 +7,6 @@ use warnings; use Encode 'encode_utf8'; use Cwd 'abs_path'; use JSON::XS; -eval { require JavaScript::Minifier::XS; }; our($ROOT, %S, %O); BEGIN { ($ROOT = abs_path $0) =~ s{/util/jsgen\.pl$}{}; } @@ -157,18 +156,42 @@ sub readjs { } +sub save { + my($f, $body) = @_; + my $content = encode_utf8($body); + + unlink "$f~"; + if(!$VNDB::JSGEN{compress}) { + open my $F, '>', "$f~" or die $!; + print $F $content; + close $F; + + } elsif($VNDB::JSGEN{compress} eq 'JavaScript::Minifier::XS') { + require JavaScript::Minifier::XS; + open my $F, '>', "$f~" or die $!; + print $F JavaScript::Minifier::XS::minify($content); + close $F; + + } elsif($VNDB::JSGEN{compress} =~ /^\|/) { # External command + (my $cmd = $VNDB::JSGEN{compress}) =~ s/^\|//; + open my $C, '|-', "$cmd >'$f~'" or die $!; + print $C $content; + close $C or die $!; + + } else { + die "Unrecognized compression option: '$VNDB::JSGEN{compress}'\n"; + } + + rename "$f~", $f or die $!; +} + sub jsgen { my $js = readjs 'main.js'; for my $l (VNDB::L10N::languages()) { my($l10n, $body) = l10n($l, $js); $body =~ s{/\*VARS\*/}{vars($l, $l10n)}eg; - - # JavaScript::Minifier::XS doesn't correctly handle perl's unicode, so manually encode - my $content = encode_utf8($body); - open my $NEWJS, '>', "$ROOT/static/f/js/$l.js" or die $!; - print $NEWJS $JavaScript::Minifier::XS::VERSION ? JavaScript::Minifier::XS::minify($content) : $content; - close $NEWJS; + save "$ROOT/static/f/js/$l.js", $body; } } |