Skip to content

Instantly share code, notes, and snippets.

@hail2u
Created December 11, 2009 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hail2u/254221 to your computer and use it in GitHub Desktop.
Save hail2u/254221 to your computer and use it in GitHub Desktop.
Google Closure Compiler Serviceを使ってtest.jsのようなJavaScriptファイルをコンパイル(最適化)。
#!/usr/bin/env perl
# gccs.pl - Compile your JavaScript code with Google Closure Compiler Service
# Usage: gccs.pl < <original file> > <compiled file>
# License: http://hail2u.mit-license.org/2009
# Modified: 2012-01-04T18:43:15+09:00
use strict;
use warnings;
use JSON;
use LWP::UserAgent;
use Path::Class qw();
my @params = (
"output_info", "compiled_code",
"output_format", "json",
);
&main;
exit;
sub main {
my @code = <STDIN>;
push @params, "js_code", join("", @code);
my $idx = 0;
my $found_metadata = 0;
while (my $line = $code[$idx++]) {
if ($line =~ /^\/\/ ==ClosureCompiler==/) {
$found_metadata = 1;
last;
}
}
if ($found_metadata) {
while (my $line = $code[$idx++]) {
if ($line =~ /^\/\/ ==\/ClosureCompiler==/) {
last;
} elsif ($line =~ /^\/\/ @(\S+)\s*(.*)$/) {
if ($1 eq "code_path" && -e $2) {
my $js_code = ";" . Path::Class::File->new($2)->slurp() . ";";
push @params, "js_code", $js_code;
} else {
push @params, $1, $2;
}
}
}
}
&compile(@params);
}
sub compile {
my $ua = LWP::UserAgent->new;
my $res = $ua->post("http://closure-compiler.appspot.com/compile", \@_);
if ($res->is_success) {
my $c = from_json($res->decoded_content);
if (defined $c->{"serverErrors"}) {
foreach (@{$c->{"serverErrors"}}) {
warn "Error(" . $_->{"code"} . "): " . $_->{"error"};
}
die "Failed to compile";
} else {
binmode STDOUT;
print $c->{"compiledCode"};
}
} else {
die $res->status_line;
}
}
// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @code_url http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js
// @code_url http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js
// @code_path your_local_path.js
// ==/ClosureCompiler==
// API reference: http://code.google.com/intl/ja/closure/compiler/docs/api-ref.html
$(function () {
var who = "Google Closure Compiler Service";
alert("Hello " + who + "!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment