summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2015-08-17 14:51:14 +0200
committerYorhel <git@yorhel.nl>2015-08-17 14:52:47 +0200
commitc24962392d8959eb912be14b225d2e8a08ae3f54 (patch)
tree92675bcfd9b7a3abdc8d552ae0bd51033e17b75f /util
parent7f81ec60b4c3fd1b6ef5aed2fe4a7f44ee21eeb1 (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.
Diffstat (limited to 'util')
-rwxr-xr-xutil/jsgen.pl37
1 files changed, 30 insertions, 7 deletions
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;
}
}