| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::Plugin::Minify::Js; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::Plugin::Minify::Js - Javascript minifier plugin for the Nile framework. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Nile::Plugin::Minify::Js - Javascript minifier plugin for the Nile framework. | ||
| 26 | |||
| 27 | $app->plugin->minify->js($output_file => $file1, $file2, $url1, $url2, ...); | ||
| 28 | |||
| 29 | $app->plugin->minify->js('site.js' => '/js/menu.js', '/js/news.js'); | ||
| 30 | |||
| 31 | my $path = $app->var->get("js_dir"); | ||
| 32 | |||
| 33 | $app->plugin->minify->js( | ||
| 34 | $app->file->catfile($path, "app.js") => | ||
| 35 | $app->file->catfile($path, "jquery.tweet.js"), | ||
| 36 | $app->file->catfile($path, "jquery.loading.js"), | ||
| 37 | $app->file->catfile($path, "jquery.likes.js"), | ||
| 38 | "http://domain.com/js/menu/menu.js", | ||
| 39 | ); | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | use Nile::Base; | ||
| 44 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 45 | has 'xsmod' => ( | ||
| 46 | is => 'rw', | ||
| 47 | default => sub { | ||
| 48 | if (!eval "use JavaScript::Minifier::XS;1;") { | ||
| 49 | eval "use JavaScript::Minifier;1;"; | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | return 1; | ||
| 53 | }, | ||
| 54 | ); | ||
| 55 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 56 | sub process { | ||
| 57 | |||
| 58 | my ($self, $out, @files) = @_; | ||
| 59 | |||
| 60 | my $content = ""; | ||
| 61 | |||
| 62 | foreach my $file (@files) { | ||
| 63 | $content .= $self->process_file($file); | ||
| 64 | } | ||
| 65 | |||
| 66 | $self->app->file->put($out, $content); | ||
| 67 | } | ||
| 68 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 69 | sub process_file { | ||
| 70 | |||
| 71 | my ($self, $file) = @_; | ||
| 72 | |||
| 73 | my $content = $self->app->file->get($file); | ||
| 74 | |||
| 75 | if ($self->xsmod) { | ||
| 76 | return JavaScript::Minifier::XS::minify(input => $content) || $self->app->abort("JavaScript::Minifier::XS Error"); | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | return JavaScript::Minifier::minify(input => $content) || $self->app->abort("JavaScript::Minifier Error"); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | =head1 Bugs | ||
| 87 | |||
| 88 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 89 | |||
| 90 | =head1 HOMEPAGE | ||
| 91 | |||
| 92 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 93 | |||
| 94 | =head1 SOURCE | ||
| 95 | |||
| 96 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 97 | |||
| 98 | =head1 SEE ALSO | ||
| 99 | |||
| 100 | See L<Nile> for details about the complete framework. | ||
| 101 | |||
| 102 | =head1 AUTHOR | ||
| 103 | |||
| 104 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 105 | Website: http://www.mewsoft.com | ||
| 106 | |||
| 107 | =head1 COPYRIGHT AND LICENSE | ||
| 108 | |||
| 109 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 110 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 111 | |||
| 112 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | 1; |