| 1 | package Silki::Web::Javascript; | ||
| 2 | { | ||
| 3 | $Silki::Web::Javascript::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use JavaScript::Minifier::XS qw( minify ); | ||
| 11 | use JSAN::ServerSide 0.04; | ||
| 12 | use Path::Class; | ||
| 13 | use Silki::Config; | ||
| 14 | |||
| 15 | use Moose; | ||
| 16 | |||
| 17 | with 'Silki::Role::Web::CombinedStaticFiles'; | ||
| 18 | |||
| 19 | sub _build_header { | ||
| 20 | return q[var JSAN = { "use": function () {} };] . "\n"; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub _build_files { | ||
| 24 | my $dir = dir( Silki::Config->instance()->share_dir(), 'js-source' ); | ||
| 25 | |||
| 26 | # Works around an error that comes from JSAN::Parse::FileDeps | ||
| 27 | # attempting to assign $_, which is somehow read-only. | ||
| 28 | local $_; | ||
| 29 | my $js = JSAN::ServerSide->new( | ||
| 30 | js_dir => $dir->stringify(), | ||
| 31 | |||
| 32 | # This is irrelevant, as we won't be | ||
| 33 | # serving the individual files. | ||
| 34 | uri_prefix => '/', | ||
| 35 | ); | ||
| 36 | |||
| 37 | $js->add('Silki'); | ||
| 38 | |||
| 39 | return [ map { file($_) } $js->files() ]; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _build_target_file { | ||
| 43 | my $js_dir = dir( Silki::Config->instance()->var_lib_dir(), 'js' ); | ||
| 44 | |||
| 45 | $js_dir->mkpath( 0, 0755 ); | ||
| 46 | |||
| 47 | return file( $js_dir, 'silki-combined.js' ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _squish { | ||
| 51 | my $self = shift; | ||
| 52 | my $code = shift; | ||
| 53 | |||
| 54 | return $code | ||
| 55 | unless Silki::Config->instance()->is_production(); | ||
| 56 | |||
| 57 | return minify($code); | ||
| 58 | } | ||
| 59 | |||
| 60 | __PACKAGE__->meta()->make_immutable(); | ||
| 61 | |||
| 62 | 1; | ||
| 63 | |||
| 64 | # ABSTRACT: Combines and minifies Javascript source files | ||
| 65 | |||
| 66 | __END__ |