Skip to content
This repository has been archived by the owner on Mar 6, 2019. It is now read-only.

Commit

Permalink
Created several utilities in the bin directory for creating combined …
Browse files Browse the repository at this point in the history
…and minified versions of o.js.
  • Loading branch information
bluefeet committed Feb 5, 2014
1 parent 10b5421 commit 7d50b67
Show file tree
Hide file tree
Showing 4 changed files with 405 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ NEXT RELEASE
web, those will be handled independently).
- Better error messaging across the board (no more "..." errors).
- Lots of adjustments to reduce the minified size by several kilobytes.
- Created several utilities in the bin directory for creating combined
and minified versions of o.js.
Classes:
- Renamed o.Constructor to o.Class, because that is what it is and it is
less to type.
Expand Down
197 changes: 197 additions & 0 deletions bin/combine
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';

use autodie;
use FindBin qw( $Bin );
use Getopt::Long;
use Pod::Usage qw( pod2usage );

GetOptions(
'help' => \my $help,
'lib-dir=s' => \my $lib_dir,
'out-file=s' => \my $out_file,
'require=s' => \my @requires,
'version=s' => \my $version,
) or die "Unable to process options.\n";

if ($help) {
pod2usage( -verbose => 2 );
exit 0;
}

$lib_dir ||= "$Bin/../lib";
$out_file ||= "$Bin/../o.js";
@requires = map { split(/,/, $_) } @requires;

print "# lib-dir: $lib_dir\n";
print "# out-file: $out_file\n";
print "# version: $version\n" if $version;

if (!@requires) {
open(my $fh, '<', "$lib_dir/o.js");
my $js = do { local $/; <$fh> };

$js =~ s{
(\S+): \s* require\(
}{
push @requires, $1;
'';
}sxeg;
}

print '# require: ' . join(', ', @requires) . "\n";

my $files = {};
load_file( $_ ) for @requires;

print "Generating version header...\n";
my $version_command = "$Bin/version-header";
$version_command .= " --version=$version" if $version;
my $version_header = `$version_command`;
die "Version header doesn't look right!" if $version_header !~ m{^//.*v\d}s;
$version_header =~ s{\s*$}{}s;
print "Version header generated.\n";

print "Generating combined JavaScript...\n";

my $names_added = {};
my @names_sorted;
my @names_left = sort keys %$files;

while (@names_left) {
my $name = shift @names_left;
my $file = $files->{$name};
my $resolved = 1;

foreach my $require (@{ $file->{requires} }) {
$resolved = 0 if !$names_added->{$require};
}

if ($resolved) {
push @names_sorted, $name;
$names_added->{$name} = 1;
}
else {
push @names_left, $name;
}
}

print "Done generating.\nWriting combined JavaScript to '$out_file'...\n";

open(my $fh, '>', $out_file);

print $fh <<JS;
$version_header
(function() {
var root = this;
var previousO = root.o;
var o = {};
root.o = o;
root.oJS = o;
o.noConflict = function () {
root.o = previousO;
return o;
};
JS

foreach my $name (@names_sorted) {
print $fh "\n// o.$name\n";
print $fh $files->{$name}->{js};
}

print $fh "\n}).call(this);\n";

print "All done!\n";

sub load_file {
my ($name) = @_;
return if $files->{$name};
my $file = $files->{$name} = {};

print "Loading o-$name module.\n";

open(my $fh, '<', "$lib_dir/o-$name.js");
my $js = do { local $/; <$fh> };

my @requires;
$js =~ s{
var \s+ o_(\S+) \s* = \s* require\( '\./.*?' \);
}{
push @requires, $1;
'';
}sxeg;

load_file( $_ ) for @requires;

$js =~ s{module.exports =}{var o_$name =}s;
$js =~ s{var o_$name = o_$name;}{}s;
$js =~ s{^\s*(.*?)\s*$}{$1}s;
$js .= "\no.$name = o_$name;";

$file->{requires} = \@requires;
$file->{js} = "$js\n";

return;
}

__END__
=head1 NAME
combine - Combine the source o.js modules into a single file.
=head1 SYNOPSIS
cd o-js
bin/combine
git add o.js
=head1 DESCRIPTION
This script takes the various o.js source CommonJS/NPM style modules
and creates a single JavaScript file ready for inclusion in a website.
=head1 ARGUMENTS
=head2 help
bin/combine --help
Shows this documentation.
=head2 lib-dir
bin/combine --lib-dir=<path-to-lib>
The directory where the source o.js modules reside. Defaults to
C<$script_dir/../lib>.
=head2 out-file
bin/combine --out-file=<path-to-out-js>
The filename to write the resulting, combined, JavaScript to. Defaults
to C<$script_dir/../o.js>.
=head2 require
bin/combine --require=<module>,<module>
A list of required o.js modules that you would like included in the
resulting combined JavaScript. If a module specified has dependencies
that are not specified then the dependencies will automatically be
included.
Defaults to all modules required by the main C<o.js> module found in
the lib directory.
=head2 version
bin/combine --version=<version-tag>
Override the default version that will included in the version header
in the combined JavaScript.
126 changes: 126 additions & 0 deletions bin/minify
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';

use autodie;
use FindBin qw( $Bin );
use Getopt::Long;
use Pod::Usage qw( pod2usage );
use LWP::UserAgent;

GetOptions(
'help' => \my $help,
'src-file=s' => \my $src_file,
'min-file=s' => \my $min_file,
'version=s' => \my $version,
) or die "Unable to process options.\n";

if ($help) {
pod2usage( -verbose => 2 );
exit 0;
}

$src_file ||= "$Bin/../o.js";
$min_file ||= "$Bin/../o-min.js";

print "# src-file: $src_file\n";
print "# min-file: $min_file\n";
print "# version: $version\n" if $version;

open(my $src_fh, '<', $src_file);
my $src_js = do { local $/; <$src_fh> };

my $url = 'http://closure-compiler.appspot.com/compile';
my $ua = LWP::UserAgent->new();

print "Checking '$src_file' for errors...\n";

my $check_res = $ua->post(
$url,
{
js_code => $src_js,
compilation_level => 'SIMPLE_OPTIMIZATIONS',
output_format => 'text',
output_info => 'errors',
},
);

my $errors = $check_res->decoded_content();
die "You've got some errors, aborting minification!\n$errors" if $errors =~ m{\S};

print "No errors.\nGenerating version header...\n";

my $version_command = "$Bin/version-header";
$version_command .= " --version=$version" if $version;
my $version_header = `$version_command`;
die "Version header doesn't look right!" if $version_header !~ m{^//.*v\d}s;
$version_header =~ s{\s*$}{}s;

print "Version header generated.\nMinifying...\n";

my $compile_res = $ua->post(
$url,
{
js_code => $src_js,
compilation_level => 'SIMPLE_OPTIMIZATIONS',
output_format => 'text',
output_info => 'compiled_code',
},
);

my $min_js = $compile_res->decoded_content();

print "Minification done.\nWriting JavaScript to '$min_file'...\n";

$min_js = "$version_header\n$min_js";
open(my $min_fh, '>', $min_file);
print $min_fh $min_js;

print "All done!\n";

__END__
=head1 NAME
minify - Minify a combined o.js file.
=head1 SYNOPSIS
cd o-js
bin/minify
git add o-min.js
=head1 DESCRIPTION
This script takes the combined o.js produced by C<bin/combine> and
produces a minified form of that JavaScript ready for inclusion in
a production web site.
=head1 ARGUMENTS
=head2 help
bin/minify --help
Shows this documentation.
=head2 src-file
bin/minify --src-file=<path-to-o-js>
The source, unminified, C<o.js> as produced by C<bin/combine>.
Defaults to C<$script_dir/../o.js>.
=head2 min-file
bin/minify --min-file=<path-to-o-min-js>
The file to write the minified JavaScript to. Defaults to
C<$script_dir/../o-min.js>.
=head2 version
bin/minify --version=<version-tag>
Override the default version that will included in the version header
in the minified JavaScript.
Loading

0 comments on commit 7d50b67

Please sign in to comment.