Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0917213721
Fetching contributors…

Cannot retrieve contributors at this time

executable file 58 lines (39 sloc) 1.808 kb
#!/usr/bin/perl
sub read_file {open my $fh, '<', shift || $_; my $result = join '', <$fh>; close $fh; $result}
sub pieces_for {
my $code = shift || $_;
$code = read_file $code if -f $code;
map split(/^(\S.*\(\);)$/mo, $_), map split(/^(if.*)$/mo, $_), split(/^([\$\w\.\[\]"']+\h*=\h*(?:function.*?\{(?:\V|\n\h)+\n^\}+;?$|\V*$))/mo, $code);
}
sub name_of {
my @matches = $_[0] =~ /^[\w\.\$\[\]'"]+/sgo;
shift(@matches) || $_[0];
}
my @files = map [pieces_for $_], @ARGV;
my %intersection;
map ++$intersection{name_of $_}, @$_ for @files;
# Delete entries appearing in fewer than all files:
$intersection{$_} < @files and delete $intersection{$_} for keys %intersection;
# And write the common definitions. Arbitrarily, we use the first file for ordering:
open my $fh, '>', 'common.js';
$intersection{name_of $_} and print $fh $_, "\n" for @{$files[0]};
close $fh;
# Now write the diffs, using whichever ordering each file gives:
for my $i (0 .. $#files) {
my ($f, $name) = ($files[$i], $ARGV[$i] =~ /(.*)\.js$/go);
open my $fh, '>', "$name.diff.js";
$intersection{name_of $_} or print $fh $_, "\n" for @$f;
close $fh;
}
__END__
=head1 NAME
factor-js - Factor common definitions out of multiple JavaScript files
=head1 SYNOPSYS
factor-js file1.js file2.js ... fileN.js
=head1 DESCRIPTION
Looks for common definitions within all of the given files. Anything within the intersection is stored in C<common.js>, and further definitions required to reconstruct the original files are
stored in file1.diff.js, file2.diff.js, ..., fileN.diff.js. The order of definitions is preserved.
If you are planning on using this in conjunction with minify-js, then you should first run minify-js on each input file and then run factor-js on all of them.
=head1 SEE ALSO
L<minify-js>(1)
=cut
Jump to Line
Something went wrong with that request. Please try again.