forked from jdegoes/stax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactor-js
executable file
·57 lines (39 loc) · 1.77 KB
/
factor-js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/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